text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE789_Uncontrolled_Mem_Alloc__new_wchar_t_fscanf_67b.cpp
  Label Definition File: CWE789_Uncontrolled_Mem_Alloc__new.label.xml
  Template File: sources-sinks-67b.tmpl.cpp
  */
  /*
   * @description
   * CWE: 789 Uncontrolled Memory Allocation
   * BadSource: fscanf Read data from the console using fscanf()
   * GoodSource: Small number greater than zero
   * Sinks:
   *    GoodSink: Allocate memory with new [] and check the size of the memory to be allocated
   *    BadSink : Allocate memory with new [], but incorrectly check the size of the memory to be allocated
   * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #define HELLO_STRING L"hello"
  
  namespace CWE789_Uncontrolled_Mem_Alloc__new_wchar_t_fscanf_67
  {
  
  typedef struct _structType
  {
      size_t structFirst;
  } structType;
  
  #ifndef OMITBAD
  
  void badSink(structType myStruct)
  {
      size_t data = myStruct.structFirst;
      {
          wchar_t * myString;
          /* POTENTIAL FLAW: No MAXIMUM limitation for memory allocation, but ensure data is large enough
           * for the wcscpy() function to not cause a buffer overflow */
          /* INCIDENTAL FLAW: The source could cause a type overrun in data or in the memory allocation */
          if (data > wcslen(HELLO_STRING))
          {
              myString = new wchar_t[data];
              /* Copy a small string into myString */
              wcscpy(myString, HELLO_STRING);
              printWLine(myString);
              delete [] myString;
          }
          else
          {
              printLine("Input is less than the length of the source string");
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSink(structType myStruct)
  {
      size_t data = myStruct.structFirst;
      {
          wchar_t * myString;
          /* POTENTIAL FLAW: No MAXIMUM limitation for memory allocation, but ensure data is large enough
           * for the wcscpy() function to not cause a buffer overflow */
          /* INCIDENTAL FLAW: The source could cause a type overrun in data or in the memory allocation */
          if (data > wcslen(HELLO_STRING))
          {
              myString = new wchar_t[data];
              /* Copy a small string into myString */
              wcscpy(myString, HELLO_STRING);
              printWLine(myString);
              delete [] myString;
          }
          else
          {
              printLine("Input is less than the length of the source string");
          }
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void goodB2GSink(structType myStruct)
  {
      size_t data = myStruct.structFirst;
      {
          wchar_t * myString;
          /* FIX: Include a MAXIMUM limitation for memory allocation and a check to ensure data is large enough
           * for the wcscpy() function to not cause a buffer overflow */
          /* INCIDENTAL FLAW: The source could cause a type overrun in data or in the memory allocation */
          if (data > wcslen(HELLO_STRING) && data < 100)
          {
              myString = new wchar_t[data];
              /* Copy a small string into myString */
              wcscpy(myString, HELLO_STRING);
              printWLine(myString);
              delete [] myString;
          }
          else
          {
              printLine("Input is less than the length of the source string or too large");
          }
      }
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  