text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE122_Heap_Based_Buffer_Overflow__c_CWE806_wchar_t_ncpy_72b.cpp
  Label Definition File: CWE122_Heap_Based_Buffer_Overflow__c_CWE806.label.xml
  Template File: sources-sink-72b.tmpl.cpp
  */
  /*
   * @description
   * CWE: 122 Heap Based Buffer Overflow
   * BadSource:  Initialize data as a large string
   * GoodSource: Initialize data as a small string
   * Sinks: ncpy
   *    BadSink : Copy data to string using wcsncpy
   * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  #include <vector>
  
  #include <wchar.h>
  
  using namespace std;
  
  namespace CWE122_Heap_Based_Buffer_Overflow__c_CWE806_wchar_t_ncpy_72
  {
  
  #ifndef OMITBAD
  
  void badSink(vector<wchar_t *> dataVector)
  {
      /* copy data out of dataVector */
      wchar_t * data = dataVector[2];
      {
          wchar_t dest[50] = L"";
          /* POTENTIAL FLAW: Possible buffer overflow if data is larger than dest */
          wcsncpy(dest, data, wcslen(data));
          dest[50-1] = L'\0'; /* Ensure the destination buffer is null terminated */
          printWLine(data);
          free(data);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSink(vector<wchar_t *> dataVector)
  {
      wchar_t * data = dataVector[2];
      {
          wchar_t dest[50] = L"";
          /* POTENTIAL FLAW: Possible buffer overflow if data is larger than dest */
          wcsncpy(dest, data, wcslen(data));
          dest[50-1] = L'\0'; /* Ensure the destination buffer is null terminated */
          printWLine(data);
          free(data);
      }
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  