Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4180934

Browse files
committed
check socket creation errors against PGINVALID_SOCKET
Previously, in some places, socket creation errors were checked fornegative values, which is not true for Windows because sockets areunsigned. This masked socket creation errors on Windows.Backpatch through 9.0. 8.4 doesn't have the infrastructure to fix this.
1 parent848b9f0 commit4180934

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

14641464
sock_fd=socket(ident_serv->ai_family,ident_serv->ai_socktype,
14651465
ident_serv->ai_protocol);
1466-
if (sock_fd<0)
1466+
if (sock_fd==PGINVALID_SOCKET)
14671467
{
14681468
ereport(LOG,
14691469
(errcode_for_socket_access(),
@@ -1543,7 +1543,7 @@ ident_inet(hbaPort *port)
15431543
ident_response)));
15441544

15451545
ident_inet_done:
1546-
if (sock_fd>=0)
1546+
if (sock_fd!=PGINVALID_SOCKET)
15471547
closesocket(sock_fd);
15481548
pg_freeaddrinfo_all(remote_addr.addr.ss_family,ident_serv);
15491549
pg_freeaddrinfo_all(local_addr.addr.ss_family,la);
@@ -2361,7 +2361,7 @@ CheckRADIUSAuth(Port *port)
23612361
packet->length=htons(packet->length);
23622362

23632363
sock=socket(serveraddrs[0].ai_family,SOCK_DGRAM,0);
2364-
if (sock<0)
2364+
if (sock==PGINVALID_SOCKET)
23652365
{
23662366
ereport(LOG,
23672367
(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
interror;
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
#ifdefHAVE_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_tn_buffer=1024;
791-
intsock;
791+
pgsocketsock;
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
(structsockaddr*)&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(SOCKETs,intwhat,inttimeout)
133133
{
134134
staticHANDLEwaitevent=INVALID_HANDLE_VALUE;
135-
staticSOCKETcurrent_socket=-1;
135+
staticSOCKETcurrent_socket=INVALID_SOCKET;
136136
staticintisUDP=0;
137137
HANDLEevents[2];
138138
intr;

‎src/backend/postmaster/postmaster.c

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

21702170
if (StreamConnection(serverFd,port)!=STATUS_OK)
21712171
{
2172-
if (port->sock>=0)
2172+
if (port->sock!=PGINVALID_SOCKET)
21732173
StreamClose(port->sock);
21742174
ConnFree(port);
21752175
returnNULL;

‎src/interfaces/libpq/fe-connect.c

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

16341634
/* Open a socket */
1635-
conn->sock=socket(addr_cur->ai_family,SOCK_STREAM,0);
1636-
if (conn->sock<0)
1635+
{
1636+
/*
1637+
* While we use 'pgsocket' as the socket type in the
1638+
* backend, we use 'int' for libpq socket values.
1639+
* This requires us to map PGINVALID_SOCKET to -1
1640+
* on Windows.
1641+
* See http://msdn.microsoft.com/en-us/library/windows/desktop/ms740516%28v=vs.85%29.aspx
1642+
*/
1643+
pgsocketsock=socket(addr_cur->ai_family,SOCK_STREAM,0);
1644+
#ifdefWIN32
1645+
if (sock==PGINVALID_SOCKET)
1646+
conn->sock=-1;
1647+
else
1648+
#endif
1649+
conn->sock=sock;
1650+
}
1651+
if (conn->sock==-1)
16371652
{
16381653
/*
16391654
* 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,interrbufsize)
31373152
{
31383153
intsave_errno=SOCK_ERRNO;
3139-
inttmpsock=-1;
3154+
pgsockettmpsock=PGINVALID_SOCKET;
31403155
charsebuf[256];
31413156
intmaxlen;
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
gotocancel_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;
@@ -5300,6 +5315,15 @@ PQerrorMessage(const PGconn *conn)
53005315
returnconn->errorMessage.data;
53015316
}
53025317

5318+
/*
5319+
* In Windows, socket values are unsigned, and an invalid socket value
5320+
* (INVALID_SOCKET) is ~0, which equals -1 in comparisons (with no compiler
5321+
* warning). Ideally we would return an unsigned value for PQsocket() on
5322+
* Windows, but that would cause the function's return value to differ from
5323+
* Unix, so we just return -1 for invalid sockets.
5324+
* http://msdn.microsoft.com/en-us/library/windows/desktop/cc507522%28v=vs.85%29.aspx
5325+
* http://stackoverflow.com/questions/10817252/why-is-invalid-socket-defined-as-0-in-winsock2-h-c
5326+
*/
53035327
int
53045328
PQsocket(constPGconn*conn)
53055329
{

‎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
intsock;/* Unix FD for socket, -1 if not connected */
368369
SockAddrladdr;/* Local address */
369370
SockAddrraddr;/* Remote address */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp