Skip to content

Commit 431b638

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 20139f4 commit 431b638

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,6 +1911,9 @@ PQconnectPoll(PGconn *conn)
19111911
closesocket(conn->sock);
19121912
conn->sock = -1;
19131913
conn->status = CONNECTION_NEEDED;
1914+
/* Discard any unread/unsent data */
1915+
conn->inStart = conn->inCursor = conn->inEnd = 0;
1916+
conn->outCount = 0;
19141917
goto keep_going;
19151918
}
19161919
else
@@ -1948,6 +1951,9 @@ PQconnectPoll(PGconn *conn)
19481951
closesocket(conn->sock);
19491952
conn->sock = -1;
19501953
conn->status = CONNECTION_NEEDED;
1954+
/* Discard any unread/unsent data */
1955+
conn->inStart = conn->inCursor = conn->inEnd = 0;
1956+
conn->outCount = 0;
19511957
goto keep_going;
19521958
}
19531959
}
@@ -2061,6 +2067,9 @@ PQconnectPoll(PGconn *conn)
20612067
closesocket(conn->sock);
20622068
conn->sock = -1;
20632069
conn->status = CONNECTION_NEEDED;
2070+
/* Discard any unread/unsent data */
2071+
conn->inStart = conn->inCursor = conn->inEnd = 0;
2072+
conn->outCount = 0;
20642073
goto keep_going;
20652074
}
20662075

@@ -2128,6 +2137,9 @@ PQconnectPoll(PGconn *conn)
21282137
closesocket(conn->sock);
21292138
conn->sock = -1;
21302139
conn->status = CONNECTION_NEEDED;
2140+
/* Discard any unread/unsent data */
2141+
conn->inStart = conn->inCursor = conn->inEnd = 0;
2142+
conn->outCount = 0;
21312143
goto keep_going;
21322144
}
21332145

@@ -2147,6 +2159,9 @@ PQconnectPoll(PGconn *conn)
21472159
closesocket(conn->sock);
21482160
conn->sock = -1;
21492161
conn->status = CONNECTION_NEEDED;
2162+
/* Discard any unread/unsent data */
2163+
conn->inStart = conn->inCursor = conn->inEnd = 0;
2164+
conn->outCount = 0;
21502165
goto keep_going;
21512166
}
21522167
#endif
@@ -2308,6 +2323,9 @@ PQconnectPoll(PGconn *conn)
23082323
closesocket(conn->sock);
23092324
conn->sock = -1;
23102325
conn->status = CONNECTION_NEEDED;
2326+
/* Discard any unread/unsent data */
2327+
conn->inStart = conn->inCursor = conn->inEnd = 0;
2328+
conn->outCount = 0;
23112329
goto keep_going;
23122330
}
23132331
}

0 commit comments

Comments
 (0)