Skip to content

Commit f716c32

Browse files
committed
check socket creation errors against PGINVALID_SOCKET
Previously, in some places, socket creation errors were checked for negative values, which is not true for Windows because sockets are unsigned. This masked socket creation errors on Windows. Backpatch through 9.0. 8.4 doesn't have the infrastructure to fix this.
1 parent c03a6ae commit f716c32

File tree

7 files changed

+42
-17
lines changed

7 files changed

+42
-17
lines changed

src/backend/libpq/auth.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,7 +1676,7 @@ ident_inet(hbaPort *port)
16761676

16771677
sock_fd = socket(ident_serv->ai_family, ident_serv->ai_socktype,
16781678
ident_serv->ai_protocol);
1679-
if (sock_fd < 0)
1679+
if (sock_fd == PGINVALID_SOCKET)
16801680
{
16811681
ereport(LOG,
16821682
(errcode_for_socket_access(),
@@ -1756,7 +1756,7 @@ ident_inet(hbaPort *port)
17561756
ident_response)));
17571757

17581758
ident_inet_done:
1759-
if (sock_fd >= 0)
1759+
if (sock_fd != PGINVALID_SOCKET)
17601760
closesocket(sock_fd);
17611761
pg_freeaddrinfo_all(remote_addr.addr.ss_family, ident_serv);
17621762
pg_freeaddrinfo_all(local_addr.addr.ss_family, la);
@@ -2583,7 +2583,7 @@ CheckRADIUSAuth(Port *port)
25832583
packet->length = htons(packet->length);
25842584

25852585
sock = socket(serveraddrs[0].ai_family, SOCK_DGRAM, 0);
2586-
if (sock < 0)
2586+
if (sock == PGINVALID_SOCKET)
25872587
{
25882588
ereport(LOG,
25892589
(errmsg("could not create RADIUS socket: %m")));

src/backend/libpq/ip.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
547547
int error;
548548

549549
sock = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
550-
if (sock == SOCKET_ERROR)
550+
if (sock == INVALID_SOCKET)
551551
return -1;
552552

553553
while (n_ii < 1024)
@@ -670,7 +670,7 @@ pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
670670
total;
671671

672672
sock = socket(AF_INET, SOCK_DGRAM, 0);
673-
if (sock == -1)
673+
if (sock == PGINVALID_SOCKET)
674674
return -1;
675675

676676
while (n_buffer < 1024 * 100)
@@ -711,7 +711,7 @@ pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
711711
#ifdef HAVE_IPV6
712712
/* We'll need an IPv6 socket too for the SIOCGLIFNETMASK ioctls */
713713
sock6 = socket(AF_INET6, SOCK_DGRAM, 0);
714-
if (sock6 == -1)
714+
if (sock6 == PGINVALID_SOCKET)
715715
{
716716
free(buffer);
717717
close(sock);
@@ -788,10 +788,10 @@ pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
788788
char *ptr,
789789
*buffer = NULL;
790790
size_t n_buffer = 1024;
791-
int sock;
791+
pgsocket sock;
792792

793793
sock = socket(AF_INET, SOCK_DGRAM, 0);
794-
if (sock == -1)
794+
if (sock == PGINVALID_SOCKET)
795795
return -1;
796796

797797
while (n_buffer < 1024 * 100)

src/backend/libpq/pqcomm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
392392
break;
393393
}
394394

395-
if ((fd = socket(addr->ai_family, SOCK_STREAM, 0)) < 0)
395+
if ((fd = socket(addr->ai_family, SOCK_STREAM, 0)) == PGINVALID_SOCKET)
396396
{
397397
ereport(LOG,
398398
(errcode_for_socket_access(),
@@ -632,7 +632,7 @@ StreamConnection(pgsocket server_fd, Port *port)
632632
port->raddr.salen = sizeof(port->raddr.addr);
633633
if ((port->sock = accept(server_fd,
634634
(struct sockaddr *) & port->raddr.addr,
635-
&port->raddr.salen)) < 0)
635+
&port->raddr.salen)) == PGINVALID_SOCKET)
636636
{
637637
ereport(LOG,
638638
(errcode_for_socket_access(),

src/backend/port/win32/socket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ int
132132
pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout)
133133
{
134134
static HANDLE waitevent = INVALID_HANDLE_VALUE;
135-
static SOCKET current_socket = -1;
135+
static SOCKET current_socket = INVALID_SOCKET;
136136
static int isUDP = 0;
137137
HANDLE events[2];
138138
int r;

src/backend/postmaster/postmaster.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2146,7 +2146,7 @@ ConnCreate(int serverFd)
21462146

21472147
if (StreamConnection(serverFd, port) != STATUS_OK)
21482148
{
2149-
if (port->sock >= 0)
2149+
if (port->sock != PGINVALID_SOCKET)
21502150
StreamClose(port->sock);
21512151
ConnFree(port);
21522152
return NULL;

src/interfaces/libpq/fe-connect.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,8 +1631,23 @@ PQconnectPoll(PGconn *conn)
16311631
conn->raddr.salen = addr_cur->ai_addrlen;
16321632

16331633
/* Open a socket */
1634-
conn->sock = socket(addr_cur->ai_family, SOCK_STREAM, 0);
1635-
if (conn->sock < 0)
1634+
{
1635+
/*
1636+
* While we use 'pgsocket' as the socket type in the
1637+
* backend, we use 'int' for libpq socket values.
1638+
* This requires us to map PGINVALID_SOCKET to -1
1639+
* on Windows.
1640+
* See http://msdn.microsoft.com/en-us/library/windows/desktop/ms740516%28v=vs.85%29.aspx
1641+
*/
1642+
pgsocket sock = socket(addr_cur->ai_family, SOCK_STREAM, 0);
1643+
#ifdef WIN32
1644+
if (sock == PGINVALID_SOCKET)
1645+
conn->sock = -1;
1646+
else
1647+
#endif
1648+
conn->sock = sock;
1649+
}
1650+
if (conn->sock == -1)
16361651
{
16371652
/*
16381653
* ignore socket() failure if we have more addresses
@@ -3136,7 +3151,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
31363151
char *errbuf, int errbufsize)
31373152
{
31383153
int save_errno = SOCK_ERRNO;
3139-
int tmpsock = -1;
3154+
pgsocket tmpsock = PGINVALID_SOCKET;
31403155
char sebuf[256];
31413156
int maxlen;
31423157
struct
@@ -3149,7 +3164,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
31493164
* We need to open a temporary connection to the postmaster. Do this with
31503165
* only kernel calls.
31513166
*/
3152-
if ((tmpsock = socket(raddr->addr.ss_family, SOCK_STREAM, 0)) < 0)
3167+
if ((tmpsock = socket(raddr->addr.ss_family, SOCK_STREAM, 0)) == PGINVALID_SOCKET)
31533168
{
31543169
strlcpy(errbuf, "PQcancel() -- socket() failed: ", errbufsize);
31553170
goto cancel_errReturn;
@@ -3220,7 +3235,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
32203235
maxlen);
32213236
strcat(errbuf, "\n");
32223237
}
3223-
if (tmpsock >= 0)
3238+
if (tmpsock != PGINVALID_SOCKET)
32243239
closesocket(tmpsock);
32253240
SOCK_ERRNO_SET(save_errno);
32263241
return FALSE;
@@ -5281,6 +5296,15 @@ PQerrorMessage(const PGconn *conn)
52815296
return conn->errorMessage.data;
52825297
}
52835298

5299+
/*
5300+
* In Windows, socket values are unsigned, and an invalid socket value
5301+
* (INVALID_SOCKET) is ~0, which equals -1 in comparisons (with no compiler
5302+
* warning). Ideally we would return an unsigned value for PQsocket() on
5303+
* Windows, but that would cause the function's return value to differ from
5304+
* Unix, so we just return -1 for invalid sockets.
5305+
* http://msdn.microsoft.com/en-us/library/windows/desktop/cc507522%28v=vs.85%29.aspx
5306+
* http://stackoverflow.com/questions/10817252/why-is-invalid-socket-defined-as-0-in-winsock2-h-c
5307+
*/
52845308
int
52855309
PQsocket(const PGconn *conn)
52865310
{

src/interfaces/libpq/libpq-int.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ struct pg_conn
364364
PGnotify *notifyTail; /* newest unreported Notify msg */
365365

366366
/* Connection data */
367+
/* See PQconnectPoll() for how we use 'int' and not 'pgsocket'. */
367368
int sock; /* Unix FD for socket, -1 if not connected */
368369
SockAddr laddr; /* Local address */
369370
SockAddr raddr; /* Remote address */

0 commit comments

Comments
 (0)