text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE590_Free_Memory_Not_on_Heap__free_struct_alloca_45.c
  Label Definition File: CWE590_Free_Memory_Not_on_Heap__free.label.xml
  Template File: sources-sink-45.tmpl.c
  */
  /*
   * @description
   * CWE: 590 Free Memory Not on Heap
   * BadSource: alloca Data buffer is allocated on the stack with alloca()
   * GoodSource: Allocate memory on the heap
   * Sinks:
   *    BadSink : Print then free data
   * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  static twoIntsStruct * CWE590_Free_Memory_Not_on_Heap__free_struct_alloca_45_badData;
  static twoIntsStruct * CWE590_Free_Memory_Not_on_Heap__free_struct_alloca_45_goodG2BData;
  
  #ifndef OMITBAD
  
  static void badSink()
  {
      twoIntsStruct * data = CWE590_Free_Memory_Not_on_Heap__free_struct_alloca_45_badData;
      printStructLine(&data[0]);
      /* POTENTIAL FLAW: Possibly deallocating memory allocated on the stack */
      free(data);
  }
  
  void CWE590_Free_Memory_Not_on_Heap__free_struct_alloca_45_bad()
  {
      twoIntsStruct * data;
      data = NULL; /* Initialize data */
      {
          /* FLAW: data is allocated on the stack and deallocated in the BadSink */
          twoIntsStruct * dataBuffer = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));
          {
              size_t i;
              for (i = 0; i < 100; i++)
              {
                  dataBuffer[i].intOne = 1;
                  dataBuffer[i].intTwo = 1;
              }
          }
          data = dataBuffer;
      }
      CWE590_Free_Memory_Not_on_Heap__free_struct_alloca_45_badData = data;
      badSink();
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() uses the GoodSource with the BadSink */
  static void goodG2BSink()
  {
      twoIntsStruct * data = CWE590_Free_Memory_Not_on_Heap__free_struct_alloca_45_goodG2BData;
      printStructLine(&data[0]);
      /* POTENTIAL FLAW: Possibly deallocating memory allocated on the stack */
      free(data);
  }
  
  static void goodG2B()
  {
      twoIntsStruct * data;
      data = NULL; /* Initialize data */
      {
          /* FIX: data is allocated on the heap and deallocated in the BadSink */
          twoIntsStruct * dataBuffer = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));
          if (dataBuffer == NULL)
          {
              printLine("malloc() failed");
              exit(1);
          }
          {
              size_t i;
              for (i = 0; i < 100; i++)
              {
                  dataBuffer[i].intOne = 1;
                  dataBuffer[i].intTwo = 1;
              }
          }
          data = dataBuffer;
      }
      CWE590_Free_Memory_Not_on_Heap__free_struct_alloca_45_goodG2BData = data;
      goodG2BSink();
  }
  
  void CWE590_Free_Memory_Not_on_Heap__free_struct_alloca_45_good()
  {
      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()...");
      CWE590_Free_Memory_Not_on_Heap__free_struct_alloca_45_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE590_Free_Memory_Not_on_Heap__free_struct_alloca_45_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  