Commit 8c79f3c4 authored by Bruce Momjian's avatar Bruce Momjian

i've spotted a following problem using DBD::Pg under win32. winsock

functions do not set errno, so some normal conditions are treated as
fatal errors. e.g. fetching large tuples fails, as at some point recv()
returns EWOULDBLOCK. here's a patch, which replaces errno with
WSAGetLastError(). i've tried to to affect non-win32 code.

Dmitry Yurtaev
parent 8f75c1b0
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.168 2001/07/16 20:05:51 petere Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.169 2001/07/20 17:45:05 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -736,9 +736,6 @@ connectNoDelay(PGconn *conn) ...@@ -736,9 +736,6 @@ connectNoDelay(PGconn *conn)
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not set socket to TCP no delay mode: %s\n"), libpq_gettext("could not set socket to TCP no delay mode: %s\n"),
strerror(errno)); strerror(errno));
#ifdef WIN32
printf("Winsock error: %i\n", WSAGetLastError());
#endif
return 0; return 0;
} }
...@@ -937,11 +934,7 @@ connectDBStart(PGconn *conn) ...@@ -937,11 +934,7 @@ connectDBStart(PGconn *conn)
*/ */
if (connect(conn->sock, &conn->raddr.sa, conn->raddr_len) < 0) if (connect(conn->sock, &conn->raddr.sa, conn->raddr_len) < 0)
{ {
#ifndef WIN32
if (errno == EINPROGRESS || errno == 0) if (errno == EINPROGRESS || errno == 0)
#else
if (WSAGetLastError() == WSAEINPROGRESS)
#endif
{ {
/* /*
...@@ -2142,7 +2135,11 @@ PQrequestCancel(PGconn *conn) ...@@ -2142,7 +2135,11 @@ PQrequestCancel(PGconn *conn)
strcpy(conn->errorMessage.data, strcpy(conn->errorMessage.data,
"PQrequestCancel() -- connection is not open\n"); "PQrequestCancel() -- connection is not open\n");
conn->errorMessage.len = strlen(conn->errorMessage.data); conn->errorMessage.len = strlen(conn->errorMessage.data);
#ifdef WIN32
WSASetLastError(save_errno);
#else
errno = save_errno; errno = save_errno;
#endif
return FALSE; return FALSE;
} }
...@@ -2184,11 +2181,12 @@ PQrequestCancel(PGconn *conn) ...@@ -2184,11 +2181,12 @@ PQrequestCancel(PGconn *conn)
/* Sent it, done */ /* Sent it, done */
#ifdef WIN32 #ifdef WIN32
closesocket(tmpsock); closesocket(tmpsock);
WSASetLastError(save_errno);
#else #else
close(tmpsock); close(tmpsock);
errno = save_errno;
#endif #endif
errno = save_errno;
return TRUE; return TRUE;
cancel_errReturn: cancel_errReturn:
...@@ -2199,11 +2197,12 @@ cancel_errReturn: ...@@ -2199,11 +2197,12 @@ cancel_errReturn:
{ {
#ifdef WIN32 #ifdef WIN32
closesocket(tmpsock); closesocket(tmpsock);
WSASetLastError(save_errno);
#else #else
close(tmpsock); close(tmpsock);
errno = save_errno;
#endif #endif
} }
errno = save_errno;
return FALSE; return FALSE;
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.103 2001/07/15 13:45:04 petere Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.104 2001/07/20 17:45:06 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -223,7 +223,7 @@ pqResultAlloc(PGresult *res, size_t nBytes, bool isBinary) ...@@ -223,7 +223,7 @@ pqResultAlloc(PGresult *res, size_t nBytes, bool isBinary)
} }
/* If there's enough space in the current block, no problem. */ /* If there's enough space in the current block, no problem. */
if (nBytes <= res->spaceLeft) if (nBytes <= (size_t)res->spaceLeft)
{ {
space = res->curBlock->space + res->curOffset; space = res->curBlock->space + res->curOffset;
res->curOffset += nBytes; res->curOffset += nBytes;
...@@ -1024,7 +1024,7 @@ getAnotherTuple(PGconn *conn, int binary) ...@@ -1024,7 +1024,7 @@ getAnotherTuple(PGconn *conn, int binary)
vlen = 0; vlen = 0;
if (tup[i].value == NULL) if (tup[i].value == NULL)
{ {
tup[i].value = (char *) pqResultAlloc(result, vlen + 1, binary); tup[i].value = (char *) pqResultAlloc(result, vlen + 1, (bool)binary);
if (tup[i].value == NULL) if (tup[i].value == NULL)
goto outOfMemory; goto outOfMemory;
} }
...@@ -2051,7 +2051,11 @@ PQoidValue(const PGresult *res) ...@@ -2051,7 +2051,11 @@ PQoidValue(const PGresult *res)
if (!res || !res->cmdStatus || strncmp(res->cmdStatus, "INSERT ", 7) != 0) if (!res || !res->cmdStatus || strncmp(res->cmdStatus, "INSERT ", 7) != 0)
return InvalidOid; return InvalidOid;
#ifdef WIN32
WSASetLastError(0);
#else
errno = 0; errno = 0;
#endif
result = strtoul(res->cmdStatus + 7, &endptr, 10); result = strtoul(res->cmdStatus + 7, &endptr, 10);
if (!endptr || (*endptr != ' ' && *endptr != '\0') || errno == ERANGE) if (!endptr || (*endptr != ' ' && *endptr != '\0') || errno == ERANGE)
......
...@@ -25,13 +25,17 @@ ...@@ -25,13 +25,17 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.51 2001/07/15 13:45:04 petere Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.52 2001/07/20 17:45:06 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include <errno.h>
#include <signal.h>
#include <time.h>
#ifdef WIN32 #ifdef WIN32
#include "win32.h" #include "win32.h"
#else #else
...@@ -39,10 +43,6 @@ ...@@ -39,10 +43,6 @@
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#include <errno.h>
#include <signal.h>
#include <time.h>
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
#endif #endif
......
...@@ -21,3 +21,18 @@ ...@@ -21,3 +21,18 @@
* crypt not available (yet) * crypt not available (yet)
*/ */
#define crypt(a,b) a #define crypt(a,b) a
/*
* assumes that errno is used for sockets only
*
*/
#undef errno
#undef EINTR
#undef EAGAIN /* doesn't apply on sockets */
#define errno WSAGetLastError()
#define EINTR WSAEINTR
#define EWOULDBLOCK WSAEWOULDBLOCK
#define ECONNRESET WSAECONNRESET
#define EINPROGRESS WSAEINPROGRESS
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment