Add missing bad-PGconn guards in libpq entry points.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 15 Aug 2022 19:40:07 +0000 (15:40 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 15 Aug 2022 19:40:07 +0000 (15:40 -0400)
There's a convention that externally-visible libpq functions should
check for a NULL PGconn pointer, and fail gracefully instead of
crashing.  PQflush() and PQisnonblocking() didn't get that memo
though.  Also add a similar check to PQdefaultSSLKeyPassHook_OpenSSL;
while it's not clear that ordinary usage could reach that with a
null conn pointer, it's cheap enough to check, so let's be consistent.

Daniele Varrazzo and Tom Lane

Discussion: https://postgr.es/m/CA+mi_8Zm_mVVyW1iNFgyMd9Oh0Nv8-F+7Y3-BqwMgTMHuo_h2Q@mail.gmail.com

src/interfaces/libpq/fe-exec.c
src/interfaces/libpq/fe-secure-openssl.c

index a36f5eb310d86abc6587660154d7ce6055da867b..bb874f7f5095d889a2f96a71a26bf721db3edab8 100644 (file)
@@ -3917,6 +3917,8 @@ PQsetnonblocking(PGconn *conn, int arg)
 int
 PQisnonblocking(const PGconn *conn)
 {
+   if (!conn || conn->status == CONNECTION_BAD)
+       return false;
    return pqIsnonblocking(conn);
 }
 
@@ -3936,6 +3938,8 @@ PQisthreadsafe(void)
 int
 PQflush(PGconn *conn)
 {
+   if (!conn || conn->status == CONNECTION_BAD)
+       return -1;
    return pqFlush(conn);
 }
 
index 8117cbd40f1b25a6e3c71fe20d5ec4f86f04f80d..3cc75380e0296e8821b96cac0687aab39c4a9a3f 100644 (file)
@@ -1938,7 +1938,7 @@ err:
 int
 PQdefaultSSLKeyPassHook_OpenSSL(char *buf, int size, PGconn *conn)
 {
-   if (conn->sslpassword)
+   if (conn && conn->sslpassword)
    {
        if (strlen(conn->sslpassword) + 1 > size)
            fprintf(stderr, libpq_gettext("WARNING: sslpassword truncated\n"));