text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE122_Heap_Based_Buffer_Overflow__c_CWE805_char_snprintf_73b.cpp
  Label Definition File: CWE122_Heap_Based_Buffer_Overflow__c_CWE805.string.label.xml
  Template File: sources-sink-73b.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: snprintf
   *    BadSink : Copy string to data using snprintf
   * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  #include <list>
  
  #include <wchar.h>
  
  #ifdef _WIN32
  #define SNPRINTF _snprintf
  #else
  #define SNPRINTF snprintf
  #endif
  
  using namespace std;
  
  namespace CWE122_Heap_Based_Buffer_Overflow__c_CWE805_char_snprintf_73
  {
  
  #ifndef OMITBAD
  
  void badSink(list<char *> dataList)
  {
      /* copy data out of dataList */
      char * data = dataList.back();
      {
          char source[100];
          memset(source, 'C', 100-1); /* fill with 'C's */
          source[100-1] = '\0'; /* null terminate */
          /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */
          SNPRINTF(data, 100, "%s", source);
          printLine(data);
          free(data);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSink(list<char *> dataList)
  {
      char * data = dataList.back();
      {
          char source[100];
          memset(source, 'C', 100-1); /* fill with 'C's */
          source[100-1] = '\0'; /* null terminate */
          /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */
          SNPRINTF(data, 100, "%s", source);
          printLine(data);
          free(data);
      }
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  