[PATCH] support WIN64 with several fixes.

"星野 喬" <[email protected]>
Newsgroups gmane.comp.web.fastcgi.devel
Message-ID <125912505451300001034@WINSERVER>
Hello,

I posted x64 patch for 2.4.0 several months ago
and now recreated it for current snapshot (2.4.1-SNAP-0910052249).
For convinience, it consists five files attached to thie email.


Each patch description:

1-align-x64.patch
* Fixed functions to support both 32/64 bit build.
  AlignInt8(), AlignPtr8().
  This modification is essential for Windows 64bit build.

2-socklen.patch
* Fixed socklen_t detection failure problem in configure script 
  for Linux.

3-gcc44.patch
* Fixed for gcc-4.4 build on Linux.

4-vc9.patch
* Converted Win32/*.{dsw,dsp} files to Win32/*.{sln,vcproj} files
  (Visual Studio 2008 solution/project files) and
  added platform "x64" for 64bit build.
* Removed almost all build warnings on Visual Studio 2008.
  with PreprocessorDefinitions:
  _CRT_SECURE_NO_WARNINGS and _SCL_SECURE_NO_WARNINGS.

5-build-warnings.patch
* Fixed type size problem (pointer, size_t, etc.)
  to support both 32/64 bit build
  using intptr_t, uintptr_t, etc.
* Fixed to check range of each numeric variable with ASSERT()
  before using type cast for demotion(narrowing).
* Replaced several standard functions to recommended ones.
  getpid() --> _getpid(), and so on.
* Removed almost all build warnings on Linux.


Current status:

* Tested the patches on Windows Server 2008 SP1 (64bit).
  with apache httpd 2.2.14 (32bit) and mod_fastcgi-SNAP-0910052141.
  Both 32/64 bit sample executables are working.
  We could not build and test two samples: threaded and log-dump.
* Tested the patches on CentOS 5.4 (64bit) with gcc-4.3.4.
  For 32bit build, we used gcc -m32 option.
  Both 32/64 bit sample executables are working except log-dump.
  We could not find out the usage of log-dump
  so that we could not test it.


How to build:

1. For Windows

1-1. Extract fastcgi-2.4.1-SNAP-0910052249.tar.gz.

> tar xzf fastcgi-2.4.1-SNAP-0910052249.tar.gz

1-2. Apply patch files.

> cd fastcgi-2.4.1-SNAP-0910052249
> patch -p1 < ../1-align-x64.patch
> patch -p1 < ../2-socklen.patch
> patch -p1 < ../3-gcc44.patch
> patch -p1 < ../4-vc9.patch
> patch -p1 < ../5-build-warnings.patch

1-3. Open fastcgi-2.4.0/Win32/FastCGI.sln with Visual Studio 2008 and build.

Debug and Release build on Win32 and x64 are supported.

Build with 'nmake' is not supported, since we could not find out
suitable project converter and we can use 'devenv' command line instead.

2. For Linux

2-1. Do the same process as (1-1).
2-2. Do the same process as (1-2).

2-3. Remake configure script.

> libtoolize -c -f
> aclocal
> autoheader
> automake -a -c -f
> autoconf

2-4. Configure and make

> ./configure
> make
> make install

If you need, NDEBUG preprocessor definition should be specified
to eliminate ASSERT check for release build.


Sincerely,
Hoshino

---
HOSHINO Takashi <[email protected]>
Cybozu Labs, Inc.

_______________________________________________
FastCGI-developers mailing list
FastCGI-developers-xGejAJT2w6xVgU18Zptdi0EOCMrvLtNR@public.gmane.org
http://mailman.pins.net/mailman/listinfo.cgi/fastcgi-developers
1-align-x64.patch (application/octet-stream, 1 KB)
diff --git a/libfcgi/fcgiapp.c b/libfcgi/fcgiapp.c
index d7bc3c8..4167aa4 100644
--- a/libfcgi/fcgiapp.c
+++ b/libfcgi/fcgiapp.c
@@ -25,6 +25,10 @@ static const char rcsid[] = "$Id: fcgiapp.c,v 1.35 2003/06/22 00:16:43 robs Exp
 #include <string.h>
 #include <sys/types.h>
 
+#ifndef _WIN32
+#include <stdint.h>
+#endif
+
 #include "fcgi_config.h"
 
 #ifdef HAVE_SYS_SOCKET_H
@@ -1269,8 +1273,8 @@ static FCGI_UnknownTypeBody MakeUnknownTypeBody(
  *
  *----------------------------------------------------------------------
  */
-static int AlignInt8(unsigned n) {
-    return (n + 7) & (UINT_MAX - 7);
+static intptr_t AlignInt8(intptr_t n) {
+    return (n + 7) & ~7;
 }
 
 /*
@@ -1284,9 +1288,7 @@ static int AlignInt8(unsigned n) {
  *----------------------------------------------------------------------
  */
 static unsigned char *AlignPtr8(unsigned char *p) {
-    unsigned long u = (unsigned long) p;
-    u = ((u + 7) & (ULONG_MAX - 7)) - u;
-    return p + u;
+    return (unsigned char *)((((uintptr_t)p) + 7) & ~7);
 }
2-socklen.patch (application/octet-stream, 693 B)
diff --git a/acinclude.m4 b/acinclude.m4
index c1648bd..e351d93 100644
--- a/acinclude.m4
+++ b/acinclude.m4
@@ -27,14 +27,12 @@ AC_DEFUN([FCGI_COMMON_CHECKS], [
 		   [Define if there's a fileno() prototype in stdio.h])],
 	    AC_MSG_RESULT([no]))
 
-    if test "$HAVE_SYS_SOCKET_H"; then
 	AC_MSG_CHECKING([for socklen_t in sys/socket.h])
 	AC_EGREP_HEADER([socklen_t], [sys/socket.h],
 	    [AC_MSG_RESULT([yes])
 	     AC_DEFINE([HAVE_SOCKLEN], [1],
 			       [Define if the socklen_t typedef is in sys/socket.h])],
 	   AC_MSG_RESULT([no]))
-    fi
 
     #--------------------------------------------------------------------
     #  Do we need cross-process locking on this platform?
3-gcc44.patch (application/octet-stream, 259 B)
diff --git a/libfcgi/fcgio.cpp b/libfcgi/fcgio.cpp
index 5a54c11..da8cbbf 100644
--- a/libfcgi/fcgio.cpp
+++ b/libfcgi/fcgio.cpp
@@ -22,6 +22,7 @@
 #define DLLAPI  __declspec(dllexport)
 #endif
 
+#include <stdio.h>
 #include <limits.h>
 #include "fcgio.h"
4-vc9.patch (application/octet-stream, 126.9 KB) - not displayed
5-build-warnings.patch (application/octet-stream, 37.1 KB)
diff --git a/cgi-fcgi/cgi-fcgi.c b/cgi-fcgi/cgi-fcgi.c
index c8dfdbe..2cbca0b 100644
--- a/cgi-fcgi/cgi-fcgi.c
+++ b/cgi-fcgi/cgi-fcgi.c
@@ -36,6 +36,8 @@ static const char rcsid[] = "$Id: cgi-fcgi.c,v 1.20 2009/10/06 01:31:59 robs Exp
 #define environ (*_NSGetEnviron())
 #else
 extern char **environ;
+#include <stdint.h>
+#include <limits.h>
 #endif
 
 #ifdef HAVE_SYS_PARAM_H
@@ -55,6 +57,11 @@ extern char **environ;
 #include "fastcgi.h"
 #include "fcgios.h"
 
+#ifdef _WIN32
+#define ACCESS _access
+#else
+#define ACCESS access
+#endif
 
 static int wsReadPending = 0;
 static int fcgiReadPending = 0;
@@ -89,8 +96,11 @@ typedef struct {
 static int GetPtr(char **ptr, int n, Buffer *pBuf)
 {
     int result;
+    intptr_t ptrDiff;
     *ptr = pBuf->next;
-    result = min(n, pBuf->stop - pBuf->next);
+    ptrDiff = pBuf->stop - pBuf->next;
+    ASSERT(0 <= ptrDiff && ptrDiff <= INT_MAX);
+    result = min(n, (int)ptrDiff);
     pBuf->next += result;
     return result;
 }
@@ -223,7 +233,7 @@ static void AppServerReadHandler(ClientData dc, int bytesRead)
     /* Touch unused parameters to avoid warnings */
     dc = NULL;
 
-    assert(fcgiReadPending == TRUE);
+    ASSERT(fcgiReadPending == TRUE);
     fcgiReadPending = FALSE;
     count = bytesRead;
 
@@ -253,7 +263,7 @@ static void AppServerReadHandler(ClientData dc, int bytesRead)
              * First priority is to complete the header.
              */
             count = GetPtr(&ptr, sizeof(header) - headerLen, &fromAS);
-            assert(count > 0);
+            ASSERT(count > 0);
             memcpy(&header + headerLen, ptr, count);
             headerLen += count;
             if(headerLen < sizeof(header)) {
@@ -381,9 +391,9 @@ static void WebServerReadHandler(ClientData dc, int bytesRead)
     /* Touch unused parameters to avoid warnings */
     dc = NULL;
 
-    assert(fromWS.next == fromWS.stop);
-    assert(fromWS.next == &fromWS.buff[0]);
-    assert(wsReadPending == TRUE);
+    ASSERT(fromWS.next == fromWS.stop);
+    ASSERT(fromWS.next == &fromWS.buff[0]);
+    ASSERT(wsReadPending == TRUE);
     wsReadPending = FALSE;
 
     if(bytesRead < 0) {
@@ -415,13 +425,13 @@ static void WebServerReadHandler(ClientData dc, int bytesRead)
 
 static void AppServerWriteHandler(ClientData dc, int bytesWritten)
 {
-    int length = fromWS.stop - fromWS.next;
+    intptr_t length = fromWS.stop - fromWS.next;
 
     /* Touch unused parameters to avoid warnings */
     dc = NULL;
 
-    assert(length > 0);
-    assert(fcgiWritePending == TRUE);
+    ASSERT(length > 0);
+    ASSERT(fcgiWritePending == TRUE);
 
     fcgiWritePending = FALSE;
     if(bytesWritten < 0) {
@@ -452,16 +462,18 @@ static void AppServerWriteHandler(ClientData dc, int bytesWritten)
  */
 static void ScheduleIo(void)
 {
-    int length;
+    intptr_t length;
 
     /*
      * Move data between standard in and the FastCGI connection.
      */
+    length = fromWS.stop - fromWS.next;
     if(!fcgiWritePending && appServerSock != -1 &&
-       ((length = fromWS.stop - fromWS.next) != 0)) {
-	if(OS_AsyncWrite(appServerSock, 0, fromWS.next, length,
+       (length != 0)) {
+        ASSERT(0 <= length && length <= INT_MAX);
+        if(OS_AsyncWrite(appServerSock, 0, fromWS.next, (int)length,
 			 AppServerWriteHandler,
-			 (ClientData)appServerSock) == -1) {
+                         (ClientData)(intptr_t)appServerSock) == -1) {
 	    FCGIexit(OS_Errno);
 	} else {
 	    fcgiWritePending = TRUE;
@@ -477,7 +489,7 @@ static void ScheduleIo(void)
 
 	if(OS_AsyncRead(appServerSock, 0, fromAS.next, BUFFLEN,
 			AppServerReadHandler,
-			(ClientData)appServerSock) == -1) {
+                        (ClientData)(intptr_t)appServerSock) == -1) {
 	    FCGIexit(OS_Errno);
 	} else {
 	    fcgiReadPending = TRUE;
@@ -521,7 +533,7 @@ static void FCGI_Start(char *bindPath, char *appPath, int nServers)
         exit(OS_Errno);
     }
 
-    if(access(appPath, X_OK) == -1) {
+    if(ACCESS(appPath, X_OK) == -1) {
 	fprintf(stderr, "%s is not executable\n", appPath);
 	exit(1);
     }
@@ -559,6 +571,7 @@ static void FCGIUtil_BuildNameValueHeader(
         unsigned char *headerBuffPtr,
         int *headerLenPtr) {
     unsigned char *startHeaderBuffPtr = headerBuffPtr;
+    intptr_t ptrDiff;
 
     ASSERT(nameLen >= 0);
     if (nameLen < 0x80) {
@@ -578,7 +591,9 @@ static void FCGIUtil_BuildNameValueHeader(
         *headerBuffPtr++ = (unsigned char) (valueLen >> 8);
         *headerBuffPtr++ = (unsigned char) valueLen;
     }
-    *headerLenPtr = headerBuffPtr - startHeaderBuffPtr;
+    ptrDiff = headerBuffPtr - startHeaderBuffPtr;
+    ASSERT(0 <= ptrDiff && ptrDiff <= INT_MAX);
+    *headerLenPtr = (int)ptrDiff;
 }
 
 
@@ -643,7 +658,7 @@ static int ParseArgs(int argc, char *argv[],
 			}
 			if((av[ac] = (char *) malloc(strlen(tp1) + 1)) == NULL) {
 			    fprintf(stderr, "Cannot allocate %d bytes\n",
-				    strlen(tp1)+1);
+				    (int) strlen(tp1)+1);
 			    exit(-1);
 			}
 			strcpy(av[ac++], tp1);
@@ -662,7 +677,7 @@ static int ParseArgs(int argc, char *argv[],
 	    } else if (!strcmp(argv[i], "-jitcgi")) {
 	        DebugBreak();
 	    } else if (!strcmp(argv[i], "-dbgfcgi")) {
-	        putenv("DEBUG_FCGI=TRUE");
+                _putenv("DEBUG_FCGI=TRUE");
 #endif
 	    } else if(!strcmp(argv[i], "-start")) {
 		*doBindPtr = FALSE;
@@ -743,11 +758,13 @@ int main(int argc, char **argv)
     FCGX_Stream *paramsStream;
     int numFDs;
     unsigned char headerBuff[8];
-    int headerLen, valueLen;
+    int headerLen;
+    intptr_t valueLen;
     char *equalPtr;
     FCGI_BeginRequestRecord beginRecord;
     int	doBind, doStart, nServers;
     char appPath[MAXPATHLEN], bindPath[MAXPATHLEN];
+    intptr_t ptrDiff;
 
     if(ParseArgs(argc, argv, &doBind, &doStart,
 		   (char *) &bindPath, (char *) &appPath, &nServers)) {
@@ -822,14 +839,17 @@ int main(int argc, char **argv)
             exit(1000);
         }
         valueLen = strlen(equalPtr + 1);
+        ASSERT(0 <= valueLen && valueLen <= INT_MAX);
+        ptrDiff = equalPtr - *envp;
+        ASSERT(0 <= ptrDiff && ptrDiff < INT_MAX);
         FCGIUtil_BuildNameValueHeader(
-                equalPtr - *envp,
-                valueLen,
+            (int)ptrDiff,
+            (int)valueLen,
                 &headerBuff[0],
                 &headerLen);
         if(FCGX_PutStr((char *) &headerBuff[0], headerLen, paramsStream) < 0
-                || FCGX_PutStr(*envp, equalPtr - *envp, paramsStream) < 0
-                || FCGX_PutStr(equalPtr + 1, valueLen, paramsStream) < 0) {
+           || FCGX_PutStr(*envp, (int)ptrDiff, paramsStream) < 0
+           || FCGX_PutStr(equalPtr + 1, (int)valueLen, paramsStream) < 0) {
             exit(FCGX_GetError(paramsStream));
         }
     }
diff --git a/examples/echo-cpp.cpp b/examples/echo-cpp.cpp
index e6fd8f9..7006898 100644
--- a/examples/echo-cpp.cpp
+++ b/examples/echo-cpp.cpp
@@ -40,6 +40,12 @@ extern char ** environ;
 #include "fcgio.h"
 #include "fcgi_config.h"  // HAVE_IOSTREAM_WITHASSIGN_STREAMBUF
 
+#ifdef _WIN32
+#define GETPID _getpid
+#else
+#define GETPID getpid
+#endif
+
 using namespace std;
 
 // Maximum number of bytes allowed to be read from stdin
@@ -55,10 +61,10 @@ static void penv(const char * const * envp)
     cout << "</PRE>\n";
 }
 
-static long gstdin(FCGX_Request * request, char ** content)
+static uintptr_t gstdin(FCGX_Request * request, char ** content)
 {
     char * clenstr = FCGX_GetParam("CONTENT_LENGTH", request->envp);
-    unsigned long clen = STDIN_MAX;
+    uintptr_t clen = STDIN_MAX;
 
     if (clenstr)
     {
@@ -99,7 +105,7 @@ static long gstdin(FCGX_Request * request, char ** content)
 int main (void)
 {
     int count = 0;
-    long pid = getpid();
+    long pid = GETPID();
 
     streambuf * cin_streambuf  = cin.rdbuf();
     streambuf * cout_streambuf = cout.rdbuf();
@@ -133,7 +139,7 @@ int main (void)
         // many http clients (browsers) don't support it (so
         // the connection deadlocks until a timeout expires!).
         char * content;
-        unsigned long clen = gstdin(&request, &content);
+        uintptr_t clen = gstdin(&request, &content);
 
         cout << "Content-type: text/html\r\n"
                 "\r\n"
diff --git a/examples/echo-x.c b/examples/echo-x.c
index 107e56f..da1fcd3 100644
--- a/examples/echo-x.c
+++ b/examples/echo-x.c
@@ -28,6 +28,12 @@ static const char rcsid[] = "$Id: echo-x.c,v 1.1 2001/06/19 15:06:17 robs Exp $"
 extern char **environ;
 #endif
 
+#ifdef _WIN32
+#define GETPID _getpid
+#else
+#define GETPID getpid
+#endif
+
 #include "fcgiapp.h"
 
 static void PrintEnv(FCGX_Stream *out, char *label, char **envp)
@@ -54,7 +60,7 @@ int main ()
            "\r\n"
            "<title>FastCGI echo (fcgiapp version)</title>"
            "<h1>FastCGI echo (fcgiapp version)</h1>\n"
-           "Request number %d,  Process ID: %d<p>\n", ++count, getpid());
+                     "Request number %d,  Process ID: %d<p>\n", ++count, GETPID());
 
         if (contentLength != NULL)
             len = strtol(contentLength, NULL, 10);
diff --git a/examples/echo.c b/examples/echo.c
index 6e71b1f..daa2528 100644
--- a/examples/echo.c
+++ b/examples/echo.c
@@ -28,6 +28,12 @@ static const char rcsid[] = "$Id: echo.c,v 1.5 1999/07/28 00:29:37 roberts Exp $
 extern char **environ;
 #endif
 
+#ifdef _WIN32
+#define GETPID _getpid
+#else
+#define GETPID getpid
+#endif
+
 #include "fcgi_stdio.h"
 
 
@@ -53,7 +59,7 @@ int main ()
 	    "\r\n"
 	    "<title>FastCGI echo</title>"
 	    "<h1>FastCGI echo</h1>\n"
-            "Request number %d,  Process ID: %d<p>\n", ++count, getpid());
+               "Request number %d,  Process ID: %d<p>\n", ++count, GETPID());
 
         if (contentLength != NULL) {
             len = strtol(contentLength, NULL, 10);
diff --git a/examples/threaded.c b/examples/threaded.c
index 3e860ec..a0c57e3 100755
--- a/examples/threaded.c
+++ b/examples/threaded.c
@@ -24,7 +24,8 @@ static int counts[THREAD_COUNT];
 
 static void *doit(void *a)
 {
-    int rc, i, thread_id = (int)a;
+    int rc, i;
+    intptr_t thread_id = (intptr_t)a;
     pid_t pid = getpid();
     FCGX_Request request;
     char *server_name;
@@ -71,7 +72,7 @@ static void *doit(void *a)
 
 int main(void)
 {
-    int i;
+    intptr_t i;
     pthread_t id[THREAD_COUNT];
 
     FCGX_Init();
diff --git a/libfcgi/fcgi_stdio.c b/libfcgi/fcgi_stdio.c
index 9f0471d..86a57f1 100644
--- a/libfcgi/fcgi_stdio.c
+++ b/libfcgi/fcgi_stdio.c
@@ -19,6 +19,8 @@ static const char rcsid[] = "$Id: fcgi_stdio.c,v 1.15 2009/09/28 00:46:30 robs E
 #include <stdarg.h> /* for va_arg */
 #include <stdlib.h> /* for malloc */
 #include <string.h> /* for strerror */
+#include <assert.h>
+#include <limits.h>
 
 #include "fcgi_config.h"
 
@@ -61,6 +63,8 @@ extern int pclose(FILE *stream);
 
 #define popen _popen
 #define pclose _pclose
+#define fdopen _fdopen
+#define fileno _fileno
 
 #endif /* _WIN32 */
 
@@ -667,8 +671,10 @@ size_t FCGI_fread(void *ptr, size_t size, size_t nmemb, FCGI_FILE *fp)
         if((size * nmemb) == 0) {
             return 0;
         }
-        n = FCGX_GetStr((char *) ptr, size * nmemb, fp->fcgx_stream);
-        return (n/size);
+        ASSERT(size * nmemb < (size_t)INT_MAX);
+        n = FCGX_GetStr((char *) ptr, (int)(size * nmemb), fp->fcgx_stream);
+        ASSERT(n >= 0);
+        return ((size_t)n/size);
     }
     return (size_t)EOF;
 }
@@ -682,8 +688,10 @@ size_t FCGI_fwrite(void *ptr, size_t size, size_t nmemb, FCGI_FILE *fp)
         if((size * nmemb) == 0) {
             return 0;
         }
-        n = FCGX_PutStr((char *) ptr, size * nmemb, fp->fcgx_stream);
-        return (n/size);
+        ASSERT(size * nmemb < (size_t)INT_MAX);
+        n = (size_t)FCGX_PutStr((char *) ptr, (int)(size * nmemb), fp->fcgx_stream);
+        ASSERT(n >= 0);
+        return ((size_t)n/size);
     }
     return (size_t)EOF;
 }
diff --git a/libfcgi/fcgiapp.c b/libfcgi/fcgiapp.c
index 4167aa4..a307347 100644
--- a/libfcgi/fcgiapp.c
+++ b/libfcgi/fcgiapp.c
@@ -24,6 +24,10 @@ static const char rcsid[] = "$Id: fcgiapp.c,v 1.35 2003/06/22 00:16:43 robs Exp
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
+#ifndef _WIN32
+#include <stdint.h>
+#include <limits.h>
+#endif
 
 #ifndef _WIN32
 #include <stdint.h>
@@ -67,6 +71,12 @@ static const char rcsid[] = "$Id: fcgiapp.c,v 1.35 2003/06/22 00:16:43 robs Exp
 #define LONG_DOUBLE long double
 #endif
 
+#ifdef _WIN64
+#define ABS(a) _abs64(a)
+#else
+#define ABS(a) abs(a)
+#endif
+
 /*
  * Globals
  */
@@ -89,7 +99,7 @@ static void *Malloc(size_t size)
 
 static char *StringCopy(char *str)
 {
-    int strLen = strlen(str);
+    size_t strLen = strlen(str);
     char *newString = (char *)Malloc(strLen + 1);
     memcpy(newString, str, strLen);
     newString[strLen] = '\000';
@@ -146,7 +156,7 @@ int FCGX_GetChar(FCGX_Stream *stream)
  */
 int FCGX_GetStr(char *str, int n, FCGX_Stream *stream)
 {
-    int m, bytesMoved;
+    intptr_t m, bytesMoved;
 
     if (stream->isClosed || ! stream->isReader || n <= 0) {
         return 0;
@@ -170,15 +180,21 @@ int FCGX_GetStr(char *str, int n, FCGX_Stream *stream)
             memcpy(str, stream->rdNext, m);
             bytesMoved += m;
             stream->rdNext += m;
-            if(bytesMoved == n)
-                return bytesMoved;
+            if(bytesMoved == n) {
+                ASSERT(INT_MIN <= bytesMoved && bytesMoved <= INT_MAX);
+                return (int)bytesMoved;
+            }
             str += m;
         }
-        if(stream->isClosed || !stream->isReader)
-            return bytesMoved;
+        if(stream->isClosed || !stream->isReader) {
+            ASSERT(INT_MIN <= bytesMoved && bytesMoved <= INT_MAX);
+            return (int)bytesMoved;
+        }
         stream->fillBuffProc(stream);
-        if (stream->isClosed)
-            return bytesMoved;
+        if (stream->isClosed) {
+            ASSERT(INT_MIN <= bytesMoved && bytesMoved <= INT_MAX);
+            return (int)bytesMoved;
+        }
 
         stream->stopUnget = stream->rdNext;
     }
@@ -313,7 +329,7 @@ int FCGX_PutChar(int c, FCGX_Stream *stream)
  */
 int FCGX_PutStr(const char *str, int n, FCGX_Stream *stream)
 {
-    int m, bytesMoved;
+    intptr_t m, bytesMoved;
 
     /*
      * Fast path: room for n bytes in the buffer
@@ -334,8 +350,10 @@ int FCGX_PutStr(const char *str, int n, FCGX_Stream *stream)
             memcpy(stream->wrNext, str, m);
             bytesMoved += m;
             stream->wrNext += m;
-            if(bytesMoved == n)
-                return bytesMoved;
+            if(bytesMoved == n) {
+                ASSERT(INT_MIN <= bytesMoved && bytesMoved <= INT_MAX);
+                return (int)bytesMoved;
+            }
             str += m;
 	}
         if(stream->isClosed || stream->isReader)
@@ -359,7 +377,9 @@ int FCGX_PutStr(const char *str, int n, FCGX_Stream *stream)
  */
 int FCGX_PutS(const char *str, FCGX_Stream *stream)
 {
-    return FCGX_PutStr(str, strlen(str), stream);
+    size_t sz = strlen(str);
+    ASSERT(sz <= INT_MAX);
+    return FCGX_PutStr(str, (int)sz, stream);
 }
 
 /*
@@ -410,15 +430,17 @@ int FCGX_FPrintF(FCGX_Stream *stream, const char *format, ...)
     /*
      * Max size of a format specifier is 1 + 5 + 7 + 7 + 2 + 1 + slop
      */
-static void CopyAndAdvance(char **destPtr, char **srcPtr, int n);
+static void CopyAndAdvance(char **destPtr, char **srcPtr, intptr_t n);
 
 int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
 {
     char *f, *fStop, *percentPtr, *p, *fmtBuffPtr, *buffPtr;
-    int op, performedOp, sizeModifier, buffCount = 0, buffLen, specifierLength;
-    int fastPath, n, auxBuffLen = 0, buffReqd, minWidth, precision, exp;
+    intptr_t op, performedOp, sizeModifier, buffCount = 0, buffLen;
+    intptr_t fastPath, n, auxBuffLen = 0, minWidth, precision;
+    int exp;
+    intptr_t specifierLength, buffReqd;
     char *auxBuffPtr = NULL;
-    int streamCount = 0;
+    intptr_t streamCount = 0;
     char fmtBuff[FMT_BUFFLEN];
     char buff[PRINTF_BUFFLEN];
 
@@ -443,9 +465,11 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
         percentPtr = (char *)memchr(f, '%', fStop - f);
         if(percentPtr == NULL) percentPtr = fStop;
         if(percentPtr != f) {
-            if(FCGX_PutStr(f, percentPtr - f, stream) < 0)
+            intptr_t ptrDiff = percentPtr - f;
+            ASSERT(0 <= ptrDiff && ptrDiff < INT_MAX);
+            if(FCGX_PutStr(f, (int)ptrDiff, stream) < 0)
                 goto ErrorReturn;
-            streamCount += percentPtr - f;
+            streamCount += (int)ptrDiff;
             f = percentPtr;
             if(f == fStop) break;
 	}
@@ -510,14 +534,14 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
                 if(n == 0) {
                     if(*p == '*') {
                         minWidth = va_arg(arg, int);
-                        if(abs(minWidth) > 999999)
+                        if(ABS(minWidth) > 999999)
                             goto ErrorReturn;
 			/*
 			 * The following use of strlen rather than the
 			 * value returned from sprintf is because SUNOS4
 			 * returns a char * instead of an int count.
 			 */
-			sprintf(fmtBuffPtr, "%d", minWidth);
+                        sprintf(fmtBuffPtr, "%lld", (long long)minWidth);
                         fmtBuffPtr += strlen(fmtBuffPtr);
                         p++;
 	            } else {
@@ -546,7 +570,7 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
 			 * value returned from sprintf is because SUNOS4
 			 * returns a char * instead of an int count.
 			 */
-			    sprintf(fmtBuffPtr, "%d", precision);
+                            sprintf(fmtBuffPtr, "%lld", (long long)precision);
 			    fmtBuffPtr += strlen(fmtBuffPtr);
                             p++;
 	                } else {
@@ -768,11 +792,13 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
                     switch(sizeModifier) {
                         case ' ':
                             intPtrArg = va_arg(arg, int *);
-                            *intPtrArg = streamCount;
+                    ASSERT(INT_MIN <= streamCount && streamCount <= INT_MAX);
+                    *intPtrArg = (int)streamCount;
                             break;
                         case 'l':
                             longPtrArg = va_arg(arg, long *);
-                            *longPtrArg = streamCount;
+                    ASSERT(LONG_MIN <= streamCount && streamCount <= LONG_MAX);
+                    *longPtrArg = (long)streamCount;
                             break;
                         case 'h':
                             shortPtrArg = (short *) va_arg(arg, short *);
@@ -839,7 +865,8 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
         } /* for (;;) */
         ASSERT(buffCount < buffLen);
         if(buffCount > 0) {
-            if(FCGX_PutStr(buffPtr, buffCount, stream) < 0)
+            ASSERT(0 <= buffCount && buffCount <= INT_MAX);
+            if(FCGX_PutStr(buffPtr, (int)buffCount, stream) < 0)
                 goto ErrorReturn;
             streamCount += buffCount;
         } else if(buffCount < 0) {
@@ -852,18 +879,19 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
     streamCount = -1;
   NormalReturn:
     if(auxBuffPtr != NULL) free(auxBuffPtr);
-    return streamCount;
+    ASSERT(INT_MIN <= streamCount && streamCount <= INT_MAX);
+    return (int)streamCount;
 }
 
 /*
  * Copy n characters from *srcPtr to *destPtr, then increment
  * both *srcPtr and *destPtr by n.
  */
-static void CopyAndAdvance(char **destPtr, char **srcPtr, int n)
+static void CopyAndAdvance(char **destPtr, char **srcPtr, intptr_t n)
 {
     char *dest = *destPtr;
     char *src = *srcPtr;
-    int i;
+    intptr_t i;
     for (i = 0; i < n; i++)
         *dest++ = *src++;
     *destPtr = dest;
@@ -1079,7 +1107,7 @@ static void FreeParams(ParamsPtr *paramsPtrPtr)
  */
 static void PutParam(ParamsPtr paramsPtr, char *nameValue)
 {
-    int size;
+    intptr_t size;
 
     *paramsPtr->cur++ = nameValue;
     size = paramsPtr->cur - paramsPtr->vec;
@@ -1106,7 +1134,7 @@ static void PutParam(ParamsPtr paramsPtr, char *nameValue)
  */
 char *FCGX_GetParam(const char *name, FCGX_ParamArray envp)
 {
-    int len;
+    intptr_t len;
     char **p;
 
 	if (name == NULL || envp == NULL) return NULL;
@@ -1361,9 +1389,9 @@ static void WriteCloseRecords(struct FCGX_Stream *stream)
 
 
 
-static int write_it_all(int fd, char *buf, int len)
+static intptr_t write_it_all(int fd, char *buf, intptr_t len)
 {
-    int wrote;
+    intptr_t wrote;
 
     while (len) {
         wrote = OS_Write(fd, buf, len);
@@ -1388,7 +1416,7 @@ static int write_it_all(int fd, char *buf, int len)
 static void EmptyBuffProc(struct FCGX_Stream *stream, int doClose)
 {
     FCGX_Stream_Data *data = (FCGX_Stream_Data *)stream->data;
-    int cLen, eLen;
+    intptr_t cLen, eLen;
     /*
      * If the buffer contains stream data, fill in the header.
      * Pad the record to a multiple of 8 bytes in length.  Padding
@@ -1405,9 +1433,11 @@ static void EmptyBuffProc(struct FCGX_Stream *stream, int doClose)
              */
             memset(stream->wrNext, 0, eLen - cLen);
             stream->wrNext += eLen - cLen;
+            ASSERT(0 <= cLen && cLen <= INT_MAX);
+            ASSERT(0 <= eLen - cLen && eLen - cLen <= INT_MAX);
             *((FCGI_Header *) data->buff)
                     = MakeHeader(data->type,
-                            data->reqDataPtr->requestId, cLen, eLen - cLen);
+                             data->reqDataPtr->requestId, (int)cLen, (int)(eLen - cLen));
         } else {
             stream->wrNext = data->buff;
 	}
@@ -1461,7 +1491,7 @@ static int ProcessManagementRecord(int type, FCGX_Stream *stream)
     char response[64]; /* 64 = 8 + 3*(1+1+14+1)* + padding */
     char *responseP = &response[FCGI_HEADER_LEN];
     char *name, value = '\0';
-    int len, paddedLen;
+    intptr_t len, paddedLen;
     if(type == FCGI_GET_VALUES) {
         ReadParams(paramsPtr, stream);
         if((FCGX_GetError(stream) != 0) || (data->contentLen != 0)) {
@@ -1482,21 +1512,24 @@ static int ProcessManagementRecord(int type, FCGX_Stream *stream)
             }
             if(name != NULL) {
                 len = strlen(name);
-                sprintf(responseP, "%c%c%s%c", len, 1, name, value);
+                sprintf(responseP, "%c%c%s%c", (int)len, 1, name, value);
                 responseP += len + 3;
 	    }
         }
         len = responseP - &response[FCGI_HEADER_LEN];
         paddedLen = AlignInt8(len);
+        ASSERT(0 <= len && len <= INT_MAX);
+        ASSERT(0 <= paddedLen - len && paddedLen - len <= INT_MAX);
         *((FCGI_Header *) response)
             = MakeHeader(FCGI_GET_VALUES_RESULT, FCGI_NULL_REQUEST_ID,
-                         len, paddedLen - len);
+                         (int)len, (int)(paddedLen - len));
         FreeParams(&paramsPtr);
     } else {
         paddedLen = len = sizeof(FCGI_UnknownTypeBody);
+        ASSERT(0 <= len && len <= INT_MAX);
         ((FCGI_UnknownTypeRecord *) response)->header
             = MakeHeader(FCGI_UNKNOWN_TYPE, FCGI_NULL_REQUEST_ID,
-                         len, 0);
+                         (int)len, 0);
         ((FCGI_UnknownTypeRecord *) response)->body
             = MakeUnknownTypeBody(type);
     }
@@ -1629,6 +1662,7 @@ static void FillBuffProc(FCGX_Stream *stream)
     FCGI_Header header;
     int headerLen = 0;
     int status, count;
+    intptr_t ptrDiff;
 
     for (;;) {
         /*
@@ -1649,7 +1683,9 @@ static void FillBuffProc(FCGX_Stream *stream)
          * more content bytes, deliver all that are present in data->buff.
          */
         if(data->contentLen > 0) {
-            count = min(data->contentLen, data->buffStop - stream->rdNext);
+            ptrDiff = data->buffStop - stream->rdNext;
+            ASSERT(0 <= ptrDiff && ptrDiff <= INT_MAX);
+            count = min(data->contentLen, (int)ptrDiff);
             data->contentLen -= count;
             if(!data->skip) {
                 stream->wrNext = stream->stop = stream->rdNext + count;
@@ -1668,7 +1704,9 @@ static void FillBuffProc(FCGX_Stream *stream)
          * the client) was padded, skip over the padding bytes.
          */
         if(data->paddingLen > 0) {
-            count = min(data->paddingLen, data->buffStop - stream->rdNext);
+            ptrDiff = data->buffStop - stream->rdNext;
+            ASSERT(0 <= ptrDiff && ptrDiff <= INT_MAX);
+            count = min(data->paddingLen, (int)ptrDiff);
             data->paddingLen -= count;
             stream->rdNext += count;
             if(data->paddingLen > 0) {
@@ -1687,8 +1725,10 @@ static void FillBuffProc(FCGX_Stream *stream)
         /*
          * Fill header with bytes from the input buffer.
          */
+        ptrDiff = data->buffStop - stream->rdNext;
+        ASSERT(0 <= ptrDiff && ptrDiff <= INT_MAX);
         count = min((int)sizeof(header) - headerLen,
-                        data->buffStop - stream->rdNext);
+                    (int)ptrDiff);
         memcpy(((char *)(&header)) + headerLen, stream->rdNext, count);
         headerLen += count;
         stream->rdNext += count;
@@ -1756,6 +1796,7 @@ static void FillBuffProc(FCGX_Stream *stream)
 static FCGX_Stream *NewStream(
         FCGX_Request *reqDataPtr, int bufflen, int isReader, int streamType)
 {
+    intptr_t bufflen_p;
     /*
      * XXX: It would be a lot cleaner to have a NewStream that only
      * knows about the type FCGX_Stream, with all other
@@ -1767,7 +1808,9 @@ static FCGX_Stream *NewStream(
     FCGX_Stream *stream = (FCGX_Stream *)Malloc(sizeof(FCGX_Stream));
     FCGX_Stream_Data *data = (FCGX_Stream_Data *)Malloc(sizeof(FCGX_Stream_Data));
     data->reqDataPtr = reqDataPtr;
-    bufflen = AlignInt8(min(max(bufflen, 32), FCGI_MAX_LENGTH + 1));
+    bufflen_p = AlignInt8(min(max(bufflen, 32), FCGI_MAX_LENGTH + 1));
+    ASSERT(0 <= bufflen_p && bufflen_p <= INT_MAX);
+    bufflen = (int)bufflen_p;
     data->bufflen = bufflen;
     data->mBuff = (unsigned char *)Malloc(bufflen);
     data->buff = AlignPtr8(data->mBuff);
@@ -2310,7 +2353,6 @@ void FCGX_SetExitStatus(int status, FCGX_Stream *stream)
     data->reqDataPtr->appStatus = status;
 }
 
-
 int 
 FCGX_Attach(FCGX_Request * r)
 {
diff --git a/libfcgi/fcgio.cpp b/libfcgi/fcgio.cpp
index da8cbbf..0d038a4 100644
--- a/libfcgi/fcgio.cpp
+++ b/libfcgi/fcgio.cpp
@@ -22,9 +22,15 @@
 #define DLLAPI  __declspec(dllexport)
 #endif
 
+#ifndef _WIN32
 #include <stdio.h>
+#include <stdint.h>
 #include <limits.h>
+#endif
+
+#include <assert.h>
 #include "fcgio.h"
+#include "fcgimisc.h"
 
 using std::streambuf;
 using std::istream;
@@ -64,12 +70,13 @@ int fcgi_streambuf::overflow(int c)
 {
     if (this->bufsize)
     {
-        int plen = pptr() - pbase();
+        intptr_t plen = pptr() - pbase();
 
         if (plen) 
         {
-            if (FCGX_PutStr(pbase(), plen, this->fcgx) != plen) return EOF;
-            pbump(-plen);
+            ASSERT(0 <= plen && plen <= INT_MAX);
+            if (FCGX_PutStr(pbase(), (int)plen, this->fcgx) != plen) return EOF;
+            pbump((int)(-plen));
         }
     }
 
@@ -110,7 +117,8 @@ int fcgi_streambuf::underflow()
     {
         if (in_avail() == 0)
         {
-            int glen = FCGX_GetStr(eback(), this->bufsize, this->fcgx);
+            ASSERT(0 <= this->bufsize && this->bufsize <= INT_MAX);
+            int glen = FCGX_GetStr(eback(), (int)(this->bufsize), this->fcgx);
             if (glen <= 0) return EOF;
 
             setg(eback(), eback(), eback() + glen);
diff --git a/libfcgi/os_win32.c b/libfcgi/os_win32.c
index ba78b90..d32ffc2 100755
--- a/libfcgi/os_win32.c
+++ b/libfcgi/os_win32.c
@@ -129,6 +129,16 @@ static HANDLE hListen = INVALID_HANDLE_VALUE;
 
 static BOOLEAN libInitialized = FALSE;
 
+HANDLE strToHandle(char *str)
+{
+#ifdef _WIN64
+    return (HANDLE)_atoi64(str);
+#else
+    return (HANDLE)atoi(str);
+#endif
+}
+
+
 /*
  *--------------------------------------------------------------
  *
@@ -227,7 +237,7 @@ static int Win32NewDescriptor(FILE_TYPE type, int fd, int desiredFd)
 static void StdinThread(void * startup) 
 {
     int doIo = TRUE;
-    unsigned long fd;
+    ULONG_PTR fd;
     unsigned long bytesRead;
     POVERLAPPED_REQUEST pOv;
 
@@ -357,7 +367,7 @@ int OS_LibInit(int stdioFds[3])
     val = getenv(SHUTDOWN_EVENT_NAME);
     if (val != NULL) 
     {
-        HANDLE shutdownEvent = (HANDLE) atoi(val);
+        HANDLE shutdownEvent = strToHandle(val);
 
         if (_beginthread(ShutdownRequestThread, 0, shutdownEvent) == -1)
         {
@@ -371,7 +381,7 @@ int OS_LibInit(int stdioFds[3])
         val = getenv(MUTEX_VARNAME);
         if (val != NULL) 
         {
-            acceptMutex = (HANDLE) atoi(val);
+            acceptMutex = strToHandle(val);
         }
     }
 
@@ -460,8 +470,10 @@ int OS_LibInit(int stdioFds[3])
  */
     }
 
+    ASSERT(INT_MIN <= (intptr_t)stdioHandles[STDIN_FILENO] && 
+           (intptr_t)stdioHandles[STDIN_FILENO] <= INT_MAX);
     if ((fakeFd = Win32NewDescriptor(FD_PIPE_SYNC,
-				     (int)stdioHandles[STDIN_FILENO],
+                                     (int)(intptr_t)stdioHandles[STDIN_FILENO],
 				     STDIN_FILENO)) == -1) {
         return -1;
     } else {
@@ -494,7 +506,7 @@ int OS_LibInit(int stdioFds[3])
     if((cLenPtr = getenv("CONTENT_LENGTH")) != NULL &&
        atoi(cLenPtr) > 0) {
         hStdinThread = (HANDLE) _beginthread(StdinThread, 0, NULL);
-	if (hStdinThread == (HANDLE) -1) {
+        if (hStdinThread == (HANDLE)(LONG_PTR) -1) {
 	    printf("<H2>OS_LibInit Failed to create STDIN thread!  ERROR: %d</H2>\r\n\r\n",
 		   GetLastError());
 	    return -1;
@@ -515,8 +527,10 @@ int OS_LibInit(int stdioFds[3])
 	exit(99);
     }
 
+    ASSERT(INT_MIN <= (intptr_t)stdioHandles[STDOUT_FILENO] &&
+           (intptr_t)stdioHandles[STDOUT_FILENO] <= INT_MAX);
     if ((fakeFd = Win32NewDescriptor(FD_PIPE_SYNC,
-				     (int)stdioHandles[STDOUT_FILENO],
+                                     (int)(intptr_t)stdioHandles[STDOUT_FILENO],
 				     STDOUT_FILENO)) == -1) {
         return -1;
     } else {
@@ -532,8 +546,10 @@ int OS_LibInit(int stdioFds[3])
         DebugBreak();
 	exit(99);
     }
+    ASSERT(INT_MIN <= (intptr_t)stdioHandles[STDERR_FILENO] &&
+           (intptr_t)stdioHandles[STDERR_FILENO] <= INT_MAX);
     if ((fakeFd = Win32NewDescriptor(FD_PIPE_SYNC,
-				     (int)stdioHandles[STDERR_FILENO],
+                                     (int)(intptr_t)stdioHandles[STDERR_FILENO],
 				     STDERR_FILENO)) == -1) {
         return -1;
     } else {
@@ -735,7 +751,9 @@ int OS_CreateLocalIpcFd(const char *bindPath, int backlog)
 	        return -5;
 	    }
 
-        pseudoFd = Win32NewDescriptor(listenType, listenSock, -1);
+        ASSERT(INT_MIN <= (intptr_t)listenSock &&
+               (intptr_t)listenSock <= INT_MAX);
+        pseudoFd = Win32NewDescriptor(listenType, (int)(intptr_t)listenSock, -1);
         
         if (pseudoFd == -1) 
         {
@@ -776,7 +794,9 @@ int OS_CreateLocalIpcFd(const char *bindPath, int backlog)
             return -9;
         }
 
-        pseudoFd = Win32NewDescriptor(listenType, (int) hListenPipe, -1);
+        ASSERT(INT_MIN <= (intptr_t)hListenPipe &&
+               (intptr_t)hListenPipe <= INT_MAX);
+        pseudoFd = Win32NewDescriptor(listenType, (int)(intptr_t)hListenPipe, -1);
         
         if (pseudoFd == -1) 
         {
@@ -822,7 +842,7 @@ int OS_FcgiConnect(char *bindPath)
         if (*bindPath != ':')
         {
             char * p = strchr(bindPath, ':');
-            int len = p - bindPath;
+            intptr_t len = p - bindPath;
             host = malloc(len + 1);
             memcpy(host, bindPath, len);
             host[len] = '\0';
@@ -858,7 +878,8 @@ int OS_FcgiConnect(char *bindPath)
 	        return -1;
 	    }
 
-	    pseudoFd = Win32NewDescriptor(FD_SOCKET_SYNC, sock, -1);
+        ASSERT(INT_MIN <= (intptr_t)sock && (intptr_t)sock <= INT_MAX);
+        pseudoFd = Win32NewDescriptor(FD_SOCKET_SYNC, (int)(intptr_t)sock, -1);
 	    if (pseudoFd == -1) 
         {
 	        closesocket(sock);
@@ -893,7 +914,8 @@ int OS_FcgiConnect(char *bindPath)
             return -1;
         }
 
-        pseudoFd = Win32NewDescriptor(FD_PIPE_ASYNC, (int) hPipe, -1);
+        ASSERT(INT_MIN <= (intptr_t)hPipe && (intptr_t)hPipe <= INT_MAX);
+        pseudoFd = Win32NewDescriptor(FD_PIPE_ASYNC, (int)(intptr_t)hPipe, -1);
         
         if (pseudoFd == -1) 
         {
@@ -948,7 +970,8 @@ int OS_Read(int fd, char * buf, size_t len)
 	case FD_PIPE_SYNC:
 	case FD_PIPE_ASYNC:
 
-	    if (ReadFile(fdTable[fd].fid.fileHandle, buf, len, &bytesRead, NULL)) 
+        ASSERT(0 <= len && len <= ULONG_MAX);
+        if (ReadFile(fdTable[fd].fid.fileHandle, buf, (DWORD)len, &bytesRead, NULL)) 
         {
             ret = bytesRead;
         }
@@ -962,7 +985,8 @@ int OS_Read(int fd, char * buf, size_t len)
 	case FD_SOCKET_SYNC:
 	case FD_SOCKET_ASYNC:
 
-        ret = recv(fdTable[fd].fid.sock, buf, len, 0);
+        ASSERT(0 <= len && len <= INT_MAX);
+        ret = recv(fdTable[fd].fid.sock, buf, (int)len, 0);
 	    if (ret == SOCKET_ERROR) 
         {
 		    fdTable[fd].Errno = WSAGetLastError();
@@ -1011,7 +1035,8 @@ int OS_Write(int fd, char * buf, size_t len)
 	case FD_PIPE_SYNC:
 	case FD_PIPE_ASYNC:
 
-        if (WriteFile(fdTable[fd].fid.fileHandle, buf, len, &bytesWritten, NULL)) 
+        ASSERT(0 <= len && len <= ULONG_MAX);
+        if (WriteFile(fdTable[fd].fid.fileHandle, buf, (DWORD)len, &bytesWritten, NULL)) 
         {
             ret = bytesWritten;
         }
@@ -1025,7 +1050,8 @@ int OS_Write(int fd, char * buf, size_t len)
 	case FD_SOCKET_SYNC:
 	case FD_SOCKET_ASYNC:
 
-        ret = send(fdTable[fd].fid.sock, buf, len, 0);
+        ASSERT(0 <= len && len <= INT_MAX);
+        ret = send(fdTable[fd].fid.sock, buf, (int)len, 0);
         if (ret == SOCKET_ERROR) 
         {
 		    fdTable[fd].Errno = WSAGetLastError();
@@ -1390,7 +1416,7 @@ int OS_Close(int fd, int shutdown_ok)
             {
                 struct timeval tv;
                 fd_set rfds;
-                int sock = fdTable[fd].fid.sock;
+                SOCKET sock = fdTable[fd].fid.sock;
                 int rv;
                 char trash[1024];
    
@@ -1399,12 +1425,11 @@ int OS_Close(int fd, int shutdown_ok)
                 do 
                 {
 #pragma warning( disable : 4127 ) 
-	            FD_SET((unsigned) sock, &rfds);
-#pragma warning( default : 4127 )
-                    
+	            FD_SET(sock, &rfds);
+#pragma warning( default : 4127 ) 
 	            tv.tv_sec = 2;
 	            tv.tv_usec = 0;
-	            rv = select(sock + 1, &rfds, NULL, NULL, &tv);
+	            rv = select(0, &rfds, NULL, NULL, &tv);
                 }
                 while (rv > 0 && recv(sock, trash, sizeof(trash), 0) > 0);
             }
@@ -1472,12 +1497,12 @@ int OS_CloseRead(int fd)
  */
 int OS_DoIo(struct timeval *tmo)
 {
-    unsigned long fd;
+    ULONG_PTR fd;
     unsigned long bytes;
     POVERLAPPED_REQUEST pOv;
     struct timeb tb;
-    int ms;
-    int ms_last;
+    time_t ms;
+    time_t ms_last;
     int err;
 
     /* XXX
@@ -1495,8 +1520,9 @@ int OS_DoIo(struct timeval *tmo)
     while (ms >= 0) {
 	if(tmo && (ms = tmo->tv_sec*1000 + tmo->tv_usec/1000)> 100)
 	    ms = 100;
+        ASSERT(0 <= ms && ms < 0xFFFFFFFF);
 	if (!GetQueuedCompletionStatus(hIoCompPort, &bytes, &fd,
-	    (LPOVERLAPPED *)&pOv, ms) && !pOv) {
+           (LPOVERLAPPED *)&pOv, (DWORD)ms) && !pOv) {
 	    err = WSAGetLastError();
 	    return 0; /* timeout */
         }
@@ -1551,7 +1577,7 @@ static int CALLBACK isAddrOKCallback(LPWSABUF  lpCallerId,
                                      LPWSABUF  dc3,
                                      LPWSABUF  dc4,
                                      GROUP     *dc5,
-                                     DWORD     data)
+                                     DWORD_PTR data)
 {
     struct sockaddr_in *sockaddr = (struct sockaddr_in *) lpCallerId->buf;
 
@@ -1622,7 +1648,8 @@ static int acceptNamedPipe()
         }
     }
 
-    ipcFd = Win32NewDescriptor(FD_PIPE_SYNC, (int) hListen, -1);
+    ASSERT(INT_MIN <= (intptr_t)hListen && (intptr_t)hListen <= INT_MAX);
+    ipcFd = Win32NewDescriptor(FD_PIPE_SYNC, (int)(intptr_t)hListen, -1);
 	if (ipcFd == -1) 
     {
         DisconnectNamedPipe(hListen);
@@ -1649,7 +1676,7 @@ static int acceptSocket(const char *webServerAddrs)
             FD_ZERO(&readfds);
 
 #pragma warning( disable : 4127 ) 
-            FD_SET((unsigned int) hListen, &readfds);
+            FD_SET((SOCKET)hListen, &readfds);
 #pragma warning( default : 4127 ) 
 
             if (select(0, &readfds, NULL, NULL, &timeout) == 0)
@@ -1681,11 +1708,11 @@ static int acceptSocket(const char *webServerAddrs)
 
         closesocket(hSock);
 #else
-        hSock = WSAAccept((unsigned int) hListen,                    
+        hSock = WSAAccept((SOCKET)hListen,                    
                           &sockaddr,  
                           &sockaddrLen,               
                           isAddrOKCallback,  
-                          (DWORD) webServerAddrs);
+                          (DWORD_PTR) webServerAddrs);
 
         if (hSock != INVALID_SOCKET)
         {
@@ -1706,7 +1733,8 @@ static int acceptSocket(const char *webServerAddrs)
         return -1;
     }
     
-    ipcFd = Win32NewDescriptor(FD_SOCKET_SYNC, hSock, -1);
+    ASSERT(INT_MIN <= (intptr_t)hSock && (intptr_t)hSock <= INT_MAX);
+    ipcFd = Win32NewDescriptor(FD_SOCKET_SYNC, (int)(intptr_t)hSock, -1);
 	if (ipcFd == -1) 
     {
 	    closesocket(hSock);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.