Hi Roman,

Thanks for the patch. I took a look at v2.

What I checked
--------------
- Applies cleanly on current master (git am, no fuzz/offset) at
  207b5293b8d.
- Builds with --enable-debug --enable-cassert --enable-tap-tests; no new
  compiler warnings.
- "make check" in src/test/regress is green (All 243 tests passed). As
  expected this exercises nothing of the change (see below), but confirms
  the tree is otherwise unaffected.

The concern: as written, the new code is never compiled
--------------------------------------------------------
The guard is added inside the "#else /* !HAVE_POLL */" arm of
PQsocketPoll() -- i.e. the select(2) fallback -- and is itself wrapped in
"#ifndef WIN32":

    #ifndef WIN32
        if (sock >= FD_SETSIZE)
        {
            SOCK_ERRNO_SET(EINVAL);
            return -1;
        }
    #endif

But src/include/port.h unconditionally does, for every non-Windows build:

    #ifndef WIN32
    ...
    #define HAVE_POLL 1
    ...
    #endif

(that line is a plain #define, not guarded by #ifndef HAVE_POLL). So:

  * On any non-Windows platform HAVE_POLL is always defined, hence the
    "#else /* !HAVE_POLL */" select() arm -- where the new check lives --
    is never compiled. I confirmed this by preprocessing fe-misc.c: the
    FD_SETSIZE check does not appear in the output, and even forcing
    -UHAVE_POLL on the command line does not help, because port.h redefines
    HAVE_POLL back to 1.

  * On Windows the select() arm *is* compiled (HAVE_POLL is not defined
    there), but the new block is excluded by the surrounding "#ifndef
    WIN32" -- which is also the behavior you intended per your v2 note that
    the check isn't needed on Windows.

So the net effect of v2 on every supported platform is a no-op: there is
no build configuration in which the new "sock >= FD_SETSIZE" test is
reached. The block itself is fine in isolation (I compiled it standalone
with -Werror; FD_SETSIZE, SOCK_ERRNO_SET and EINVAL are all in scope, and
the shape matches the existing precedent in fe-secure-gssapi.c), but it
can't protect anything as placed.

Because PostgreSQL assumes poll(2) on all Unix (that is exactly what the
port.h block encodes), the select() fallback in PQsocketPoll() is
effectively Windows-only today, and Windows is the one platform you
established does not need the guard. That seems to leave the original
scenario (a Unix without poll(2) handing out an fd >= FD_SETSIZE) without
a live code path to hit in the first place -- which may be why this reads
as "mostly theoretical".

Questions / suggestions
-----------------------
- Is there a real, currently-supported platform where PQsocketPoll() takes
  the select() path *and* lacks poll(2)? If not, it's worth asking on-list
  whether the hardening is still wanted, or whether the right fix is
  elsewhere (e.g. the pgbench/client-fd thread you referenced in [0]).
- If it is still wanted, the check needs to sit where it actually compiles
  and runs. As-is it will not.

Given the above, my tentative verdict is that v2 does not yet do what it
intends; I'd suggest clarifying the target platform before another spin.
Happy to re-test once that's settled.

Best regards,
Abraham


Reply via email to