text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE121_Stack_Based_Buffer_Overflow__CWE131_memcpy_45.c
  Label Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE131.label.xml
  Template File: sources-sink-45.tmpl.c
  */
  /*
   * @description
   * CWE: 121 Stack Based Buffer Overflow
   * BadSource:  Allocate memory without using sizeof(int)
   * GoodSource: Allocate memory using sizeof(int)
   * Sinks: memcpy
   *    BadSink : Copy array to data using memcpy()
   * 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"
  
  static int * CWE121_Stack_Based_Buffer_Overflow__CWE131_memcpy_45_badData;
  static int * CWE121_Stack_Based_Buffer_Overflow__CWE131_memcpy_45_goodG2BData;
  
  #ifndef OMITBAD
  
  static void badSink()
  {
      int * data = CWE121_Stack_Based_Buffer_Overflow__CWE131_memcpy_45_badData;
      {
          int source[10] = {0};
          /* POTENTIAL FLAW: Possible buffer overflow if data was not allocated correctly in the source */
          memcpy(data, source, 10*sizeof(int));
          printIntLine(data[0]);
      }
  }
  
  void CWE121_Stack_Based_Buffer_Overflow__CWE131_memcpy_45_bad()
  {
      int * data;
      data = NULL;
      /* FLAW: Allocate memory without using sizeof(int) */
      data = (int *)ALLOCA(10);
      CWE121_Stack_Based_Buffer_Overflow__CWE131_memcpy_45_badData = data;
      badSink();
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() uses the GoodSource with the BadSink */
  static void goodG2BSink()
  {
      int * data = CWE121_Stack_Based_Buffer_Overflow__CWE131_memcpy_45_goodG2BData;
      {
          int source[10] = {0};
          /* POTENTIAL FLAW: Possible buffer overflow if data was not allocated correctly in the source */
          memcpy(data, source, 10*sizeof(int));
          printIntLine(data[0]);
      }
  }
  
  static void goodG2B()
  {
      int * data;
      data = NULL;
      /* FIX: Allocate memory using sizeof(int) */
      data = (int *)ALLOCA(10*sizeof(int));
      CWE121_Stack_Based_Buffer_Overflow__CWE131_memcpy_45_goodG2BData = data;
      goodG2BSink();
  }
  
  void CWE121_Stack_Based_Buffer_Overflow__CWE131_memcpy_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()...");
      CWE121_Stack_Based_Buffer_Overflow__CWE131_memcpy_45_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE121_Stack_Based_Buffer_Overflow__CWE131_memcpy_45_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  