text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE590_Free_Memory_Not_on_Heap__free_int64_t_declare_08.c
  Label Definition File: CWE590_Free_Memory_Not_on_Heap__free.label.xml
  Template File: sources-sink-08.tmpl.c
  */
  /*
   * @description
   * CWE: 590 Free Memory Not on Heap
   * BadSource: declare Data buffer is declared on the stack
   * GoodSource: Allocate memory on the heap
   * Sink:
   *    BadSink : Print then free data
   * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  /* The two function below always return the same value, so a tool
   * should be able to identify that calls to the functions will always
   * return a fixed value.
   */
  static int staticReturnsTrue()
  {
      return 1;
  }
  
  static int staticReturnsFalse()
  {
      return 0;
  }
  
  #ifndef OMITBAD
  
  void CWE590_Free_Memory_Not_on_Heap__free_int64_t_declare_08_bad()
  {
      int64_t * data;
      data = NULL; /* Initialize data */
      if(staticReturnsTrue())
      {
          {
              /* FLAW: data is allocated on the stack and deallocated in the BadSink */
              int64_t dataBuffer[100];
              {
                  size_t i;
                  for (i = 0; i < 100; i++)
                  {
                      dataBuffer[i] = 5LL;
                  }
              }
              data = dataBuffer;
          }
      }
      printLongLongLine(data[0]);
      /* POTENTIAL FLAW: Possibly deallocating memory allocated on the stack */
      free(data);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B1() - use goodsource and badsink by changing the staticReturnsTrue() to staticReturnsFalse() */
  static void goodG2B1()
  {
      int64_t * data;
      data = NULL; /* Initialize data */
      if(staticReturnsFalse())
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          {
              /* FIX: data is allocated on the heap and deallocated in the BadSink */
              int64_t * dataBuffer = (int64_t *)malloc(100*sizeof(int64_t));
              if (dataBuffer == NULL)
              {
                  printLine("malloc() failed");
                  exit(1);
              }
              {
                  size_t i;
                  for (i = 0; i < 100; i++)
                  {
                      dataBuffer[i] = 5LL;
                  }
              }
              data = dataBuffer;
          }
      }
      printLongLongLine(data[0]);
      /* POTENTIAL FLAW: Possibly deallocating memory allocated on the stack */
      free(data);
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */
  static void goodG2B2()
  {
      int64_t * data;
      data = NULL; /* Initialize data */
      if(staticReturnsTrue())
      {
          {
              /* FIX: data is allocated on the heap and deallocated in the BadSink */
              int64_t * dataBuffer = (int64_t *)malloc(100*sizeof(int64_t));
              if (dataBuffer == NULL)
              {
                  printLine("malloc() failed");
                  exit(1);
              }
              {
                  size_t i;
                  for (i = 0; i < 100; i++)
                  {
                      dataBuffer[i] = 5LL;
                  }
              }
              data = dataBuffer;
          }
      }
      printLongLongLine(data[0]);
      /* POTENTIAL FLAW: Possibly deallocating memory allocated on the stack */
      free(data);
  }
  
  void CWE590_Free_Memory_Not_on_Heap__free_int64_t_declare_08_good()
  {
      goodG2B1();
      goodG2B2();
  }
  
  #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_int64_t_declare_08_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE590_Free_Memory_Not_on_Heap__free_int64_t_declare_08_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  