text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE590_Free_Of_Invalid_Pointer_Not_On_The_Heap__delete_array_class_static_67a.cpp
  Label Definition File: CWE590_Free_Of_Invalid_Pointer_Not_On_The_Heap__delete_array.label.xml
  Template File: sources-sink-67a.tmpl.cpp
  */
  /*
   * @description
   * CWE: 590 Free of Invalid Pointer Not on the Heap
   * BadSource: static Data buffer is declared static on the stack
   * GoodSource: Allocate memory on the heap
   * Sinks:
   *    BadSink : Print then free data
   * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  namespace CWE590_Free_Of_Invalid_Pointer_Not_On_The_Heap__delete_array_class_static_67
  {
  
  typedef struct _struct_type
  {
      twointsclass * a;
  } struct_type;
  
  #ifndef OMITBAD
  
  /* bad function declaration */
  void bad_sink(struct_type my_struct);
  
  void bad()
  {
      twointsclass * data;
      struct_type my_struct;
      data = NULL; /* Initialize data */
      {
          /* FLAW: data is allocated on the stack and deallocated in the BadSink */
          static twointsclass data_buf[100];
          {
              size_t i;
              for (i = 0; i < 100; i++)
              {
                  data_buf[i].a = 1;
                  data_buf[i].b = 1;
              }
          }
          data = data_buf;
      }
      my_struct.a = data;
      bad_sink(my_struct);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2B_sink(struct_type my_struct);
  
  static void goodG2B()
  {
      twointsclass * data;
      struct_type my_struct;
      data = NULL; /* Initialize data */
      {
          /* FIX: data is allocated on the heap and deallocated in the BadSink */
          twointsclass * data_buf = new twointsclass[100];
          {
              size_t i;
              for (i = 0; i < 100; i++)
              {
                  data_buf[i].a = 1;
                  data_buf[i].b = 1;
              }
          }
          data = data_buf;
      }
      my_struct.a = data;
      goodG2B_sink(my_struct);
  }
  
  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_Of_Invalid_Pointer_Not_On_The_Heap__delete_array_class_static_67; // 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
  