text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE590_Free_Memory_Not_on_Heap__delete_array_int_static_16.cpp
  Label Definition File: CWE590_Free_Memory_Not_on_Heap__delete_array.label.xml
  Template File: sources-sink-16.tmpl.cpp
  */
  /*
   * @description
   * CWE: 590 Free Memory Not on Heap
   * BadSource: static Data buffer is declared static on the stack
   * GoodSource: Allocate memory on the heap
   * Sink:
   *    BadSink : Print then free data
   * Flow Variant: 16 Control flow: while(1)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  namespace CWE590_Free_Memory_Not_on_Heap__delete_array_int_static_16
  {
  
  #ifndef OMITBAD
  
  void bad()
  {
      int * data;
      data = NULL; /* Initialize data */
      while(1)
      {
          {
              /* FLAW: data is allocated on the stack and deallocated in the BadSink */
              static int dataBuffer[100];
              {
                  size_t i;
                  for (i = 0; i < 100; i++)
                  {
                      dataBuffer[i] = 5;
                  }
              }
              data = dataBuffer;
          }
          break;
      }
      printIntLine(data[0]);
      /* POTENTIAL FLAW: Possibly deallocating memory allocated on the stack */
      delete [] data;
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() - use goodsource and badsink by changing the conditions on the while statements */
  static void goodG2B()
  {
      int * data;
      data = NULL; /* Initialize data */
      while(1)
      {
          {
              /* FIX: data is allocated on the heap and deallocated in the BadSink */
              int * dataBuffer = new int[100];
              {
                  size_t i;
                  for (i = 0; i < 100; i++)
                  {
                      dataBuffer[i] = 5;
                  }
              }
              data = dataBuffer;
          }
          break;
      }
      printIntLine(data[0]);
      /* POTENTIAL FLAW: Possibly deallocating memory allocated on the stack */
      delete [] data;
  }
  
  void good()
  {
      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 CWE590_Free_Memory_Not_on_Heap__delete_array_int_static_16; /* 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
  