From 77e4fd5c4a500a4e6b24076c83bee17f55690831 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 24 Jul 2011 16:29:18 -0400 Subject: Improve libpq's error reporting for SSL failures. In many cases, pqsecure_read/pqsecure_write set up useful error messages, which were then overwritten with useless ones by their callers. Fix this by defining the responsibility to set an error message to be entirely that of the lower-level function when using SSL. Back-patch to 8.3; the code is too different in 8.2 to be worth the trouble. --- src/interfaces/libpq/fe-misc.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'src/interfaces/libpq/fe-misc.c') diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c index 0b189453dda..0c5c90ed895 100644 --- a/src/interfaces/libpq/fe-misc.c +++ b/src/interfaces/libpq/fe-misc.c @@ -639,7 +639,9 @@ retry3: if (SOCK_ERRNO == ECONNRESET) goto definitelyFailed; #endif - printfPQExpBuffer(&conn->errorMessage, + /* in SSL mode, pqsecure_read set the error message */ + if (conn->ssl == NULL) + printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not receive data from server: %s\n"), SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); return -1; @@ -729,7 +731,9 @@ retry4: if (SOCK_ERRNO == ECONNRESET) goto definitelyFailed; #endif - printfPQExpBuffer(&conn->errorMessage, + /* in SSL mode, pqsecure_read set the error message */ + if (conn->ssl == NULL) + printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not receive data from server: %s\n"), SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); return -1; @@ -745,8 +749,10 @@ retry4: * means the connection has been closed. Cope. */ definitelyFailed: - printfPQExpBuffer(&conn->errorMessage, - libpq_gettext( + /* in SSL mode, pqsecure_read set the error message */ + if (conn->ssl == NULL) + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext( "server closed the connection unexpectedly\n" "\tThis probably means the server terminated abnormally\n" "\tbefore or while processing the request.\n")); @@ -823,8 +829,10 @@ pqSendSome(PGconn *conn, int len) #ifdef ECONNRESET case ECONNRESET: #endif - printfPQExpBuffer(&conn->errorMessage, - libpq_gettext( + /* in SSL mode, pqsecure_write set the error message */ + if (conn->ssl == NULL) + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext( "server closed the connection unexpectedly\n" "\tThis probably means the server terminated abnormally\n" "\tbefore or while processing the request.\n")); @@ -841,7 +849,9 @@ pqSendSome(PGconn *conn, int len) return -1; default: - printfPQExpBuffer(&conn->errorMessage, + /* in SSL mode, pqsecure_write set the error message */ + if (conn->ssl == NULL) + printfPQExpBuffer(&conn->errorMessage, libpq_gettext("could not send data to server: %s\n"), SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf))); /* We don't assume it's a fatal error... */ -- cgit v1.2.3