mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-09-05 22:19:20 +02:00
Improve handling of out-of-memory in libpq.
If an allocation fails in the main message handling loop, pqParseInput3 or pqParseInput2, it should not be treated as "not enough data available yet". Otherwise libpq will wait indefinitely for more data to arrive from the server, and gets stuck forever. This isn't a complete fix - getParamDescriptions and getCopyStart still have the same issue, but it's a step in the right direction. Michael Paquier and me. Backpatch to all supported versions.
This commit is contained in:
parent
162ae5b9bb
commit
28c38396ed
@ -498,10 +498,17 @@ pqParseInput2(PGconn *conn)
|
||||
conn->result = PQmakeEmptyPGresult(conn,
|
||||
PGRES_COMMAND_OK);
|
||||
if (!conn->result)
|
||||
return;
|
||||
{
|
||||
printfPQExpBuffer(&conn->errorMessage,
|
||||
libpq_gettext("out of memory"));
|
||||
pqSaveErrorResult(conn);
|
||||
}
|
||||
}
|
||||
if (conn->result)
|
||||
{
|
||||
strlcpy(conn->result->cmdStatus, conn->workBuffer.data,
|
||||
CMDSTATUS_LEN);
|
||||
}
|
||||
checkXactStatus(conn, conn->workBuffer.data);
|
||||
conn->asyncStatus = PGASYNC_READY;
|
||||
break;
|
||||
@ -522,8 +529,16 @@ pqParseInput2(PGconn *conn)
|
||||
"unexpected character %c following empty query response (\"I\" message)",
|
||||
id);
|
||||
if (conn->result == NULL)
|
||||
{
|
||||
conn->result = PQmakeEmptyPGresult(conn,
|
||||
PGRES_EMPTY_QUERY);
|
||||
if (!conn->result)
|
||||
{
|
||||
printfPQExpBuffer(&conn->errorMessage,
|
||||
libpq_gettext("out of memory"));
|
||||
pqSaveErrorResult(conn);
|
||||
}
|
||||
}
|
||||
conn->asyncStatus = PGASYNC_READY;
|
||||
break;
|
||||
case 'K': /* secret key data from the backend */
|
||||
@ -965,14 +980,17 @@ pqGetErrorNotice2(PGconn *conn, bool isError)
|
||||
* Make a PGresult to hold the message. We temporarily lie about the
|
||||
* result status, so that PQmakeEmptyPGresult doesn't uselessly copy
|
||||
* conn->errorMessage.
|
||||
*
|
||||
* NB: This allocation can fail, if you run out of memory. The rest of the
|
||||
* function handles that gracefully, and we still try to set the error
|
||||
* message as the connection's error message.
|
||||
*/
|
||||
res = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY);
|
||||
if (!res)
|
||||
goto failure;
|
||||
if (res)
|
||||
{
|
||||
res->resultStatus = isError ? PGRES_FATAL_ERROR : PGRES_NONFATAL_ERROR;
|
||||
res->errMsg = pqResultStrdup(res, workBuf.data);
|
||||
if (!res->errMsg)
|
||||
goto failure;
|
||||
}
|
||||
|
||||
/*
|
||||
* Break the message into fields. We can't do very much here, but we can
|
||||
@ -1024,16 +1042,23 @@ pqGetErrorNotice2(PGconn *conn, bool isError)
|
||||
pqClearAsyncResult(conn);
|
||||
conn->result = res;
|
||||
resetPQExpBuffer(&conn->errorMessage);
|
||||
if (res && !PQExpBufferDataBroken(workBuf) && res->errMsg)
|
||||
appendPQExpBufferStr(&conn->errorMessage, res->errMsg);
|
||||
else
|
||||
printfPQExpBuffer(&conn->errorMessage,
|
||||
libpq_gettext("out of memory"));
|
||||
if (conn->xactStatus == PQTRANS_INTRANS)
|
||||
conn->xactStatus = PQTRANS_INERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (res)
|
||||
{
|
||||
if (res->noticeHooks.noticeRec != NULL)
|
||||
(*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res);
|
||||
PQclear(res);
|
||||
}
|
||||
}
|
||||
|
||||
termPQExpBuffer(&workBuf);
|
||||
return 0;
|
||||
|
@ -204,8 +204,13 @@ pqParseInput3(PGconn *conn)
|
||||
conn->result = PQmakeEmptyPGresult(conn,
|
||||
PGRES_COMMAND_OK);
|
||||
if (!conn->result)
|
||||
return;
|
||||
{
|
||||
printfPQExpBuffer(&conn->errorMessage,
|
||||
libpq_gettext("out of memory"));
|
||||
pqSaveErrorResult(conn);
|
||||
}
|
||||
}
|
||||
if (conn->result)
|
||||
strlcpy(conn->result->cmdStatus, conn->workBuffer.data,
|
||||
CMDSTATUS_LEN);
|
||||
conn->asyncStatus = PGASYNC_READY;
|
||||
@ -226,7 +231,11 @@ pqParseInput3(PGconn *conn)
|
||||
conn->result = PQmakeEmptyPGresult(conn,
|
||||
PGRES_EMPTY_QUERY);
|
||||
if (!conn->result)
|
||||
return;
|
||||
{
|
||||
printfPQExpBuffer(&conn->errorMessage,
|
||||
libpq_gettext("out of memory"));
|
||||
pqSaveErrorResult(conn);
|
||||
}
|
||||
}
|
||||
conn->asyncStatus = PGASYNC_READY;
|
||||
break;
|
||||
@ -239,7 +248,11 @@ pqParseInput3(PGconn *conn)
|
||||
conn->result = PQmakeEmptyPGresult(conn,
|
||||
PGRES_COMMAND_OK);
|
||||
if (!conn->result)
|
||||
return;
|
||||
{
|
||||
printfPQExpBuffer(&conn->errorMessage,
|
||||
libpq_gettext("out of memory"));
|
||||
pqSaveErrorResult(conn);
|
||||
}
|
||||
}
|
||||
conn->asyncStatus = PGASYNC_READY;
|
||||
}
|
||||
@ -306,7 +319,11 @@ pqParseInput3(PGconn *conn)
|
||||
conn->result = PQmakeEmptyPGresult(conn,
|
||||
PGRES_COMMAND_OK);
|
||||
if (!conn->result)
|
||||
return;
|
||||
{
|
||||
printfPQExpBuffer(&conn->errorMessage,
|
||||
libpq_gettext("out of memory"));
|
||||
pqSaveErrorResult(conn);
|
||||
}
|
||||
}
|
||||
conn->asyncStatus = PGASYNC_READY;
|
||||
}
|
||||
@ -822,10 +839,13 @@ pqGetErrorNotice3(PGconn *conn, bool isError)
|
||||
* Make a PGresult to hold the accumulated fields. We temporarily lie
|
||||
* about the result status, so that PQmakeEmptyPGresult doesn't uselessly
|
||||
* copy conn->errorMessage.
|
||||
*
|
||||
* NB: This allocation can fail, if you run out of memory. The rest of the
|
||||
* function handles that gracefully, and we still try to set the error
|
||||
* message as the connection's error message.
|
||||
*/
|
||||
res = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY);
|
||||
if (!res)
|
||||
goto fail;
|
||||
if (res)
|
||||
res->resultStatus = isError ? PGRES_FATAL_ERROR : PGRES_NONFATAL_ERROR;
|
||||
|
||||
/*
|
||||
@ -966,14 +986,20 @@ pqGetErrorNotice3(PGconn *conn, bool isError)
|
||||
*/
|
||||
if (isError)
|
||||
{
|
||||
if (res)
|
||||
res->errMsg = pqResultStrdup(res, workBuf.data);
|
||||
if (!res->errMsg)
|
||||
goto fail;
|
||||
pqClearAsyncResult(conn);
|
||||
conn->result = res;
|
||||
if (PQExpBufferDataBroken(workBuf))
|
||||
printfPQExpBuffer(&conn->errorMessage,
|
||||
libpq_gettext("out of memory"));
|
||||
else
|
||||
appendPQExpBufferStr(&conn->errorMessage, workBuf.data);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* if we couldn't allocate the result set, just discard the NOTICE */
|
||||
if (res)
|
||||
{
|
||||
/* We can cheat a little here and not copy the message. */
|
||||
res->errMsg = workBuf.data;
|
||||
@ -981,6 +1007,7 @@ pqGetErrorNotice3(PGconn *conn, bool isError)
|
||||
(*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res);
|
||||
PQclear(res);
|
||||
}
|
||||
}
|
||||
|
||||
termPQExpBuffer(&workBuf);
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user