text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193_wchar_t_ncpy_22a.cpp
  Label Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193.label.xml
  Template File: sources-sink-22a.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: ncpy
   *    BadSink : Copy string to data using wcsncpy()
   * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  /* MAINTENANCE NOTE: The length of this string should equal the 10 */
  #define SRC_STRING L"AAAAAAAAAA"
  
  namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193_wchar_t_ncpy_22
  {
  
  #ifndef OMITBAD
  
  /* The global variable below is used to drive control flow in the source function. Since it is in
  a C++ namespace, it doesn't need a globally unique name. */
  int badGlobal = 0;
  
  wchar_t * badSource(wchar_t * data);
  
  void bad()
  {
      wchar_t * data;
      data = NULL;
      badGlobal = 1; /* true */
      data = badSource(data);
      {
          wchar_t 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 */
          wcsncpy(data, source, wcslen(source) + 1);
          printWLine(data);
          delete [] data;
      }
      ;
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* The global variables below are used to drive control flow in the source functions. Since they are in
  a C++ namespace, they don't need globally unique names. */
  int goodG2B1Global = 0;
  int goodG2B2Global = 0;
  
  /* goodG2B1() - use goodsource and badsink by setting the global variable to false instead of true */
  wchar_t * goodG2B1Source(wchar_t * data);
  
  static void goodG2B1()
  {
      wchar_t * data;
      data = NULL;
      goodG2B1Global = 0; /* false */
      data = goodG2B1Source(data);
      {
          wchar_t 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 */
          wcsncpy(data, source, wcslen(source) + 1);
          printWLine(data);
          delete [] data;
      }
      ;
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the if in the source function */
  wchar_t * goodG2B2Source(wchar_t * data);
  
  static void goodG2B2()
  {
      wchar_t * data;
      data = NULL;
      goodG2B2Global = 1; /* true */
      data = goodG2B2Source(data);
      {
          wchar_t 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 */
          wcsncpy(data, source, wcslen(source) + 1);
          printWLine(data);
          delete [] data;
      }
      ;
  }
  
  void good()
  {
      goodG2B1();
      goodG2B2();
  }
  
  #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 CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193_wchar_t_ncpy_22; /* 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
  