text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE789_Uncontrolled_Mem_Alloc__malloc_char_rand_21.c
  Label Definition File: CWE789_Uncontrolled_Mem_Alloc__malloc.label.xml
  Template File: sources-sinks-21.tmpl.c
  */
  /*
   * @description
   * CWE: 789 Uncontrolled Memory Allocation
   * BadSource: rand Set data to result of rand(), which may be zero
   * GoodSource: Small number greater than zero
   * Sinks:
   *    GoodSink: Allocate memory with malloc() and check the size of the memory to be allocated
   *    BadSink : Allocate memory with malloc(), but incorrectly check the size of the memory to be allocated
   * Flow Variant: 21 Control flow: Flow controlled by value of a static global variable. All functions contained in one file.
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #define HELLO_STRING "hello"
  
  #ifndef OMITBAD
  
  /* The static variable below is used to drive control flow in the sink function */
  static int badStatic = 0;
  
  static void badSink(size_t data)
  {
      if(badStatic)
      {
          {
              char * myString;
              /* POTENTIAL FLAW: No MAXIMUM limitation for memory allocation, but ensure data is large enough
               * for the strcpy() function to not cause a buffer overflow */
              /* INCIDENTAL FLAW: The source could cause a type overrun in data or in the memory allocation */
              if (data > strlen(HELLO_STRING))
              {
                  myString = (char *)malloc(data*sizeof(char));
                  /* Copy a small string into myString */
                  strcpy(myString, HELLO_STRING);
                  printLine(myString);
                  free(myString);
              }
              else
              {
                  printLine("Input is less than the length of the source string");
              }
          }
      }
  }
  
  void CWE789_Uncontrolled_Mem_Alloc__malloc_char_rand_21_bad()
  {
      size_t data;
      /* Initialize data */
      data = 0;
      /* POTENTIAL FLAW: Set data to a random value */
      data = rand();
      badStatic = 1; /* true */
      badSink(data);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* The static variables below are used to drive control flow in the sink functions. */
  static int goodB2G1Static = 0;
  static int goodB2G2Static = 0;
  static int goodG2BStatic = 0;
  
  /* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */
  static void goodB2G1Sink(size_t data)
  {
      if(goodB2G1Static)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          {
              char * myString;
              /* FIX: Include a MAXIMUM limitation for memory allocation and a check to ensure data is large enough
               * for the strcpy() function to not cause a buffer overflow */
              /* INCIDENTAL FLAW: The source could cause a type overrun in data or in the memory allocation */
              if (data > strlen(HELLO_STRING) && data < 100)
              {
                  myString = (char *)malloc(data*sizeof(char));
                  /* Copy a small string into myString */
                  strcpy(myString, HELLO_STRING);
                  printLine(myString);
                  free(myString);
              }
              else
              {
                  printLine("Input is less than the length of the source string or too large");
              }
          }
      }
  }
  
  static void goodB2G1()
  {
      size_t data;
      /* Initialize data */
      data = 0;
      /* POTENTIAL FLAW: Set data to a random value */
      data = rand();
      goodB2G1Static = 0; /* false */
      goodB2G1Sink(data);
  }
  
  /* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */
  static void goodB2G2Sink(size_t data)
  {
      if(goodB2G2Static)
      {
          {
              char * myString;
              /* FIX: Include a MAXIMUM limitation for memory allocation and a check to ensure data is large enough
               * for the strcpy() function to not cause a buffer overflow */
              /* INCIDENTAL FLAW: The source could cause a type overrun in data or in the memory allocation */
              if (data > strlen(HELLO_STRING) && data < 100)
              {
                  myString = (char *)malloc(data*sizeof(char));
                  /* Copy a small string into myString */
                  strcpy(myString, HELLO_STRING);
                  printLine(myString);
                  free(myString);
              }
              else
              {
                  printLine("Input is less than the length of the source string or too large");
              }
          }
      }
  }
  
  static void goodB2G2()
  {
      size_t data;
      /* Initialize data */
      data = 0;
      /* POTENTIAL FLAW: Set data to a random value */
      data = rand();
      goodB2G2Static = 1; /* true */
      goodB2G2Sink(data);
  }
  
  /* goodG2B() - use goodsource and badsink */
  static void goodG2BSink(size_t data)
  {
      if(goodG2BStatic)
      {
          {
              char * myString;
              /* POTENTIAL FLAW: No MAXIMUM limitation for memory allocation, but ensure data is large enough
               * for the strcpy() function to not cause a buffer overflow */
              /* INCIDENTAL FLAW: The source could cause a type overrun in data or in the memory allocation */
              if (data > strlen(HELLO_STRING))
              {
                  myString = (char *)malloc(data*sizeof(char));
                  /* Copy a small string into myString */
                  strcpy(myString, HELLO_STRING);
                  printLine(myString);
                  free(myString);
              }
              else
              {
                  printLine("Input is less than the length of the source string");
              }
          }
      }
  }
  
  static void goodG2B()
  {
      size_t data;
      /* Initialize data */
      data = 0;
      /* FIX: Use a relatively small number for memory allocation */
      data = 20;
      goodG2BStatic = 1; /* true */
      goodG2BSink(data);
  }
  
  void CWE789_Uncontrolled_Mem_Alloc__malloc_char_rand_21_good()
  {
      goodB2G1();
      goodB2G2();
      goodG2B();
  }
  
  #endif /* OMITGOOD */
  
  /* Below is the main(). It is only used when building this testcase on
     its own for testing or for building a binary to use in testing binary
     analysis tools. It is not used when compiling all the testcases as one
     application, which is how source code analysis tools are tested. */
  
  #ifdef INCLUDEMAIN
  
  int main(int argc, char * argv[])
  {
      /* seed randomness */
      srand( (unsigned)time(NULL) );
  #ifndef OMITGOOD
      printLine("Calling good()...");
      CWE789_Uncontrolled_Mem_Alloc__malloc_char_rand_21_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE789_Uncontrolled_Mem_Alloc__malloc_char_rand_21_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  