Skip to content

Commit 966f015

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 a4c4e0b commit 966f015

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
@@ -1673,7 +1673,7 @@ ident_inet(hbaPort *port)
16731673

16741674
sock_fd = socket(ident_serv->ai_family, ident_serv->ai_socktype,
16751675
ident_serv->ai_protocol);
1676-
if (sock_fd < 0)
1676+
if (sock_fd == PGINVALID_SOCKET)
16771677
{
16781678
ereport(LOG,
16791679
(errcode_for_socket_access(),
@@ -1753,7 +1753,7 @@ ident_inet(hbaPort *port)
17531753
ident_response)));
17541754

17551755
ident_inet_done:
1756-
if (sock_fd >= 0)
1756+
if (sock_fd != PGINVALID_SOCKET)
17571757
closesocket(sock_fd);
17581758
pg_freeaddrinfo_all(remote_addr.addr.ss_family, ident_serv);
17591759
pg_freeaddrinfo_all(local_addr.addr.ss_family, la);
@@ -2576,7 +2576,7 @@ CheckRADIUSAuth(Port *port)
25762576
packet->length = htons(packet->length);
25772577

25782578
sock = socket(serveraddrs[0].ai_family, SOCK_DGRAM, 0);
2579-
if (sock < 0)
2579+
if (sock == PGINVALID_SOCKET)
25802580
{
25812581
ereport(LOG,
25822582
(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
@@ -363,7 +363,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
363363
break;
364364
}
365365

366-
if ((fd = socket(addr->ai_family, SOCK_STREAM, 0)) < 0)
366+
if ((fd = socket(addr->ai_family, SOCK_STREAM, 0)) == PGINVALID_SOCKET)
367367
{
368368
ereport(LOG,
369369
(errcode_for_socket_access(),
@@ -606,7 +606,7 @@ StreamConnection(pgsocket server_fd, Port *port)
606606
port->raddr.salen = sizeof(port->raddr.addr);
607607
if ((port->sock = accept(server_fd,
608608
(struct sockaddr *) & port->raddr.addr,
609-
&port->raddr.salen)) < 0)
609+
&port->raddr.salen)) == PGINVALID_SOCKET)
610610
{
611611
ereport(LOG,
612612
(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
@@ -1919,7 +1919,7 @@ ConnCreate(int serverFd)
19191919

19201920
if (StreamConnection(serverFd, port) != STATUS_OK)
19211921
{
1922-
if (port->sock >= 0)
1922+
if (port->sock != PGINVALID_SOCKET)
19231923
StreamClose(port->sock);
19241924
ConnFree(port);
19251925
return NULL;

src/interfaces/libpq/fe-connect.c

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

16281628
/* Open a socket */
1629-
conn->sock = socket(addr_cur->ai_family, SOCK_STREAM, 0);
1630-
if (conn->sock < 0)
1629+
{
1630+
/*
1631+
* While we use 'pgsocket' as the socket type in the
1632+
* backend, we use 'int' for libpq socket values.
1633+
* This requires us to map PGINVALID_SOCKET to -1
1634+
* on Windows.
1635+
* See http://msdn.microsoft.com/en-us/library/windows/desktop/ms740516%28v=vs.85%29.aspx
1636+
*/
1637+
pgsocket sock = socket(addr_cur->ai_family, SOCK_STREAM, 0);
1638+
#ifdef WIN32
1639+
if (sock == PGINVALID_SOCKET)
1640+
conn->sock = -1;
1641+
else
1642+
#endif
1643+
conn->sock = sock;
1644+
}
1645+
if (conn->sock == -1)
16311646
{
16321647
/*
16331648
* ignore socket() failure if we have more addresses
@@ -3173,7 +3188,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
31733188
char *errbuf, int errbufsize)
31743189
{
31753190
int save_errno = SOCK_ERRNO;
3176-
int tmpsock = -1;
3191+
pgsocket tmpsock = PGINVALID_SOCKET;
31773192
char sebuf[256];
31783193
int maxlen;
31793194
struct
@@ -3186,7 +3201,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
31863201
* We need to open a temporary connection to the postmaster. Do this with
31873202
* only kernel calls.
31883203
*/
3189-
if ((tmpsock = socket(raddr->addr.ss_family, SOCK_STREAM, 0)) < 0)
3204+
if ((tmpsock = socket(raddr->addr.ss_family, SOCK_STREAM, 0)) == PGINVALID_SOCKET)
31903205
{
31913206
strlcpy(errbuf, "PQcancel() -- socket() failed: ", errbufsize);
31923207
goto cancel_errReturn;
@@ -3257,7 +3272,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
32573272
maxlen);
32583273
strcat(errbuf, "\n");
32593274
}
3260-
if (tmpsock >= 0)
3275+
if (tmpsock != PGINVALID_SOCKET)
32613276
closesocket(tmpsock);
32623277
SOCK_ERRNO_SET(save_errno);
32633278
return FALSE;
@@ -5246,6 +5261,15 @@ PQerrorMessage(const PGconn *conn)
52465261
return conn->errorMessage.data;
52475262
}
52485263

5264+
/*
5265+
* In Windows, socket values are unsigned, and an invalid socket value
5266+
* (INVALID_SOCKET) is ~0, which equals -1 in comparisons (with no compiler
5267+
* warning). Ideally we would return an unsigned value for PQsocket() on
5268+
* Windows, but that would cause the function's return value to differ from
5269+
* Unix, so we just return -1 for invalid sockets.
5270+
* http://msdn.microsoft.com/en-us/library/windows/desktop/cc507522%28v=vs.85%29.aspx
5271+
* http://stackoverflow.com/questions/10817252/why-is-invalid-socket-defined-as-0-in-winsock2-h-c
5272+
*/
52495273
int
52505274
PQsocket(const PGconn *conn)
52515275
{

src/interfaces/libpq/libpq-int.h

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

363363
/* Connection data */
364+
/* See PQconnectPoll() for how we use 'int' and not 'pgsocket'. */
364365
int sock; /* Unix FD for socket, -1 if not connected */
365366
SockAddr laddr; /* Local address */
366367
SockAddr raddr; /* Remote address */

0 commit comments

Comments
 (0)