text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE121_Stack_Based_Buffer_Overflow__CWE805_wchar_t_alloca_loop_72b.cpp
  Label Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml
  Template File: sources-sink-72b.tmpl.cpp
  */
  /*
   * @description
   * CWE: 121 Stack Based Buffer Overflow
   * BadSource:  Set data pointer to the bad buffer
   * GoodSource: Set data pointer to the good buffer
   * Sinks: loop
   *    BadSink : Copy string to data using a loop
   * 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 CWE121_Stack_Based_Buffer_Overflow__CWE805_wchar_t_alloca_loop_72
  {
  
  #ifndef OMITBAD
  
  void badSink(vector<wchar_t *> dataVector)
  {
      /* copy data out of dataVector */
      wchar_t * data = dataVector[2];
      {
          size_t i;
          wchar_t source[100];
          wmemset(source, L'C', 100-1); /* fill with L'C's */
          source[100-1] = L'\0'; /* null terminate */
          /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */
          for (i = 0; i < 100; i++)
          {
              data[i] = source[i];
          }
          data[100-1] = L'\0'; /* Ensure the destination buffer is null terminated */
          printWLine(data);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSink(vector<wchar_t *> dataVector)
  {
      wchar_t * data = dataVector[2];
      {
          size_t i;
          wchar_t source[100];
          wmemset(source, L'C', 100-1); /* fill with L'C's */
          source[100-1] = L'\0'; /* null terminate */
          /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */
          for (i = 0; i < 100; i++)
          {
              data[i] = source[i];
          }
          data[100-1] = L'\0'; /* Ensure the destination buffer is null terminated */
          printWLine(data);
      }
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  