text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE187_Partial_Comparison__wchar_t_connect_socket_ncmp_correct_pw_17.c
  Label Definition File: CWE187_Partial_Comparison.label.xml
  Template File: sources-sinks-17.tmpl.c
  */
  /*
   * @description
   * CWE: 187 Partial Comparison
   * BadSource: connect_socket Read data using a connect socket (client side)
   * GoodSource: Provide a matching password
   * Sinks: ncmp_correct_pw
   *    GoodSink: Compare the 2 passwords correctly
   *    BadSink : use wcsncmp() to do password match, but use the length of the correct password
   * Flow Variant: 17 Control flow: for loops
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #define PASSWORD L"Password1234"
  /* PASSWORD_SZ must equal the length of PASSWORD */
  #define PASSWORD_SZ wcslen(PASSWORD)
  
  #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
  # define PATH_SZ 100
  #else /* NOT _WIN32 */
  # define INVALID_SOCKET -1
  # define SOCKET_ERROR -1
  # define CLOSE_SOCKET close
  # define SOCKET int
  # define PATH_SZ PATH_MAX
  #endif
  
  #define TCP_PORT 27015
  
  #ifndef OMITBAD
  
  void CWE187_Partial_Comparison__wchar_t_connect_socket_ncmp_correct_pw_17_bad()
  {
      int h,i,j,k;
      wchar_t * data;
      wchar_t data_buf[100] = L"";
      data = data_buf;
      for(h = 0; h < 0; h++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Use the matching string */
          data = PASSWORD;
      }
      for(i = 0; i < 1; i++)
      {
          {
  #ifdef _WIN32
              WSADATA wsa_data;
              int wsa_data_init = 0;
  #endif
              int recv_rv;
              struct sockaddr_in s_in;
              wchar_t *replace;
              SOCKET connect_socket = INVALID_SOCKET;
              size_t data_len = wcslen(data);
              do
              {
  #ifdef _WIN32
                  if (WSAStartup(MAKEWORD(2,2), &wsa_data) != NO_ERROR) break;
                  wsa_data_init = 1;
  #endif
                  connect_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                  if (connect_socket == INVALID_SOCKET) break;
                  memset(&s_in, 0, sizeof(s_in));
                  s_in.sin_family = AF_INET;
                  s_in.sin_addr.s_addr = inet_addr("127.0.0.1");
                  s_in.sin_port = htons(TCP_PORT);
                  if (connect(connect_socket, (struct sockaddr*)&s_in, sizeof(s_in)) == SOCKET_ERROR) break;
                  /* Abort on error or the connection was closed, make sure to recv one
                   * less char than is in the recv_buf in order to append a terminator */
                  recv_rv = recv(connect_socket, (char *)data+data_len, (int)(100-data_len-1), 0);
                  if (recv_rv == SOCKET_ERROR || recv_rv == 0) break;
                  /* Append null terminator */
                  data[recv_rv] = L'\0';
                  /* Eliminate CRLF */
                  replace = wcschr(data, L'\r');
                  if (replace) *replace = L'\0';
                  replace = wcschr(data, L'\n');
                  if (replace) *replace = L'\0';
              }
              while (0);
              if (connect_socket != INVALID_SOCKET) CLOSE_SOCKET(connect_socket);
  #ifdef _WIN32
              if (wsa_data_init) WSACleanup();
  #endif
          }
      }
      for(j = 0; j < 1; j++)
      {
          /* By using the strlen() of the correct password, you are able to create a partial comparison */
          /* For example if PASSWORD=PASSWORD1234, a user supplied password of PASSWORD123456 will allow access */
          /* POTENTIAL FLAW: Possibly partially compare the two passwords */
          if (wcsncmp(PASSWORD, data, wcslen(PASSWORD)) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
      for(k = 0; k < 0; k++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* Ideally, we would want to do a check to see if the passwords are of equal length */
          /* FIX: Compare the two passwords completely and correctly */
          if (wcscmp(PASSWORD, data) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G() - use badsource and goodsink by changing the conditions on the third and fourth for statements */
  static void goodB2G()
  {
      int h,i,j,k;
      wchar_t * data;
      wchar_t data_buf[100] = L"";
      data = data_buf;
      for(h = 0; h < 0; h++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Use the matching string */
          data = PASSWORD;
      }
      for(i = 0; i < 1; i++)
      {
          {
  #ifdef _WIN32
              WSADATA wsa_data;
              int wsa_data_init = 0;
  #endif
              int recv_rv;
              struct sockaddr_in s_in;
              wchar_t *replace;
              SOCKET connect_socket = INVALID_SOCKET;
              size_t data_len = wcslen(data);
              do
              {
  #ifdef _WIN32
                  if (WSAStartup(MAKEWORD(2,2), &wsa_data) != NO_ERROR) break;
                  wsa_data_init = 1;
  #endif
                  connect_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                  if (connect_socket == INVALID_SOCKET) break;
                  memset(&s_in, 0, sizeof(s_in));
                  s_in.sin_family = AF_INET;
                  s_in.sin_addr.s_addr = inet_addr("127.0.0.1");
                  s_in.sin_port = htons(TCP_PORT);
                  if (connect(connect_socket, (struct sockaddr*)&s_in, sizeof(s_in)) == SOCKET_ERROR) break;
                  /* Abort on error or the connection was closed, make sure to recv one
                   * less char than is in the recv_buf in order to append a terminator */
                  recv_rv = recv(connect_socket, (char *)data+data_len, (int)(100-data_len-1), 0);
                  if (recv_rv == SOCKET_ERROR || recv_rv == 0) break;
                  /* Append null terminator */
                  data[recv_rv] = L'\0';
                  /* Eliminate CRLF */
                  replace = wcschr(data, L'\r');
                  if (replace) *replace = L'\0';
                  replace = wcschr(data, L'\n');
                  if (replace) *replace = L'\0';
              }
              while (0);
              if (connect_socket != INVALID_SOCKET) CLOSE_SOCKET(connect_socket);
  #ifdef _WIN32
              if (wsa_data_init) WSACleanup();
  #endif
          }
      }
      for(j = 0; j < 0; j++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* By using the strlen() of the correct password, you are able to create a partial comparison */
          /* For example if PASSWORD=PASSWORD1234, a user supplied password of PASSWORD123456 will allow access */
          /* POTENTIAL FLAW: Possibly partially compare the two passwords */
          if (wcsncmp(PASSWORD, data, wcslen(PASSWORD)) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
      for(k = 0; k < 1; k++)
      {
          /* Ideally, we would want to do a check to see if the passwords are of equal length */
          /* FIX: Compare the two passwords completely and correctly */
          if (wcscmp(PASSWORD, data) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
  }
  
  /* goodG2B() - use goodsource and badsink by changing the conditions on the first and second for statements */
  static void goodG2B()
  {
      int h,i,j,k;
      wchar_t * data;
      wchar_t data_buf[100] = L"";
      data = data_buf;
      for(h = 0; h < 1; h++)
      {
          /* FIX: Use the matching string */
          data = PASSWORD;
      }
      for(i = 0; i < 0; i++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
  #ifdef _WIN32
              WSADATA wsa_data;
              int wsa_data_init = 0;
  #endif
              int recv_rv;
              struct sockaddr_in s_in;
              wchar_t *replace;
              SOCKET connect_socket = INVALID_SOCKET;
              size_t data_len = wcslen(data);
              do
              {
  #ifdef _WIN32
                  if (WSAStartup(MAKEWORD(2,2), &wsa_data) != NO_ERROR) break;
                  wsa_data_init = 1;
  #endif
                  connect_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                  if (connect_socket == INVALID_SOCKET) break;
                  memset(&s_in, 0, sizeof(s_in));
                  s_in.sin_family = AF_INET;
                  s_in.sin_addr.s_addr = inet_addr("127.0.0.1");
                  s_in.sin_port = htons(TCP_PORT);
                  if (connect(connect_socket, (struct sockaddr*)&s_in, sizeof(s_in)) == SOCKET_ERROR) break;
                  /* Abort on error or the connection was closed, make sure to recv one
                   * less char than is in the recv_buf in order to append a terminator */
                  recv_rv = recv(connect_socket, (char *)data+data_len, (int)(100-data_len-1), 0);
                  if (recv_rv == SOCKET_ERROR || recv_rv == 0) break;
                  /* Append null terminator */
                  data[recv_rv] = L'\0';
                  /* Eliminate CRLF */
                  replace = wcschr(data, L'\r');
                  if (replace) *replace = L'\0';
                  replace = wcschr(data, L'\n');
                  if (replace) *replace = L'\0';
              }
              while (0);
              if (connect_socket != INVALID_SOCKET) CLOSE_SOCKET(connect_socket);
  #ifdef _WIN32
              if (wsa_data_init) WSACleanup();
  #endif
          }
      }
      for(j = 0; j < 1; j++)
      {
          /* By using the strlen() of the correct password, you are able to create a partial comparison */
          /* For example if PASSWORD=PASSWORD1234, a user supplied password of PASSWORD123456 will allow access */
          /* POTENTIAL FLAW: Possibly partially compare the two passwords */
          if (wcsncmp(PASSWORD, data, wcslen(PASSWORD)) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
      for(k = 0; k < 0; k++)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* Ideally, we would want to do a check to see if the passwords are of equal length */
          /* FIX: Compare the two passwords completely and correctly */
          if (wcscmp(PASSWORD, data) == 0)
              printLine("Access granted");
          else
              printLine("Access denied!");
      }
  }
  
  void CWE187_Partial_Comparison__wchar_t_connect_socket_ncmp_correct_pw_17_good()
  {
      goodB2G();
      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()...");
      CWE187_Partial_Comparison__wchar_t_connect_socket_ncmp_correct_pw_17_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE187_Partial_Comparison__wchar_t_connect_socket_ncmp_correct_pw_17_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  