text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE122_Heap_Based_Buffer_Overflow__cpp_dest_wchar_t_ncpy_03.cpp
  Label Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_dest.string.label.xml
  Template File: sources-sink-03.tmpl.cpp
  */
  /*
   * @description
   * CWE: 122 Heap Based Buffer Overflow
   * BadSource:  Allocate using new[] and set data pointer to a small buffer
   * GoodSource: Allocate using new[] and set data pointer to a large buffer
   * Sink: ncpy
   *    BadSink : Copy string to data using wcsncpy
   * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  namespace CWE122_Heap_Based_Buffer_Overflow__cpp_dest_wchar_t_ncpy_03
  {
  
  #ifndef OMITBAD
  
  void bad()
  {
      wchar_t * data;
      data = NULL;
      if(5==5)
      {
          /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */
          data = new wchar_t[50];
          data[0] = L'\0'; /* null terminate */
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */
          data = new wchar_t[100];
          data[0] = L'\0'; /* null terminate */
      }
      {
          wchar_t src[100];
          wmemset(src, L'C', 100-1); /* fill with L'C's */
          src[100-1] = L'\0'; /* null terminate */
          /* POTENTIAL FLAW: Possible buffer overflow if src is larger than data */
          wcsncpy(data, src, 100);
          data[100-1] = L'\0'; /* Ensure the destination buffer is null terminated */
          printWLine(data);
          delete [] data;
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B1() - use goodsource and badsink by changing the 5==5 to 5!=5 */
  static void goodG2B1()
  {
      wchar_t * data;
      data = NULL;
      if(5!=5)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */
          data = new wchar_t[50];
          data[0] = L'\0'; /* null terminate */
      }
      else
      {
          /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */
          data = new wchar_t[100];
          data[0] = L'\0'; /* null terminate */
      }
      {
          wchar_t src[100];
          wmemset(src, L'C', 100-1); /* fill with L'C's */
          src[100-1] = L'\0'; /* null terminate */
          /* POTENTIAL FLAW: Possible buffer overflow if src is larger than data */
          wcsncpy(data, src, 100);
          data[100-1] = L'\0'; /* Ensure the destination buffer is null terminated */
          printWLine(data);
          delete [] data;
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */
  static void goodG2B2()
  {
      wchar_t * data;
      data = NULL;
      if(5==5)
      {
          /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */
          data = new wchar_t[100];
          data[0] = L'\0'; /* null terminate */
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */
          data = new wchar_t[50];
          data[0] = L'\0'; /* null terminate */
      }
      {
          wchar_t src[100];
          wmemset(src, L'C', 100-1); /* fill with L'C's */
          src[100-1] = L'\0'; /* null terminate */
          /* POTENTIAL FLAW: Possible buffer overflow if src is larger than data */
          wcsncpy(data, src, 100);
          data[100-1] = L'\0'; /* Ensure the destination buffer is null terminated */
          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_dest_wchar_t_ncpy_03; // 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
  