10000 pgbench: avoid FD_ISSET on an invalid file descriptor · prmdeveloper/postgres@0b2aefe · GitHub
[go: up one dir, main page]

Skip to content

Commit 0b2aefe

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 1f52d23 commit 0b2aefe

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

contrib/pgbench/pgbench.c

Lines changed: 15 additions & 5 deletions
< 10760 tbody>
Original file line numberDiff line numberDiff line change
@@ -2398,7 +2398,7 @@ threadRun(void *arg)
23982398
sock = PQsocket(st->con);
23992399
if (sock < 0)
24002400
{
2401-
fprintf(stderr, "bad socket: %s\n", strerror(errno));
2401+
fprintf(stderr, "bad socket: %s", PQerrorMessage(st->con));
24022402
goto done;
24032403
}
24042404

@@ -2439,11 +2439,21 @@ threadRun(void *arg)
24392439
Command **commands = sql_files[st->use_file];
24402440
int prev_ecnt = st->ecnt;
24412441

2442-
if (st->con && (FD_ISSET(PQsocket(st->con), &input_mask)
2443-
|| commands[st->state]->type == META_COMMAND))
2442+
if (st->con)
24442443
{
2445-
if (!doCustom(thread, st, &result->conn_time, logfile))
2446-
remains--; /* I've aborted */
2444+
int sock = PQsocket(st->con);
2445+
2446+
if (sock < 0)
2447+
{
2448+
fprintf(stderr, "bad socket: %s", PQerrorMessage(st->con));
2449+
goto done;
2450+
}
2451+
if (FD_ISSET(sock, &input_mask) ||
2452+
commands[st->state]->type == META_COMMAND)
2453+
{
2454+
if (!doCustom(thread, st, &result->conn_time, logfile))
2455+
remains--; /* I've aborted */
2456+
}
24472457
}
24482458

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

0 commit comments

Comments
 (0)
0