text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_ncpy_73b.cpp
  Label Definition File: CWE122_Heap_Based_Buffer_Overflow__c_CWE193.label.xml
  Template File: sources-sink-73b.tmpl.cpp
  */
  /*
   * @description
   * CWE: 122 Heap Based Buffer Overflow
   * BadSource:  Allocate memory for a string, but do not allocate space for NULL terminator
   * GoodSource: Allocate enough memory for a string and the NULL terminator
   * Sinks: ncpy
   *    BadSink : Copy string to data using strncpy()
   * 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>
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  /* MAINTENANCE NOTE: The length of this string should equal the 10 */
  #define SRC_STRING "AAAAAAAAAA"
  
  using namespace std;
  
  namespace CWE122_Heap_Based_Buffer_Overflow__c_CWE193_char_ncpy_73
  {
  
  #ifndef OMITBAD
  
  void badSink(list<char *> dataList)
  {
      /* copy data out of dataList */
      char * data = dataList.back();
      {
          char source[10+1] = SRC_STRING;
          /* Copy length + 1 to include NUL terminator from source */
          /* POTENTIAL FLAW: data may not have enough space to hold source */
          strncpy(data, source, strlen(source) + 1);
          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[10+1] = SRC_STRING;
          /* Copy length + 1 to include NUL terminator from source */
          /* POTENTIAL FLAW: data may not have enough space to hold source */
          strncpy(data, source, strlen(source) + 1);
          printLine(data);
          free(data);
      }
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  