Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for malloc failure in libpq calls #5446

Merged
merged 1 commit into from Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,7 @@ accidentally triggering the load of a previous DB version.**
* #5233 Out of on_proc_exit slots on guc license change
* #5427 Handle user-defined FDW options properly
* #5442 Decompression may have lost DEFAULT values
* #5446 Add checks for malloc failure in libpq calls

**Thanks**
* @nikolaps for reporting an issue with the COPY fetcher
Expand Down
8 changes: 8 additions & 0 deletions src/utils.h
Expand Up @@ -40,6 +40,14 @@
/* find the length of a statically sized array */
#define TS_ARRAY_LEN(array) (sizeof(array) / sizeof(*array))

/* Use condition to check if out of memory */
#define TS_OOM_CHECK(COND, FMT, ...) \
do \
{ \
if (!(COND)) \
ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg(FMT, ##__VA_ARGS__))); \
} while (0)

extern TSDLLEXPORT bool ts_type_is_int8_binary_compatible(Oid sourcetype);

typedef enum TimevalInfinity
Expand Down
5 changes: 3 additions & 2 deletions tsl/src/remote/connection.c
Expand Up @@ -546,7 +546,7 @@
PQconninfoOption *lopt;
PQconninfoOption *options = PQconndefaults();

Assert(options != NULL);
TS_OOM_CHECK(options, "out of memory");

/* Explicitly unset all libpq environment variables.
*
Expand Down Expand Up @@ -1024,6 +1024,7 @@
if (PQconsumeInput(conn->pg_conn) == 0)
{
pgres = PQmakeEmptyPGresult(conn->pg_conn, PGRES_FATAL_ERROR);
TS_OOM_CHECK(pgres, "out of memory");
PQfireResultCreateEvents(conn->pg_conn, pgres);
return pgres;
}
Expand Down Expand Up @@ -1102,6 +1103,7 @@
if (ret == 0)
{
res = PQmakeEmptyPGresult(conn->pg_conn, PGRES_FATAL_ERROR);
TS_OOM_CHECK(res, "out of memory");

Check warning on line 1106 in tsl/src/remote/connection.c

View check run for this annotation

Codecov / codecov/patch

tsl/src/remote/connection.c#L1106

Added line #L1106 was not covered by tests
PQfireResultCreateEvents(conn->pg_conn, res);
return res;
}
Expand Down Expand Up @@ -2522,7 +2524,6 @@
}
}

Assert(res == NULL);
remote_connection_set_status(conn, CONN_IDLE);

return success;
Expand Down