text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE127_Buffer_Underread__CWE839_listen_socket_65b.c
  Label Definition File: CWE127_Buffer_Underread__CWE839.label.xml
  Template File: sources-sinks-65b.tmpl.c
  */
  /*
   * @description
   * CWE: 127 Buffer Underread
   * BadSource: listen_socket Read data using a listen socket (server side)
   * GoodSource: Non-negative but less than 10
   * Sinks:
   *    GoodSink: Ensure the array index is valid
   *    BadSink : Improperly check the array index by not checking to see if the value is negative
   * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer
   *
   * */
  
  #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
  #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)
  
  #ifndef OMITBAD
  
  void CWE127_Buffer_Underread__CWE839_listen_socket_65b_badSink(int data)
  {
      {
          int buffer[10] = { 0 };
          /* POTENTIAL FLAW: Attempt to access a negative index of the array
           * This check does not check to see if the array index is negative */
          if (data < 10)
          {
              printIntLine(buffer[data]);
          }
          else
          {
              printLine("ERROR: Array index is too big.");
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE127_Buffer_Underread__CWE839_listen_socket_65b_goodG2BSink(int data)
  {
      {
          int buffer[10] = { 0 };
          /* POTENTIAL FLAW: Attempt to access a negative index of the array
           * This check does not check to see if the array index is negative */
          if (data < 10)
          {
              printIntLine(buffer[data]);
          }
          else
          {
              printLine("ERROR: Array index is too big.");
          }
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void CWE127_Buffer_Underread__CWE839_listen_socket_65b_goodB2GSink(int data)
  {
      {
          int buffer[10] = { 0 };
          /* FIX: Properly validate the array index and prevent a buffer underread */
          if (data >= 0 && data < (10))
          {
              printIntLine(buffer[data]);
          }
          else
          {
              printLine("ERROR: Array index is out-of-bounds");
          }
      }
  }
  
  #endif /* OMITGOOD */
  