pgbench: avoid FD_ISSET on an invalid file descriptor
authorAlvaro Herrera
Mon, 15 Feb 2016 23:33:43 +0000 (20:33 -0300)
committerAlvaro Herrera
Mon, 15 Feb 2016 23:33:43 +0000 (20:33 -0300)
commitccbb01ff4a4cac8622930ef6475ed3cf8281e4c7
tree69a1f12a3ce01e02877902b7d8467fd6ebd3917c
parente3ad502740e818335a930c38528b747edebf3684
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