text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193_char_loop_52c.cpp
  Label Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193.label.xml
  Template File: sources-sink-52c.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
   * Sink: loop
   *    BadSink : Copy array to data using a loop
   * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  /* MAINTENANCE NOTE: The length of this string should equal the 10 */
  #define SRC_STRING "AAAAAAAAAA"
  
  namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193_char_loop_52
  {
  
  /* all the sinks are the same, we just want to know where the hit originated if a tool flags one */
  
  #ifndef OMITBAD
  
  void badSink_c(char * data)
  {
      {
          char source[10+1] = SRC_STRING;
          size_t i, sourceLen;
          sourceLen = strlen(source);
          /* Copy length + 1 to include NUL terminator from source */
          /* POTENTIAL FLAW: data may not have enough space to hold source */
          for (i = 0; i < sourceLen + 1; i++)
          {
              data[i] = source[i];
          }
          printLine(data);
          delete [] data;
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSink_c(char * data)
  {
      {
          char source[10+1] = SRC_STRING;
          size_t i, sourceLen;
          sourceLen = strlen(source);
          /* Copy length + 1 to include NUL terminator from source */
          /* POTENTIAL FLAW: data may not have enough space to hold source */
          for (i = 0; i < sourceLen + 1; i++)
          {
              data[i] = source[i];
          }
          printLine(data);
          delete [] data;
      }
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  