Skip to content

Commit bb42ad1

Browse files
committed
Ensure we discard unread/unsent data when abandoning a connection attempt.
There are assorted situations wherein PQconnectPoll() will abandon a connection attempt and try again with different parameters (eg, SSL versus not SSL). However, the code forgot to discard any pending data in libpq's I/O buffers when doing this. In at least one case (server returns E message during SSL negotiation), there is unread input data which bollixes the next connection attempt. I have not checked to see whether this is possible in the other cases where we close the socket and retry, but it seems like a matter of good defensive programming to add explicit buffer-flushing code to all of them. This is one of several issues exposed by Daniel Farina's report of misbehavior after a server-side fork failure. This has been wrong since forever, so back-patch to all supported branches.
1 parent ae42744 commit bb42ad1

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/interfaces/libpq/fe-connect.c

+15
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,9 @@ PQconnectPoll(PGconn *conn)
13791379
closesocket(conn->sock);
13801380
conn->sock = -1;
13811381
conn->status = CONNECTION_NEEDED;
1382+
/* Discard any unread/unsent data */
1383+
conn->inStart = conn->inCursor = conn->inEnd = 0;
1384+
conn->outCount = 0;
13821385
goto keep_going;
13831386
}
13841387
else
@@ -1416,6 +1419,9 @@ PQconnectPoll(PGconn *conn)
14161419
closesocket(conn->sock);
14171420
conn->sock = -1;
14181421
conn->status = CONNECTION_NEEDED;
1422+
/* Discard any unread/unsent data */
1423+
conn->inStart = conn->inCursor = conn->inEnd = 0;
1424+
conn->outCount = 0;
14191425
goto keep_going;
14201426
}
14211427
}
@@ -1528,6 +1534,9 @@ PQconnectPoll(PGconn *conn)
15281534
closesocket(conn->sock);
15291535
conn->sock = -1;
15301536
conn->status = CONNECTION_NEEDED;
1537+
/* Discard any unread/unsent data */
1538+
conn->inStart = conn->inCursor = conn->inEnd = 0;
1539+
conn->outCount = 0;
15311540
goto keep_going;
15321541
}
15331542

@@ -1594,6 +1603,9 @@ PQconnectPoll(PGconn *conn)
15941603
closesocket(conn->sock);
15951604
conn->sock = -1;
15961605
conn->status = CONNECTION_NEEDED;
1606+
/* Discard any unread/unsent data */
1607+
conn->inStart = conn->inCursor = conn->inEnd = 0;
1608+
conn->outCount = 0;
15971609
goto keep_going;
15981610
}
15991611

@@ -1613,6 +1625,9 @@ PQconnectPoll(PGconn *conn)
16131625
closesocket(conn->sock);
16141626
conn->sock = -1;
16151627
conn->status = CONNECTION_NEEDED;
1628+
/* Discard any unread/unsent data */
1629+
conn->inStart = conn->inCursor = conn->inEnd = 0;
1630+
conn->outCount = 0;
16161631
goto keep_going;
16171632
}
16181633
#endif

0 commit comments

Comments
 (0)