]> granicus.if.org Git - postgresql/commitdiff
libpq: use pgsocket for socket values, for portability
authorBruce Momjian <bruce@momjian.us>
Wed, 16 Apr 2014 23:46:51 +0000 (19:46 -0400)
committerBruce Momjian <bruce@momjian.us>
Wed, 16 Apr 2014 23:46:51 +0000 (19:46 -0400)
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

src/interfaces/libpq/fe-connect.c
src/interfaces/libpq/fe-exec.c
src/interfaces/libpq/fe-misc.c
src/interfaces/libpq/fe-protocol2.c
src/interfaces/libpq/fe-protocol3.c
src/interfaces/libpq/libpq-int.h

index 5d1e45629ca2f5d0940eefb26f097b9cba543584..10cc0e69b20eef8eb05b3e2edfe375975eae2934 100644 (file)
@@ -398,9 +398,9 @@ pqDropConnection(PGconn *conn)
        /* Drop any SSL state */
        pqsecure_close(conn);
        /* Close the socket itself */
-       if (conn->sock >= 0)
+       if (conn->sock != PGINVALID_SOCKET)
                closesocket(conn->sock);
-       conn->sock = -1;
+       conn->sock = PGINVALID_SOCKET;
        /* Discard any unread/unsent data */
        conn->inStart = conn->inCursor = conn->inEnd = 0;
        conn->outCount = 0;
@@ -1631,24 +1631,8 @@ keep_going:                                              /* We will come back to here until there is
                                                   addr_cur->ai_addrlen);
                                        conn->raddr.salen = addr_cur->ai_addrlen;
 
-                                       /* Open a socket */
-                                       {
-                                               /*
-                                                * While we use 'pgsocket' as the socket type in the
-                                                * backend, we use 'int' for libpq socket values.
-                                                * This requires us to map PGINVALID_SOCKET to -1
-                                                * on Windows.
-                                                * See http://msdn.microsoft.com/en-us/library/windows/desktop/ms740516%28v=vs.85%29.aspx
-                                                */
-                                               pgsocket sock = socket(addr_cur->ai_family, SOCK_STREAM, 0);
-#ifdef WIN32
-                                               if (sock == PGINVALID_SOCKET)
-                                                       conn->sock = -1;
-                                               else
-#endif
-                                                       conn->sock = sock;
-                                       }
-                                       if (conn->sock == -1)
+                                       conn->sock = socket(addr_cur->ai_family, SOCK_STREAM, 0);
+                                       if (conn->sock == PGINVALID_SOCKET)
                                        {
                                                /*
                                                 * ignore socket() failure if we have more addresses
@@ -2717,7 +2701,7 @@ makeEmptyPGconn(void)
        conn->client_encoding = PG_SQL_ASCII;
        conn->std_strings = false;      /* unless server says differently */
        conn->verbosity = PQERRORS_DEFAULT;
-       conn->sock = -1;
+       conn->sock = PGINVALID_SOCKET;
        conn->auth_req_received = false;
        conn->password_needed = false;
        conn->dot_pgpass_used = false;
@@ -2882,7 +2866,7 @@ closePGconn(PGconn *conn)
         * Note that the protocol doesn't allow us to send Terminate messages
         * during the startup phase.
         */
-       if (conn->sock >= 0 && conn->status == CONNECTION_OK)
+       if (conn->sock != PGINVALID_SOCKET && conn->status == CONNECTION_OK)
        {
                /*
                 * Try to send "close connection" message to backend. Ignore any
@@ -3103,7 +3087,7 @@ PQgetCancel(PGconn *conn)
        if (!conn)
                return NULL;
 
-       if (conn->sock < 0)
+       if (conn->sock == PGINVALID_SOCKET)
                return NULL;
 
        cancel = malloc(sizeof(PGcancel));
@@ -3284,7 +3268,7 @@ PQrequestCancel(PGconn *conn)
        if (!conn)
                return FALSE;
 
-       if (conn->sock < 0)
+       if (conn->sock == PGINVALID_SOCKET)
        {
                strlcpy(conn->errorMessage.data,
                                "PQrequestCancel() -- connection is not open\n",
@@ -5361,7 +5345,7 @@ PQsocket(const PGconn *conn)
 {
        if (!conn)
                return -1;
-       return conn->sock;
+       return (conn->sock != PGINVALID_SOCKET) ? conn->sock : -1;
 }
 
 int
index 8ccf6d39ee246d9ffa43c2010d5e3c8a8dc8de16..50e4035781ad26b14899e2303f84ff2b88cd5282 100644 (file)
@@ -2549,7 +2549,7 @@ PQfn(PGconn *conn,
        /* clear the error string */
        resetPQExpBuffer(&conn->errorMessage);
 
-       if (conn->sock < 0 || conn->asyncStatus != PGASYNC_IDLE ||
+       if (conn->sock == PGINVALID_SOCKET || conn->asyncStatus != PGASYNC_IDLE ||
                conn->result != NULL)
        {
                printfPQExpBuffer(&conn->errorMessage,
index a7afd42556225477bdb867175032c1f4fbc180fa..cc487b22eec2b0780f4d5b06b5ed3782e1835461 100644 (file)
@@ -604,7 +604,7 @@ pqReadData(PGconn *conn)
        int                     someread = 0;
        int                     nread;
 
-       if (conn->sock < 0)
+       if (conn->sock == PGINVALID_SOCKET)
        {
                printfPQExpBuffer(&conn->errorMessage,
                                                  libpq_gettext("connection not open\n"));
@@ -800,7 +800,7 @@ pqSendSome(PGconn *conn, int len)
        int                     remaining = conn->outCount;
        int                     result = 0;
 
-       if (conn->sock < 0)
+       if (conn->sock == PGINVALID_SOCKET)
        {
                printfPQExpBuffer(&conn->errorMessage,
                                                  libpq_gettext("connection not open\n"));
@@ -1011,7 +1011,7 @@ pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
 
        if (!conn)
                return -1;
-       if (conn->sock < 0)
+       if (conn->sock == PGINVALID_SOCKET)
        {
                printfPQExpBuffer(&conn->errorMessage,
                                                  libpq_gettext("socket not open\n"));
index f3fddaa0364835b384e27868feaa054228b8933a..10510b5bf5a0051ac57ec4db533b1cdec99a9778 100644 (file)
@@ -1211,7 +1211,7 @@ pqGetline2(PGconn *conn, char *s, int maxlen)
 {
        int                     result = 1;             /* return value if buffer overflows */
 
-       if (conn->sock < 0 ||
+       if (conn->sock == PGINVALID_SOCKET ||
                conn->asyncStatus != PGASYNC_COPY_OUT)
        {
                *s = '\0';
index 47cd7f487f0c22d42348c18ce19c1ba631a5d105..d895589148cfc8af37ee4f70c3b8483478c518a6 100644 (file)
@@ -1568,7 +1568,7 @@ pqGetline3(PGconn *conn, char *s, int maxlen)
 {
        int                     status;
 
-       if (conn->sock < 0 ||
+       if (conn->sock == PGINVALID_SOCKET ||
                (conn->asyncStatus != PGASYNC_COPY_OUT &&
                 conn->asyncStatus != PGASYNC_COPY_BOTH) ||
                conn->copy_is_binary)
index ee975d41fa1c94690e60c5fdec87f84b81dc7e3c..0725c17023dbd758d33b633f0075ca931cc973fd 100644 (file)
@@ -365,7 +365,7 @@ struct pg_conn
 
        /* Connection data */
        /* See PQconnectPoll() for how we use 'int' and not 'pgsocket'. */
-       int                     sock;                   /* Unix FD for socket, -1 if not connected */
+       pgsocket        sock;                   /* FD for socket, PGINVALID_SOCKET if unconnected */
        SockAddr        laddr;                  /* Local address */
        SockAddr        raddr;                  /* Remote address */
        ProtocolVersion pversion;       /* FE/BE protocol version in use */