Skip to content

Commit 1bad289

Browse files
committed
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.
1 parent d2ab9b0 commit 1bad289

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/bin/pgbench/pgbench.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3683,7 +3683,7 @@ threadRun(void *arg)
36833683
sock = PQsocket(st->con);
36843684
if (sock < 0)
36853685
{
3686-
fprintf(stderr, "bad socket: %s\n", strerror(errno));
3686+
fprintf(stderr, "bad socket: %s", PQerrorMessage(st->con));
36873687
goto done;
36883688
}
36893689

@@ -3751,11 +3751,21 @@ threadRun(void *arg)
37513751
Command **commands = sql_files[st->use_file];
37523752
int prev_ecnt = st->ecnt;
37533753

3754-
if (st->con && (FD_ISSET(PQsocket(st->con), &input_mask)
3755-
|| commands[st->state]->type == META_COMMAND))
3754+
if (st->con)
37563755
{
3757-
if (!doCustom(thread, st, &result->conn_time, logfile, &aggs))
3758-
remains--; /* I've aborted */
3756+
int sock = PQsocket(st->con);
3757+
3758+
if (sock < 0)
3759+
{
3760+
fprintf(stderr, "bad socket: %s", PQerrorMessage(st->con));
3761+
goto done;
3762+
}
3763+
if (FD_ISSET(sock, &input_mask) ||
3764+
commands[st->state]->type == META_COMMAND)
3765+
{
3766+
if (!doCustom(thread, st, &result->conn_time, logfile, &aggs))
3767+
remains--; /* I've aborted */
3768+
}
37593769
}
37603770

37613771
if (st->ecnt > prev_ecnt && commands[st->state]->type == META_COMMAND)

0 commit comments

Comments
 (0)