Skip to content

Commit 849a611

Browse files
hlinnakaCommitfest Bot
authored andcommitted
libpq: Don't hang on out-of-memory
If an allocation failed while handling an async notification, we returned EOF, which stopped processing any further data until more data was received from the server. If more data never arrives, e.g. because the connection was used just to wait for the notification, or because the next ReadyForQuery was already received and buffered, it would get stuck forever. Instead, silently ignore the notification. Silently ignoring the notification is not a great way to handle the situation, but at least the connection doesn't get stuck, and it's consistent with how the malloc() later in the function is handled, and with e.g. how pqSaveParameterStatus() handles allocation failures. Fix the same issue with OOM on receiving BackendKeyData message. That one is new in v18. Discussion: https://www.postgresql.org/message-id/[email protected]
1 parent 16a0039 commit 849a611

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/interfaces/libpq/fe-protocol3.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,9 +1550,8 @@ getBackendKeyData(PGconn *conn, int msgLength)
15501550
conn->be_cancel_key = malloc(cancel_key_len);
15511551
if (conn->be_cancel_key == NULL)
15521552
{
1553-
libpq_append_conn_error(conn, "out of memory");
1554-
/* discard the message */
1555-
return EOF;
1553+
/* continue without cancel key */
1554+
return 0;
15561555
}
15571556
if (pqGetnchar(conn->be_cancel_key, cancel_key_len, conn))
15581557
{
@@ -1588,14 +1587,19 @@ getNotify(PGconn *conn)
15881587
return EOF;
15891588
/* must save name while getting extra string */
15901589
svname = strdup(conn->workBuffer.data);
1591-
if (!svname)
1592-
return EOF;
15931590
if (pqGets(&conn->workBuffer, conn))
15941591
{
1595-
free(svname);
1592+
if (svname)
1593+
free(svname);
15961594
return EOF;
15971595
}
15981596

1597+
if (!svname)
1598+
{
1599+
/* out of memory; silently ignore the notification */
1600+
return 0;
1601+
}
1602+
15991603
/*
16001604
* Store the strings right after the PGnotify structure so it can all be
16011605
* freed at once. We don't use NAMEDATALEN because we don't want to tie

0 commit comments

Comments
 (0)