text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE122_Heap_Based_Buffer_Overflow__c_CWE805_int64_t_memcpy_62a.cpp
  Label Definition File: CWE122_Heap_Based_Buffer_Overflow__c_CWE805.label.xml
  Template File: sources-sink-62a.tmpl.cpp
  */
  /*
   * @description
   * CWE: 122 Heap Based Buffer Overflow
   * BadSource:  Allocate using malloc() and set data pointer to a small buffer
   * GoodSource: Allocate using malloc() and set data pointer to a large buffer
   * Sinks: memcpy
   *    BadSink : Copy int64_t array to data using memcpy
   * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  namespace CWE122_Heap_Based_Buffer_Overflow__c_CWE805_int64_t_memcpy_62
  {
  
  #ifndef OMITBAD
  
  /* bad function declaration */
  void badSource(int64_t * &data);
  
  void bad()
  {
      int64_t * data;
      data = NULL;
      badSource(data);
      {
          int64_t source[100] = {0}; /* fill with 0's */
          /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */
          memcpy(data, source, 100*sizeof(int64_t));
          printLongLongLine(data[0]);
          free(data);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSource(int64_t * &data);
  
  static void goodG2B()
  {
      int64_t * data;
      data = NULL;
      goodG2BSource(data);
      {
          int64_t source[100] = {0}; /* fill with 0's */
          /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */
          memcpy(data, source, 100*sizeof(int64_t));
          printLongLongLine(data[0]);
          free(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 CWE122_Heap_Based_Buffer_Overflow__c_CWE805_int64_t_memcpy_62; /* 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
  