Re: Fix errno handling in popen_check() to avoid false error reports

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: ma lz <ma100(at)hotmail(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Fix errno handling in popen_check() to avoid false error reports
Date: 2026-03-04 18:45:35
Message-ID: 1541734.1772649935@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

ma lz <ma100(at)hotmail(dot)com> writes:
> In certain environments (e.g., CentOS 7 host with Docker container running Euler 24.03, older kernel), popen() internally first tries clone3, which fails with ENOSYS (38), then falls back to traditional clone and succeeds. However, the errno value from the failed clone3 attempt (38) is not cleared, so after a successful popen(), errno remains 38.

Sure.

> If a caller (incorrectly) checks errno even when popen() succeeds, they might see errno == ENOSYS and mistakenly believe an error occurred, even though the command executed successfully.

That would be a a bug in that caller (and I don't see any such
caller). It is *extremely* common for system functions to trash errno
in non-failure paths. That variable is only promised to be meaningful
after a failure return.

The point of the errno = 0 step in popen_check() is so that we don't
print an unrelated error code if popen fails without setting errno.
POSIX doesn't require it to set errno, oddly enough:

Upon successful completion, popen() shall return a pointer to an
open stream that can be used to read or write to the
pipe. Otherwise, it shall return a null pointer and may set errno
to indicate the error.

> Proposed Fix
> Move errno = 0 to the success path, so errno is only cleared when popen() actually succeeds:

This is based on a complete misunderstanding of what that step is for.
It would break the case we're protecting against without fixing
anything.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2026-03-04 18:50:34 Re: POC: PLpgSQL FOREACH IN JSON ARRAY
Previous Message Tom Lane 2026-03-04 18:12:48 Re: astreamer_lz4: fix bug of output pointer advancement in decompressor