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

Skip to content

Commit baa7f63

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 e1df791 commit baa7f63

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
Original file line numberDiff line numberDiff line change
@@ -2845,7 +2845,7 @@ threadRun(void *arg)
28452845
sock = PQsocket(st->con);
28462846
if (sock < 0)
28472847
{
2848-
fprintf(stderr, "bad socket: %s\n", strerror(errno));
2848+
fprintf(stderr, "bad socket: %s", PQerrorMessage(st->con));
28492849
goto done;
28502850
}
28512851

@@ -2886,11 +2886,21 @@ threadRun(void *arg)
28862886
Command **commands = sql_files[st->use_file];
28872887
int prev_ecnt = st->ecnt;
28882888

2889-
if (st->con && (FD_ISSET(PQsocket(st->con), &input_mask)
2890-
|| commands[st->state]->type == META_COMMAND))
2889+
if (st->con)
28912890
{
2892-
if (!doCustom(thread, st, &result->conn_time, logfile, &aggs))
2893-
remains--; /* I've aborted */
2891+
int sock = PQsocket(st->con);
2892+
2893+
if (sock < 0)
2894+
{
2895+
fprintf(stderr, "bad socket: %s", PQerrorMessage(st->con));
2896+
goto done;
2897+
}
2898+
if (FD_ISSET(sock, &input_mask) ||
2899+
commands[st->state]->type == META_COMMAND)
2900+
{
2901+
if (!doCustom(thread, st, &result->conn_time, logfile, &aggs))
2902+
remains--; /* I've aborted */
2903+
}
28942904
}
28952905

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

0 commit comments

Comments
 (0)
0