Skip to content

Commit d63a691

Browse files
committed
Add missing bad-PGconn guards in libpq entry points.
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
1 parent 63b64d8 commit d63a691

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/interfaces/libpq/fe-exec.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3829,6 +3829,8 @@ PQsetnonblocking(PGconn *conn, int arg)
38293829
int
38303830
PQisnonblocking(const PGconn *conn)
38313831
{
3832+
if (!conn || conn->status == CONNECTION_BAD)
3833+
return false;
38323834
return pqIsnonblocking(conn);
38333835
}
38343836

@@ -3848,6 +3850,8 @@ PQisthreadsafe(void)
38483850
int
38493851
PQflush(PGconn *conn)
38503852
{
3853+
if (!conn || conn->status == CONNECTION_BAD)
3854+
return -1;
38513855
return pqFlush(conn);
38523856
}
38533857

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1806,7 +1806,7 @@ my_SSL_set_fd(PGconn *conn, int fd)
18061806
int
18071807
PQdefaultSSLKeyPassHook_OpenSSL(char *buf, int size, PGconn *conn)
18081808
{
1809-
if (conn->sslpassword)
1809+
if (conn && conn->sslpassword)
18101810
{
18111811
if (strlen(conn->sslpassword) + 1 > size)
18121812
fprintf(stderr, libpq_gettext("WARNING: sslpassword truncated\n"));

0 commit comments

Comments
 (0)