text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193_wchar_t_memmove_21.cpp
  Label Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE193.label.xml
  Template File: sources-sink-21.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: memmove
   *    BadSink : Copy string to data using memmove()
   * Flow Variant: 21 Control flow: Flow controlled by value of a static global variable. All functions contained in one file.
   *
   * */
  
  #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_memmove_21
  {
  
  #ifndef OMITBAD
  
  /* The static variable below is used to drive control flow in the source function */
  static int badStatic = 0;
  
  static wchar_t * badSource(wchar_t * data)
  {
      if(badStatic)
      {
          /* FLAW: Did not leave space for a null terminator */
          data = new wchar_t[10];
      }
      return data;
  }
  
  void bad()
  {
      wchar_t * data;
      data = NULL;
      badStatic = 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 */
          memmove(data, source, (wcslen(source) + 1) * sizeof(wchar_t));
          printWLine(data);
          delete [] data;
      }
      ;
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* The static variables below are used to drive control flow in the source functions. */
  static int goodG2B1Static = 0;
  static int goodG2B2Static = 0;
  
  /* goodG2B1() - use goodsource and badsink by setting the static variable to false instead of true */
  static wchar_t * goodG2B1Source(wchar_t * data)
  {
      if(goodG2B1Static)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          /* FIX: Allocate space for a null terminator */
          data = new wchar_t[10+1];
      }
      return data;
  }
  
  static void goodG2B1()
  {
      wchar_t * data;
      data = NULL;
      goodG2B1Static = 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 */
          memmove(data, source, (wcslen(source) + 1) * sizeof(wchar_t));
          printWLine(data);
          delete [] data;
      }
      ;
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the if in the source function */
  static wchar_t * goodG2B2Source(wchar_t * data)
  {
      if(goodG2B2Static)
      {
          /* FIX: Allocate space for a null terminator */
          data = new wchar_t[10+1];
      }
      return data;
  }
  
  static void goodG2B2()
  {
      wchar_t * data;
      data = NULL;
      goodG2B2Static = 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 */
          memmove(data, source, (wcslen(source) + 1) * sizeof(wchar_t));
          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_memmove_21; /* 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
  