text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE789_Uncontrolled_Mem_Alloc__new_char_fscanf_17.cpp
  Label Definition File: CWE789_Uncontrolled_Mem_Alloc__new.label.xml
  Template File: sources-sinks-17.tmpl.cpp
  */
  /*
   * @description
   * CWE: 789 Uncontrolled Memory Allocation
   * BadSource: fscanf Read data from the console using fscanf()
   * GoodSource: Small number greater than zero
   * Sinks:
   *    GoodSink: Allocate memory with new [] and check the size of the memory to be allocated
   *    BadSink : Allocate memory with new [], but incorrectly check the size of the memory to be allocated
   * Flow Variant: 17 Control flow: for loops
   * */
  
  #include "std_testcase.h"
  
  #define HELLO_STRING "hello"
  
  namespace CWE789_Uncontrolled_Mem_Alloc__new_char_fscanf_17
  {
  
  #ifndef OMITBAD
  
  void bad()
  {
      int h,i,j,k;
      int data;
      /* Initialize data */
      data = -1;
      for(h = 0; h < 0; h++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Use a relatively small number for memory allocation */
          data = 20;
      }
      for(i = 0; i < 1; i++)
      {
          fscanf (stdin, "%d", &data);
      }
      for(j = 0; j < 1; j++)
      {
          {
              char * char_string;
              /* FLAW: No MAXIMUM limitation for memory allocation and no 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 > 0)
              {
                  char_string = new char[data];
                  /* Copy a small string into char_string$ */
                  strcpy(char_string, HELLO_STRING);
                  printLine(char_string);
                  delete [] char_string;
              }
              else
              {
                  printLine("Input is less than 0");
              }
          }
      }
      for(k = 0; k < 0; k++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              char * char_string;
              /* 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 ((size_t)data > strlen(HELLO_STRING) && data < 100)
              {
                  char_string = new char[data];
                  /* Copy a small string into char_string$ */
                  strcpy(char_string, HELLO_STRING);
                  printLine(char_string);
                  delete [] char_string;
              }
              else
              {
                  printLine("Input is less than the length of the source string or too large");
              }
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G() - use badsource and goodsink by changing the conditions on the third and fourth for statements */
  static void goodB2G()
  {
      int h,i,j,k;
      int data;
      /* Initialize data */
      data = -1;
      for(h = 0; h < 0; h++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Use a relatively small number for memory allocation */
          data = 20;
      }
      for(i = 0; i < 1; i++)
      {
          fscanf (stdin, "%d", &data);
      }
      for(j = 0; j < 0; j++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              char * char_string;
              /* FLAW: No MAXIMUM limitation for memory allocation and no 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 > 0)
              {
                  char_string = new char[data];
                  /* Copy a small string into char_string$ */
                  strcpy(char_string, HELLO_STRING);
                  printLine(char_string);
                  delete [] char_string;
              }
              else
              {
                  printLine("Input is less than 0");
              }
          }
      }
      for(k = 0; k < 1; k++)
      {
          {
              char * char_string;
              /* 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 ((size_t)data > strlen(HELLO_STRING) && data < 100)
              {
                  char_string = new char[data];
                  /* Copy a small string into char_string$ */
                  strcpy(char_string, HELLO_STRING);
                  printLine(char_string);
                  delete [] char_string;
              }
              else
              {
                  printLine("Input is less than the length of the source string or too large");
              }
          }
      }
  }
  
  /* goodG2B() - use goodsource and badsink by changing the conditions on the first and second for statements */
  static void goodG2B()
  {
      int h,i,j,k;
      int data;
      /* Initialize data */
      data = -1;
      for(h = 0; h < 1; h++)
      {
          /* FIX: Use a relatively small number for memory allocation */
          data = 20;
      }
      for(i = 0; i < 0; i++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          fscanf (stdin, "%d", &data);
      }
      for(j = 0; j < 1; j++)
      {
          {
              char * char_string;
              /* FLAW: No MAXIMUM limitation for memory allocation and no 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 > 0)
              {
                  char_string = new char[data];
                  /* Copy a small string into char_string$ */
                  strcpy(char_string, HELLO_STRING);
                  printLine(char_string);
                  delete [] char_string;
              }
              else
              {
                  printLine("Input is less than 0");
              }
          }
      }
      for(k = 0; k < 0; k++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              char * char_string;
              /* 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 ((size_t)data > strlen(HELLO_STRING) && data < 100)
              {
                  char_string = new char[data];
                  /* Copy a small string into char_string$ */
                  strcpy(char_string, HELLO_STRING);
                  printLine(char_string);
                  delete [] char_string;
              }
              else
              {
                  printLine("Input is less than the length of the source string or too large");
              }
          }
      }
  }
  
  void good()
  {
      goodB2G();
      goodG2B();
  }
  
  #endif /* OMITGOOD */
  
  } // close namespace
  
  /* 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
  
  using namespace CWE789_Uncontrolled_Mem_Alloc__new_char_fscanf_17; // so that we can use good and bad easily
  
  int main(int argc, char * argv[])
  {
      /* seed randomness */
      srand( (unsigned)time(NULL) );
  #ifndef OMITGOOD
      printLine("Calling good()...");
      good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  