Skip to content

Commit 5d305d8

Browse files
committed
libpq: use pgsocket for socket values, for portability
Previously, 'int' was used for socket values in libpq, but socket values are unsigned on Windows. This is a style correction. Initial patch and previous PGINVALID_SOCKET initial patch by Joel Jacobson, modified by me Report from PVS-Studio
1 parent be5f7ff commit 5d305d8

File tree

6 files changed

+16
-32
lines changed

6 files changed

+16
-32
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,9 @@ pqDropConnection(PGconn *conn)
398398
/* Drop any SSL state */
399399
pqsecure_close(conn);
400400
/* Close the socket itself */
401-
if (conn->sock >= 0)
401+
if (conn->sock != PGINVALID_SOCKET)
402402
closesocket(conn->sock);
403-
conn->sock = -1;
403+
conn->sock = PGINVALID_SOCKET;
404404
/* Discard any unread/unsent data */
405405
conn->inStart = conn->inCursor = conn->inEnd = 0;
406406
conn->outCount = 0;
@@ -1631,24 +1631,8 @@ PQconnectPoll(PGconn *conn)
16311631
addr_cur->ai_addrlen);
16321632
conn->raddr.salen = addr_cur->ai_addrlen;
16331633

1634-
/* Open a socket */
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-
pgsocket sock = socket(addr_cur->ai_family, SOCK_STREAM, 0);
1644-
#ifdef WIN32
1645-
if (sock == PGINVALID_SOCKET)
1646-
conn->sock = -1;
1647-
else
1648-
#endif
1649-
conn->sock = sock;
1650-
}
1651-
if (conn->sock == -1)
1634+
conn->sock = socket(addr_cur->ai_family, SOCK_STREAM, 0);
1635+
if (conn->sock == PGINVALID_SOCKET)
16521636
{
16531637
/*
16541638
* ignore socket() failure if we have more addresses
@@ -2717,7 +2701,7 @@ makeEmptyPGconn(void)
27172701
conn->client_encoding = PG_SQL_ASCII;
27182702
conn->std_strings = false; /* unless server says differently */
27192703
conn->verbosity = PQERRORS_DEFAULT;
2720-
conn->sock = -1;
2704+
conn->sock = PGINVALID_SOCKET;
27212705
conn->auth_req_received = false;
27222706
conn->password_needed = false;
27232707
conn->dot_pgpass_used = false;
@@ -2882,7 +2866,7 @@ closePGconn(PGconn *conn)
28822866
* Note that the protocol doesn't allow us to send Terminate messages
28832867
* during the startup phase.
28842868
*/
2885-
if (conn->sock >= 0 && conn->status == CONNECTION_OK)
2869+
if (conn->sock != PGINVALID_SOCKET && conn->status == CONNECTION_OK)
28862870
{
28872871
/*
28882872
* Try to send "close connection" message to backend. Ignore any
@@ -3103,7 +3087,7 @@ PQgetCancel(PGconn *conn)
31033087
if (!conn)
31043088
return NULL;
31053089

3106-
if (conn->sock < 0)
3090+
if (conn->sock == PGINVALID_SOCKET)
31073091
return NULL;
31083092

31093093
cancel = malloc(sizeof(PGcancel));
@@ -3284,7 +3268,7 @@ PQrequestCancel(PGconn *conn)
32843268
if (!conn)
32853269
return FALSE;
32863270

3287-
if (conn->sock < 0)
3271+
if (conn->sock == PGINVALID_SOCKET)
32883272
{
32893273
strlcpy(conn->errorMessage.data,
32903274
"PQrequestCancel() -- connection is not open\n",
@@ -5361,7 +5345,7 @@ PQsocket(const PGconn *conn)
53615345
{
53625346
if (!conn)
53635347
return -1;
5364-
return conn->sock;
5348+
return (conn->sock != PGINVALID_SOCKET) ? conn->sock : -1;
53655349
}
53665350

53675351
int

src/interfaces/libpq/fe-exec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2549,7 +2549,7 @@ PQfn(PGconn *conn,
25492549
/* clear the error string */
25502550
resetPQExpBuffer(&conn->errorMessage);
25512551

2552-
if (conn->sock < 0 || conn->asyncStatus != PGASYNC_IDLE ||
2552+
if (conn->sock == PGINVALID_SOCKET || conn->asyncStatus != PGASYNC_IDLE ||
25532553
conn->result != NULL)
25542554
{
25552555
printfPQExpBuffer(&conn->errorMessage,

src/interfaces/libpq/fe-misc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ pqReadData(PGconn *conn)
604604
int someread = 0;
605605
int nread;
606606

607-
if (conn->sock < 0)
607+
if (conn->sock == PGINVALID_SOCKET)
608608
{
609609
printfPQExpBuffer(&conn->errorMessage,
610610
libpq_gettext("connection not open\n"));
@@ -800,7 +800,7 @@ pqSendSome(PGconn *conn, int len)
800800
int remaining = conn->outCount;
801801
int result = 0;
802802

803-
if (conn->sock < 0)
803+
if (conn->sock == PGINVALID_SOCKET)
804804
{
805805
printfPQExpBuffer(&conn->errorMessage,
806806
libpq_gettext("connection not open\n"));
@@ -1011,7 +1011,7 @@ pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
10111011

10121012
if (!conn)
10131013
return -1;
1014-
if (conn->sock < 0)
1014+
if (conn->sock == PGINVALID_SOCKET)
10151015
{
10161016
printfPQExpBuffer(&conn->errorMessage,
10171017
libpq_gettext("socket not open\n"));

src/interfaces/libpq/fe-protocol2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ pqGetline2(PGconn *conn, char *s, int maxlen)
12111211
{
12121212
int result = 1; /* return value if buffer overflows */
12131213

1214-
if (conn->sock < 0 ||
1214+
if (conn->sock == PGINVALID_SOCKET ||
12151215
conn->asyncStatus != PGASYNC_COPY_OUT)
12161216
{
12171217
*s = '\0';

src/interfaces/libpq/fe-protocol3.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,7 @@ pqGetline3(PGconn *conn, char *s, int maxlen)
15681568
{
15691569
int status;
15701570

1571-
if (conn->sock < 0 ||
1571+
if (conn->sock == PGINVALID_SOCKET ||
15721572
(conn->asyncStatus != PGASYNC_COPY_OUT &&
15731573
conn->asyncStatus != PGASYNC_COPY_BOTH) ||
15741574
conn->copy_is_binary)

src/interfaces/libpq/libpq-int.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ struct pg_conn
365365

366366
/* Connection data */
367367
/* See PQconnectPoll() for how we use 'int' and not 'pgsocket'. */
368-
int sock; /* Unix FD for socket, -1 if not connected */
368+
pgsocket sock; /* FD for socket, PGINVALID_SOCKET if unconnected */
369369
SockAddr laddr; /* Local address */
370370
SockAddr raddr; /* Remote address */
371371
ProtocolVersion pversion; /* FE/BE protocol version in use */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy