postgres.git / summary / log / commit / refs
commit 0b2aefe74afaf418ca74f31fc3ef39f2a3cd336a
Author: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Mon Feb 15 23:33:43 2016 +0000
First released in: 9.6.0, 9.1.21, 9.2.16, 9.3.12, 9.4.7, 9.5.2
pgbench: avoid FD_ISSET on an invalid file descriptor
The original code wasn't careful to test the file descriptor returned by
PQsocket() for an invalid socket. If an invalid socket did turn up,
that would amount to calling FD_ISSET with fd = -1, whereby undefined
behavior can be invoked.
To fix, test file descriptor for validity and stop further processing if
that fails.
Problem noticed by Coverity.
There is an existing FD_ISSET callsite that does check for invalid
sockets beforehand, but the error message reported by it was
strerror(errno); in testing the aforementioned change, that turns out to
result in "bad socket: Success" which isn't terribly helpful. Instead
use PQerrorMessage() in both places which is more likely to contain an
useful error message.
Backpatch-through: 9.1.
contrib/pgbench/pgbench.c | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)
diff --git a/contrib/pgbench/pgbench.c b/contrib/pgbench/pgbench.c
index 035024d6f58..9e58779cc46 100644
--- a/contrib/pgbench/pgbench.c
+++ b/contrib/pgbench/pgbench.c
@@ -2398,7 +2398,7 @@ threadRun(void *arg)
sock = PQsocket(st->con);
if (sock < 0)
{
- fprintf(stderr, "bad socket: %s\n", strerror(errno));
+ fprintf(stderr, "bad socket: %s", PQerrorMessage(st->con));
goto done;
}
@@ -2439,11 +2439,21 @@ threadRun(void *arg)
Command **commands = sql_files[st->use_file];
int prev_ecnt = st->ecnt;
- if (st->con && (FD_ISSET(PQsocket(st->con), &input_mask)
- || commands[st->state]->type == META_COMMAND))
+ if (st->con)
{
- if (!doCustom(thread, st, &result->conn_time, logfile))
- remains--; /* I've aborted */
+ int sock = PQsocket(st->con);
+
+ if (sock < 0)
+ {
+ fprintf(stderr, "bad socket: %s", PQerrorMessage(st->con));
+ goto done;
+ }
+ if (FD_ISSET(sock, &input_mask) ||
+ commands[st->state]->type == META_COMMAND)
+ {
+ if (!doCustom(thread, st, &result->conn_time, logfile))
+ remains--; /* I've aborted */
+ }
}
if (st->ecnt > prev_ecnt && commands[st->state]->type == META_COMMAND)
[parent: 1f52d23ef256]