text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE789_Uncontrolled_Mem_Alloc__new_wchar_t_fgets_61a.cpp
  Label Definition File: CWE789_Uncontrolled_Mem_Alloc__new.label.xml
  Template File: sources-sinks-61a.tmpl.cpp
  */
  /*
   * @description
   * CWE: 789 Uncontrolled Memory Allocation
   * BadSource: fgets Read data from the console using fgets()
   * 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: 61 Data flow: data returned from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)
  
  #define HELLO_STRING L"hello"
  
  namespace CWE789_Uncontrolled_Mem_Alloc__new_wchar_t_fgets_61
  {
  
  #ifndef OMITBAD
  
  /* bad function declaration */
  size_t badSource(size_t data);
  
  void bad()
  {
      size_t data;
      /* Initialize data */
      data = 0;
      data = badSource(data);
      {
          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 */
  size_t goodG2BSource(size_t data);
  
  static void goodG2B()
  {
      size_t data;
      /* Initialize data */
      data = 0;
      data = goodG2BSource(data);
      {
          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 */
  size_t goodB2GSource(size_t data);
  
  static void goodB2G()
  {
      size_t data;
      /* Initialize data */
      data = 0;
      data = goodB2GSource(data);
      {
          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");
          }
      }
  }
  
  void good()
  {
      goodG2B();
      goodB2G();
  }
  
  #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 CWE789_Uncontrolled_Mem_Alloc__new_wchar_t_fgets_61; /* 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
  