text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE789_Uncontrolled_Mem_Alloc__malloc_wchar_t_fgets_53d.c
  Label Definition File: CWE789_Uncontrolled_Mem_Alloc__malloc.label.xml
  Template File: sources-sinks-53d.tmpl.c
  */
  /*
   * @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 malloc() and check the size of the memory to be allocated
   *    BadSink : Allocate memory with malloc(), but incorrectly check the size of the memory to be allocated
   * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #define CHAR_ARRAY_SIZE sizeof(data)*sizeof(data)
  
  #define HELLO_STRING L"hello"
  
  #ifndef OMITBAD
  
  void CWE789_Uncontrolled_Mem_Alloc__malloc_wchar_t_fgets_53d_bad_sink(int data)
  {
      {
          wchar_t * wchar_t_string;
          /* FLAW: No MAXIMUM limitation for memory allocation and no 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 > 0)
          {
              wchar_t_string = (wchar_t *)malloc(data*sizeof(wchar_t));
              /* Copy a small string into wchar_t_string$ */
              wcscpy(wchar_t_string, HELLO_STRING);
              printWLine(wchar_t_string);
              free(wchar_t_string);
          }
          else
          {
              printLine("Input is less than 0");
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE789_Uncontrolled_Mem_Alloc__malloc_wchar_t_fgets_53d_goodG2B_sink(int data)
  {
      {
          wchar_t * wchar_t_string;
          /* FLAW: No MAXIMUM limitation for memory allocation and no 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 > 0)
          {
              wchar_t_string = (wchar_t *)malloc(data*sizeof(wchar_t));
              /* Copy a small string into wchar_t_string$ */
              wcscpy(wchar_t_string, HELLO_STRING);
              printWLine(wchar_t_string);
              free(wchar_t_string);
          }
          else
          {
              printLine("Input is less than 0");
          }
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void CWE789_Uncontrolled_Mem_Alloc__malloc_wchar_t_fgets_53d_goodB2G_sink(int data)
  {
      {
          wchar_t * wchar_t_string;
          /* 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 ((size_t)data > wcslen(HELLO_STRING) && data < 100)
          {
              wchar_t_string = (wchar_t *)malloc(data*sizeof(wchar_t));
              /* Copy a small string into wchar_t_string$ */
              wcscpy(wchar_t_string, HELLO_STRING);
              printWLine(wchar_t_string);
              free(wchar_t_string);
          }
          else
          {
              printLine("Input is less than the length of the source string or too large");
          }
      }
  }
  
  #endif /* OMITGOOD */
  