text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE197_Numeric_Truncation_Error__int_listen_socket_to_short_61a.c
  Label Definition File: CWE197_Numeric_Truncation_Error__int.label.xml
  Template File: sources-sink-61a.tmpl.c
  */
  /*
   * @description
   * CWE: 197 Numeric Truncation Error
   * BadSource: listen_socket Read data using a listen socket (server side)
   * GoodSource: Less than CHAR_MAX
   * Sinks: to_short
   *    BadSink : Convert data to a short
   * Flow Variant: 61 Data flow: data returned 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
  
  /* bad function declaration */
  int CWE197_Numeric_Truncation_Error__int_listen_socket_to_short_61b_bad_source(int data);
  
  void CWE197_Numeric_Truncation_Error__int_listen_socket_to_short_61_bad()
  {
      int data;
      /* Initialize data */
      data = -1;
      data = CWE197_Numeric_Truncation_Error__int_listen_socket_to_short_61b_bad_source(data);
      {
          /* POTENTIAL FLAW: Convert data to a short, possibly causing a truncation error */
          short s = (short)data;
          printf("%hd\n", s);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  int CWE197_Numeric_Truncation_Error__int_listen_socket_to_short_61b_goodG2B_source(int data);
  
  static void goodG2B()
  {
      int data;
      /* Initialize data */
      data = -1;
      data = CWE197_Numeric_Truncation_Error__int_listen_socket_to_short_61b_goodG2B_source(data);
      {
          /* POTENTIAL FLAW: Convert data to a short, possibly causing a truncation error */
          short s = (short)data;
          printf("%hd\n", s);
      }
  }
  
  void CWE197_Numeric_Truncation_Error__int_listen_socket_to_short_61_good()
  {
      goodG2B();
  }
  
  #endif /* OMITGOOD */
  
  /* Below is the main(). It is only used when building this testcase on
     its own for testing or for building a binary to use in testing binary
     analysis tools. It is not used when compiling all the testcases as one
     application, which is how source code analysis tools are tested. */
  
  #ifdef INCLUDEMAIN
  
  int main(int argc, char * argv[])
  {
      /* seed randomness */
      srand( (unsigned)time(NULL) );
  #ifndef OMITGOOD
      printLine("Calling good()...");
      CWE197_Numeric_Truncation_Error__int_listen_socket_to_short_61_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE197_Numeric_Truncation_Error__int_listen_socket_to_short_61_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  