text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE124_Buffer_Underwrite__CWE839_connect_socket_84_goodG2B.cpp
  Label Definition File: CWE124_Buffer_Underwrite__CWE839.label.xml
  Template File: sources-sinks-84_goodG2B.tmpl.cpp
  */
  /*
   * @description
   * CWE: 124 Buffer Underwrite
   * BadSource: connect_socket Read data using a connect socket (client 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 the lower bound
   * 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 "CWE124_Buffer_Underwrite__CWE839_connect_socket_84.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 /* NOT _WIN32 */
  #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 IP_ADDRESS "127.0.0.1"
  #define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)
  
  namespace CWE124_Buffer_Underwrite__CWE839_connect_socket_84
  {
  CWE124_Buffer_Underwrite__CWE839_connect_socket_84_goodG2B::CWE124_Buffer_Underwrite__CWE839_connect_socket_84_goodG2B(int dataCopy)
  {
      data = dataCopy;
      /* FIX: Use a value greater than 0, but less than 10 to avoid attempting to
      * access an index of the array in the sink that is out-of-bounds */
      data = 7;
  }
  
  CWE124_Buffer_Underwrite__CWE839_connect_socket_84_goodG2B::~CWE124_Buffer_Underwrite__CWE839_connect_socket_84_goodG2B()
  {
      {
          int i;
          int buffer[10] = { 0 };
          /* POTENTIAL FLAW: Attempt to access a negative index of the array
          * This code does not check to see if the array index is negative */
          if (data < 10)
          {
              buffer[data] = 1;
              /* Print the array values */
              for(i = 0; i < 10; i++)
              {
                  printIntLine(buffer[i]);
              }
          }
          else
          {
              printLine("ERROR: Array index is negative.");
          }
      }
  }
  }
  #endif /* OMITGOOD */
  