text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_snprintf_64b.cpp
  Label Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml
  Template File: sources-sink-64b.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
   * Sinks: swprintf
   *    BadSink : Copy string to data using swprintf
   * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #ifdef _WIN32
  #define SNPRINTF _snwprintf
  #else
  #define SNPRINTF swprintf
  #endif
  
  namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_snprintf_64
  {
  
  #ifndef OMITBAD
  
  void badSink(void * dataVoidPtr)
  {
      /* cast void pointer to a pointer of the appropriate type */
      wchar_t * * dataPtr = (wchar_t * *)dataVoidPtr;
      /* dereference dataPtr into data */
      wchar_t * data = (*dataPtr);
      {
          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 source is larger than data */
          SNPRINTF(data, 100, L"%s", source);
          printWLine(data);
          delete [] data;
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSink(void * dataVoidPtr)
  {
      /* cast void pointer to a pointer of the appropriate type */
      wchar_t * * dataPtr = (wchar_t * *)dataVoidPtr;
      /* dereference dataPtr into data */
      wchar_t * data = (*dataPtr);
      {
          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 source is larger than data */
          SNPRINTF(data, 100, L"%s", source);
          printWLine(data);
          delete [] data;
      }
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  