text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE680_Integer_Overflow_To_Buffer_Overflow__malloc_listen_socket_64b.c
  Label Definition File: CWE680_Integer_Overflow_To_Buffer_Overflow__malloc.label.xml
  Template File: sources-sink-64b.tmpl.c
  */
  /*
   * @description
   * CWE: 680 Integer Overflow to Buffer Overflow
   * BadSource: listen_socket Read data using a listen socket (server side)
   * GoodSource: Small number greater than zero that will not cause an integer overflow in the sink
   * Sinks:
   *    BadSink : Attempt to allocate array using length value from source
   * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  #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
  # 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 sizeof(data)*sizeof(data)
  
  #ifndef OMITBAD
  
  void CWE680_Integer_Overflow_To_Buffer_Overflow__malloc_listen_socket_64b_bad_sink(void * void_data_ptr)
  {
      /* cast void pointer to a pointer of the appropriate type */
      int * data_ptr = (int *)void_data_ptr;
      /* dereference data_ptr into data */
      int data = (*data_ptr);
      {
          size_t i;
          int *b;
          /* POTENTIAL FLAW: if data * sizeof(int) > SIZE_MAX, overflows to a small value
               so that the for loop doing the initialization causes a buffer overflow */
          b = (int*)malloc(data * sizeof(int));
          for (i = 0; i < (size_t)data; i++)
          {
              b[i] = 0; /* Potentially writes beyond the boundary of b */
          }
          printIntLine(b[0]);
          free(b);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE680_Integer_Overflow_To_Buffer_Overflow__malloc_listen_socket_64b_goodG2B_sink(void * void_data_ptr)
  {
      /* cast void pointer to a pointer of the appropriate type */
      int * data_ptr = (int *)void_data_ptr;
      /* dereference data_ptr into data */
      int data = (*data_ptr);
      {
          size_t i;
          int *b;
          /* POTENTIAL FLAW: if data * sizeof(int) > SIZE_MAX, overflows to a small value
               so that the for loop doing the initialization causes a buffer overflow */
          b = (int*)malloc(data * sizeof(int));
          for (i = 0; i < (size_t)data; i++)
          {
              b[i] = 0; /* Potentially writes beyond the boundary of b */
          }
          printIntLine(b[0]);
          free(b);
      }
  }
  
  #endif /* OMITGOOD */
  