text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE121_Stack_Based_Buffer_Overflow__CWE805_wchar_t_declare_loop_72a.cpp
  Label Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml
  Template File: sources-sink-72a.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_declare_loop_72
  {
  
  #ifndef OMITBAD
  
  /* bad function declaration */
  void badSink(vector<wchar_t *> dataVector);
  
  void bad()
  {
      wchar_t * data;
      vector<wchar_t *> dataVector;
      wchar_t dataBadBuffer[50];
      wchar_t dataGoodBuffer[100];
      /* FLAW: Set a pointer to a "small" buffer. This buffer will be used in the sinks as a destination
       * buffer in various memory copying functions using a "large" source buffer. */
      data = dataBadBuffer;
      data[0] = L'\0'; /* null terminate */
      /* Put data in a vector */
      dataVector.insert(dataVector.end(), 1, data);
      dataVector.insert(dataVector.end(), 1, data);
      dataVector.insert(dataVector.end(), 1, data);
      badSink(dataVector);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* good function declarations */
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSink(vector<wchar_t *> dataVector);
  
  static void goodG2B()
  {
      wchar_t * data;
      vector<wchar_t *> dataVector;
      wchar_t dataBadBuffer[50];
      wchar_t dataGoodBuffer[100];
      /* FIX: Set a pointer to a "large" buffer, thus avoiding buffer overflows in the sinks. */
      data = dataGoodBuffer;
      data[0] = L'\0'; /* null terminate */
      /* Put data in a vector */
      dataVector.insert(dataVector.end(), 1, data);
      dataVector.insert(dataVector.end(), 1, data);
      dataVector.insert(dataVector.end(), 1, data);
      goodG2BSink(dataVector);
  }
  
  void good()
  {
      goodG2B();
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  
  /* Below is the main(). It is only used when building this testcase on
   * its own for testing or for building a binary to use in testing binary
   * analysis tools. It is not used when compiling all the testcases as one
   * application, which is how source code analysis tools are tested.
   */
  
  #ifdef INCLUDEMAIN
  
  using namespace CWE121_Stack_Based_Buffer_Overflow__CWE805_wchar_t_declare_loop_72; /* so that we can use good and bad easily */
  
  int main(int argc, char * argv[])
  {
      /* seed randomness */
      srand( (unsigned)time(NULL) );
  #ifndef OMITGOOD
      printLine("Calling good()...");
      good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  