Skip to content

Commit 65c033c

Browse files
committed
Fix previous patch so it also works if not USE_SSL (mea culpa).
On balance, the need to cover this case changes my mind in favor of pushing all error-message generation duties into the two fe-secure.c routines. So do it that way.
1 parent 77e4fd5 commit 65c033c

File tree

2 files changed

+170
-98
lines changed

2 files changed

+170
-98
lines changed

src/interfaces/libpq/fe-misc.c

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,6 @@ pqReadData(PGconn *conn)
570570
{
571571
int someread = 0;
572572
int nread;
573-
char sebuf[256];
574573

575574
if (conn->sock < 0)
576575
{
@@ -639,11 +638,7 @@ pqReadData(PGconn *conn)
639638
if (SOCK_ERRNO == ECONNRESET)
640639
goto definitelyFailed;
641640
#endif
642-
/* in SSL mode, pqsecure_read set the error message */
643-
if (conn->ssl == NULL)
644-
printfPQExpBuffer(&conn->errorMessage,
645-
libpq_gettext("could not receive data from server: %s\n"),
646-
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
641+
/* pqsecure_read set the error message for us */
647642
return -1;
648643
}
649644
if (nread > 0)
@@ -703,6 +698,11 @@ pqReadData(PGconn *conn)
703698
/* ready for read */
704699
break;
705700
default:
701+
printfPQExpBuffer(&conn->errorMessage,
702+
libpq_gettext(
703+
"server closed the connection unexpectedly\n"
704+
"\tThis probably means the server terminated abnormally\n"
705+
"\tbefore or while processing the request.\n"));
706706
goto definitelyFailed;
707707
}
708708

@@ -731,11 +731,7 @@ pqReadData(PGconn *conn)
731731
if (SOCK_ERRNO == ECONNRESET)
732732
goto definitelyFailed;
733733
#endif
734-
/* in SSL mode, pqsecure_read set the error message */
735-
if (conn->ssl == NULL)
736-
printfPQExpBuffer(&conn->errorMessage,
737-
libpq_gettext("could not receive data from server: %s\n"),
738-
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
734+
/* pqsecure_read set the error message for us */
739735
return -1;
740736
}
741737
if (nread > 0)
@@ -746,16 +742,10 @@ pqReadData(PGconn *conn)
746742

747743
/*
748744
* OK, we are getting a zero read even though select() says ready. This
749-
* means the connection has been closed. Cope.
745+
* means the connection has been closed. Cope. Note that errorMessage
746+
* has been set already.
750747
*/
751748
definitelyFailed:
752-
/* in SSL mode, pqsecure_read set the error message */
753-
if (conn->ssl == NULL)
754-
printfPQExpBuffer(&conn->errorMessage,
755-
libpq_gettext(
756-
"server closed the connection unexpectedly\n"
757-
"\tThis probably means the server terminated abnormally\n"
758-
"\tbefore or while processing the request.\n"));
759749
conn->status = CONNECTION_BAD; /* No more connection to backend */
760750
pqsecure_close(conn);
761751
closesocket(conn->sock);
@@ -791,7 +781,6 @@ pqSendSome(PGconn *conn, int len)
791781
while (len > 0)
792782
{
793783
int sent;
794-
char sebuf[256];
795784

796785
#ifndef WIN32
797786
sent = pqsecure_write(conn, ptr, len);
@@ -807,11 +796,7 @@ pqSendSome(PGconn *conn, int len)
807796

808797
if (sent < 0)
809798
{
810-
/*
811-
* Anything except EAGAIN/EWOULDBLOCK/EINTR is trouble. If it's
812-
* EPIPE or ECONNRESET, assume we've lost the backend connection
813-
* permanently.
814-
*/
799+
/* Anything except EAGAIN/EWOULDBLOCK/EINTR is trouble */
815800
switch (SOCK_ERRNO)
816801
{
817802
#ifdef EAGAIN
@@ -825,17 +810,8 @@ pqSendSome(PGconn *conn, int len)
825810
case EINTR:
826811
continue;
827812

828-
case EPIPE:
829-
#ifdef ECONNRESET
830-
case ECONNRESET:
831-
#endif
832-
/* in SSL mode, pqsecure_write set the error message */
833-
if (conn->ssl == NULL)
834-
printfPQExpBuffer(&conn->errorMessage,
835-
libpq_gettext(
836-
"server closed the connection unexpectedly\n"
837-
"\tThis probably means the server terminated abnormally\n"
838-
"\tbefore or while processing the request.\n"));
813+
default:
814+
/* pqsecure_write set the error message for us */
839815

840816
/*
841817
* We used to close the socket here, but that's a bad idea
@@ -847,16 +823,6 @@ pqSendSome(PGconn *conn, int len)
847823
*/
848824
conn->outCount = 0;
849825
return -1;
850-
851-
default:
852-
/* in SSL mode, pqsecure_write set the error message */
853-
if (conn->ssl == NULL)
854-
printfPQExpBuffer(&conn->errorMessage,
855-
libpq_gettext("could not send data to server: %s\n"),
856-
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
857-
/* We don't assume it's a fatal error... */
858-
conn->outCount = 0;
859-
return -1;
860826
}
861827
}
862828
else

0 commit comments

Comments
 (0)