pgsql: pgbench: avoid FD_ISSET on an invalid file descriptor

From: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: pgbench: avoid FD_ISSET on an invalid file descriptor
Date: 2016-02-15 23:37:18
Message-ID: E1aVShi-0006RQ-BE@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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.

Branch
------
REL9_2_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/daac53779acdab512e8347ce7472cf0923cd8598

Modified Files
--------------
contrib/pgbench/pgbench.c | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Andres Freund 2016-02-16 00:18:08 pgsql: Allow the WAL writer to flush WAL at a reduced rate.
Previous Message Andres Freund 2016-02-15 22:23:57 Re: pgsql: Suppress compiler warnings about useless comparison of unsigned