text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE789_Uncontrolled_Mem_Alloc__malloc_char_rand_84_goodB2G.cpp
  Label Definition File: CWE789_Uncontrolled_Mem_Alloc__malloc.label.xml
  Template File: sources-sinks-84_goodB2G.tmpl.cpp
  */
  /*
   * @description
   * CWE: 789 Uncontrolled Memory Allocation
   * BadSource: rand Set data to result of rand(), which may be zero
   * 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: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use
   *
   * */
  #ifndef OMITGOOD
  
  #include "std_testcase.h"
  #include "CWE789_Uncontrolled_Mem_Alloc__malloc_char_rand_84.h"
  
  #define HELLO_STRING "hello"
  
  namespace CWE789_Uncontrolled_Mem_Alloc__malloc_char_rand_84
  {
  CWE789_Uncontrolled_Mem_Alloc__malloc_char_rand_84_goodB2G::CWE789_Uncontrolled_Mem_Alloc__malloc_char_rand_84_goodB2G(size_t dataCopy)
  {
      data = dataCopy;
      /* POTENTIAL FLAW: Set data to a random value */
      data = rand();
  }
  
  CWE789_Uncontrolled_Mem_Alloc__malloc_char_rand_84_goodB2G::~CWE789_Uncontrolled_Mem_Alloc__malloc_char_rand_84_goodB2G()
  {
      {
          char * myString;
          /* FIX: Include a MAXIMUM limitation for memory allocation and a check to ensure data is large enough
           * for the strcpy() 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 > strlen(HELLO_STRING) && data < 100)
          {
              myString = (char *)malloc(data*sizeof(char));
              /* Copy a small string into myString */
              strcpy(myString, HELLO_STRING);
              printLine(myString);
              free(myString);
          }
          else
          {
              printLine("Input is less than the length of the source string or too large");
          }
      }
  }
  }
  #endif /* OMITGOOD */
  