text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE789_Uncontrolled_Mem_Alloc__malloc_char_listen_socket_64b.c
  Label Definition File: CWE789_Uncontrolled_Mem_Alloc__malloc.label.xml
  Template File: sources-sinks-64b.tmpl.c
  */
  /*
   * @description
   * CWE: 789 Uncontrolled Memory Allocation
   * BadSource: listen_socket Read data using a listen socket (server side)
   * 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: 64 Data flow: void pointer to data passed from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #ifdef _WIN32
  #include <winsock2.h>
  #include <windows.h>
  #include <direct.h>
  #pragma comment(lib, "ws2_32") /* include ws2_32.lib when linking */
  #define CLOSE_SOCKET closesocket
  #else
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <netinet/in.h>
  #include <arpa/inet.h>
  #include <unistd.h>
  #define INVALID_SOCKET -1
  #define SOCKET_ERROR -1
  #define CLOSE_SOCKET close
  #define SOCKET int
  #endif
  
  #define TCP_PORT 27015
  #define LISTEN_BACKLOG 5
  #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)
  
  #define HELLO_STRING "hello"
  
  #ifndef OMITBAD
  
  void CWE789_Uncontrolled_Mem_Alloc__malloc_char_listen_socket_64b_badSink(void * dataVoidPtr)
  {
      /* cast void pointer to a pointer of the appropriate type */
      size_t * dataPtr = (size_t *)dataVoidPtr;
      /* dereference dataPtr into data */
      size_t data = (*dataPtr);
      {
          char * myString;
          /* POTENTIAL FLAW: No MAXIMUM limitation for memory allocation, but 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))
          {
              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");
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE789_Uncontrolled_Mem_Alloc__malloc_char_listen_socket_64b_goodG2BSink(void * dataVoidPtr)
  {
      /* cast void pointer to a pointer of the appropriate type */
      size_t * dataPtr = (size_t *)dataVoidPtr;
      /* dereference dataPtr into data */
      size_t data = (*dataPtr);
      {
          char * myString;
          /* POTENTIAL FLAW: No MAXIMUM limitation for memory allocation, but 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))
          {
              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");
          }
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void CWE789_Uncontrolled_Mem_Alloc__malloc_char_listen_socket_64b_goodB2GSink(void * dataVoidPtr)
  {
      /* cast void pointer to a pointer of the appropriate type */
      size_t * dataPtr = (size_t *)dataVoidPtr;
      /* dereference dataPtr into data */
      size_t data = (*dataPtr);
      {
          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 */
  