Incorrect fd handling in syslogger.c for Win64 under EXEC_BACKEND
Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.
You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:
docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t44868psql -h localhost -U postgresBuilt from patchset v2 (message #2), July 27, 2026 at 04:10 PM.
Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:
git clone --branch t44868_2 https://github.com/hackorum-dev/postgres.gitIn a checkout you already have, add the fork once:
git remote add hackorum https://github.com/hackorum-dev/postgres.gitthen, for this patchset and every later one:
git fetch hackorum t44868_2 && git checkout t44868_2Patchset v2 (message #2) is on t44868_2
Hi all,
While reviewing a patch that refactors syslogger.c, we use the
following code to pass down a HANDLE to a forked syslogger as of
syslogger_forkexec():
if (syslogFile != NULL)
snprintf(filenobuf, sizeof(filenobuf), "%ld",
(long) _get_osfhandle(_fileno(syslogFile)));
Then, in the kicked syslogger, the parsing is done as follows in
syslogger_parseArgs() for WIN32, with a simple atoi():
fd = atoi(*argv++);
_get_osfhandle() returns intptr_t whose size is system-dependent, as
it would be 32b for Win32 and 64b for Win64:
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle
https://docs.microsoft.com/en-us/cpp/c-runtime-library/standard-types
As long is 4 bytes on Windows, we would run into overflows here if the
handle is out of the normal 32b range. So the logic as coded is fine
for Win32, but it could be wrong under Win64.
Am I missing something obvious? One thing that we could do here is
to do the parsing with pg_lltoa() while printing the argument with
INT64_FORMAT, no?
Thoughts?
--
Michael
On Tue, Sep 28, 2021 at 12:41:40PM +0900, Michael Paquier wrote:
Am I missing something obvious? One thing that we could do here is
to do the parsing with pg_lltoa() while printing the argument with
INT64_FORMAT, no?
I wrote that a bit too quickly. After looking at it, what we could
use to parse the handle pointer is scanint8() instead, even if that's
a bit ugly. I also found the code a bit confused regarding "fd", that
could be manipulated as an int or intptr_t, so something like the
attached should improve the situation.
Opinions welcome.
--
Michael
On Tue, Sep 28, 2021 at 02:36:52PM +0900, Michael Paquier wrote:
I wrote that a bit too quickly. After looking at it, what we could
use to parse the handle pointer is scanint8() instead, even if that's
a bit ugly. I also found the code a bit confused regarding "fd", that
could be manipulated as an int or intptr_t, so something like the
attached should improve the situation.
As reminded by Jacob, the code is corrently correct as handles are
4 bytes on both Win32 and Win64:
https://docs.microsoft.com/en-us/windows/win32/winauto/32-bit-and-64-bit-interoperability
Sorry for the noise. It looks like I got confused by intptr_t :p
--
Michael