text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE191_Integer_Underflow__int_listen_socket_multiply_67b.c
  Label Definition File: CWE191_Integer_Underflow__int.label.xml
  Template File: sources-sinks-67b.tmpl.c
  */
  /*
   * @description
   * CWE: 191 Integer Underflow
   * BadSource: listen_socket Read data using a listen socket (server side)
   * GoodSource: Set data to a small, non-zero number (negative two)
   * Sinks: multiply
   *    GoodSink: Ensure there will not be an underflow before multiplying data by 2
   *    BadSink : If data is negative, multiply by 2, which can cause an underflow
   * Flow Variant: 67 Data flow: data passed in a struct 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
  #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)
  
  typedef struct _CWE191_Integer_Underflow__int_listen_socket_multiply_67_structType
  {
      int structFirst;
  } CWE191_Integer_Underflow__int_listen_socket_multiply_67_structType;
  
  #ifndef OMITBAD
  
  void CWE191_Integer_Underflow__int_listen_socket_multiply_67b_badSink(CWE191_Integer_Underflow__int_listen_socket_multiply_67_structType myStruct)
  {
      int data = myStruct.structFirst;
      if(data < 0) /* ensure we won't have an overflow */
      {
          /* POTENTIAL FLAW: if (data * 2) < INT_MIN, this will underflow */
          int result = data * 2;
          printIntLine(result);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void CWE191_Integer_Underflow__int_listen_socket_multiply_67b_goodG2BSink(CWE191_Integer_Underflow__int_listen_socket_multiply_67_structType myStruct)
  {
      int data = myStruct.structFirst;
      if(data < 0) /* ensure we won't have an overflow */
      {
          /* POTENTIAL FLAW: if (data * 2) < INT_MIN, this will underflow */
          int result = data * 2;
          printIntLine(result);
      }
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  void CWE191_Integer_Underflow__int_listen_socket_multiply_67b_goodB2GSink(CWE191_Integer_Underflow__int_listen_socket_multiply_67_structType myStruct)
  {
      int data = myStruct.structFirst;
      if(data < 0) /* ensure we won't have an overflow */
      {
          /* FIX: Add a check to prevent an underflow from occurring */
          if (data > (INT_MIN/2))
          {
              int result = data * 2;
              printIntLine(result);
          }
          else
          {
              printLine("data value is too small to perform multiplication.");
          }
      }
  }
  
  #endif /* OMITGOOD */
  