[PATCH] Fix getopt_long() argument handling and add tests

Started by Sehrope Sarkuni3 months ago5 messageshackers
Jump to latest
#1Sehrope Sarkuni
sehrope@jackdb.com

Hi hackers,

While reworking the pg_waldump TAP test [1]/messages/by-id/CAH7T-araSEdsNpxiKaMOW8kr_ZHhekCLHx5yzm2ksq4FdgULbA@mail.gmail.com I ran "pg_waldump --stats
--limit 5" and it failed on Windows CI with 'could not locate WAL file
"5"'. For a long option with optional_argument and no "=", the port
getopt_long() increments optind in the missing-argument branch and
again at the end of the long option path, so the next option is
skipped and its value becomes a positional argument. The same branch
returns BADARG when optstring starts with a colon although nothing is
missing.

glibc handles both correctly, so this only happens in CI on Windows, for
--stats in pg_waldump and --install-missing in pg_amcheck when another
option follows. psql's --help is also optional_argument but exits
before anything after it matters.

0001 gives optional_argument its own branch that sets optarg to NULL
and falls through to the common optind increment. The remaining else
is then only the missing required argument case.

0002 adds src/test/modules/test_getopt_long, since nothing in
check-world exercises this file on Linux or macOS. A small harness
takes an optstring, a long option list and the arguments, and prints
one line per getopt_long() return plus the leftover non-options.
src/port/getopt_long.c is compiled into it directly so it is tested
everywhere. A second copy linked without it runs the same cases
against the system getopt_long().

The TAP test is a text table of named cases, each with its arguments
and expected output:

== long optional, separate not consumed
args: --gamma foo
--gamma
-- foo

== short missing arg, silent
optstring: :ab:
args: -b
:
--

It covers short options attached, separate and bundled, missing and
unknown options with and without the leading colon, long required
arguments in each form, long optional arguments first, middle and
last, the flag pointer form, "--", a lone "-" and non-option
reordering. Port and system output match on every case on Linux and
macOS. Only the error message wording differs and the test accepts
either.

0003 fixes a second problem the tests turned up. The port moves each
non-option to the end of argv as soon as it sees it, so by the time a
following option looks for its argument, a non-option that came before
it on the command line is already sitting after it and gets taken:
"pg_amcheck mydb --jobs" parses as --jobs=mydb rather than reporting
the missing argument. The system getopt_long() reports the missing
argument. The fix bounds the argument search by the start of the
moved block. 0004 adds the test cases for it, kept separate in case
0003 is backpatched without the test module.

This behavior comes from the non-option reordering added in 411b720343.
CC'ing Nathan in case I'm missing an intended aspect of that
implementation.

Without 0001 and 0003 the port binary fails 16 cases, the 12 optional
argument ones that 0001 fixes and the four from 0004 that 0003 fixes.
The system binary passes throughout on Linux. With both patches, all
244 checks pass.

Both fixes seem worth backpatching. The test module itself probably
doesn't need to be.

AI review also flagged a couple of possible portability issues with
the system getopt_long() comparison on BSD, particularly around
differences in error messages. I don't have a BSD system to test on,
so will see what the buildfarm says.

[1]: /messages/by-id/CAH7T-araSEdsNpxiKaMOW8kr_ZHhekCLHx5yzm2ksq4FdgULbA@mail.gmail.com

Regards,
-- Sehrope Sarkuni
Founder & CEO | JackDB, Inc. | https://www.jackdb.com/

Attachments:

t253601_1
0001-Fix-optional-argument-handling-in-port-getopt_long.patchtext/x-patch; charset=US-ASCII; name=0001-Fix-optional-argument-handling-in-port-getopt_long.patchDownload+6-15
0004-Test-getopt_long-with-a-non-option-before-a-missing-.patchtext/x-patch; charset=US-ASCII; name=0004-Test-getopt_long-with-a-non-option-before-a-missing-.patchDownload+24-1
0002-Add-test_getopt_long-module.patchtext/x-patch; charset=US-ASCII; name=0002-Add-test_getopt_long-module.patchDownload+628-1
0003-Do-not-take-a-moved-non-option-as-an-option-argument.patchtext/x-patch; charset=US-ASCII; name=0003-Do-not-take-a-moved-non-option-as-an-option-argument.patchDownload+12-3
#2Nathan Bossart
nathandbossart@gmail.com
In reply to: Sehrope Sarkuni (#1)
Re: [PATCH] Fix getopt_long() argument handling and add tests

On Sat, Aug 29, 2026 at 12:57:00PM -0400, Sehrope Sarkuni wrote:

While reworking the pg_waldump TAP test [1] I ran "pg_waldump --stats
--limit 5" and it failed on Windows CI with 'could not locate WAL file
"5"'. For a long option with optional_argument and no "=", the port
getopt_long() increments optind in the missing-argument branch and
again at the end of the long option path, so the next option is
skipped and its value becomes a positional argument. The same branch
returns BADARG when optstring starts with a colon although nothing is
missing.

[...]

0003 fixes a second problem the tests turned up. The port moves each
non-option to the end of argv as soon as it sees it, so by the time a
following option looks for its argument, a non-option that came before
it on the command line is already sitting after it and gets taken:
"pg_amcheck mydb --jobs" parses as --jobs=mydb rather than reporting
the missing argument. The system getopt_long() reports the missing
argument. The fix bounds the argument search by the start of the
moved block. 0004 adds the test cases for it, kept separate in case
0003 is backpatched without the test module.

Nice finds. I've attached what I have staged for commit. I've tried to
simplify the diffs a bit, and I also got rid of the new test module in
favor of adding cases to existing test files. I don't think we need to
bother back-patching 0001; the bug went undiscovered for ~23 years. I can
try back-patching 0002 to v17, though.

--
nathan

Attachments:

t253601_2
v2-0001-Fix-optional-argument-handling-in-in-tree-getopt_.patchtext/plain; charset=us-asciiDownload+6-9
v2-0002-Fix-option-argument-lookup-in-in-tree-getopt_long.patchtext/plain; charset=us-asciiDownload+11-8
#3Sehrope Sarkuni
sehrope@jackdb.com
In reply to: Nathan Bossart (#2)
Re: [PATCH] Fix getopt_long() argument handling and add tests

On Fri, Sep 4, 2026 at 5:58 PM Nathan Bossart <nathandbossart@gmail.com> wrote:

Nice finds. I've attached what I have staged for commit.

Thanks for the review!

I tried it out locally and also layered the new test module on top of
your patches. Everything passes.

I've tried to simplify the diffs a bit,

Yes, I like your 0002 better. Easier to reason about the state machine
by setting nonopt_start once in the beginning instead of the ternaries.

and I also got rid of the new test module in favor of adding cases to
existing test files. I don't think we need to bother back-patching
0001; the bug went undiscovered for ~23 years.

Sounds good. It was useful for verifying the change but it's a bit
overkill to include.

Regards,
-- Sehrope Sarkuni
Founder & CEO | JackDB, Inc. | https://www.jackdb.com/

#4solai v
solai.cdac@gmail.com
In reply to: Nathan Bossart (#2)
Re: [PATCH] Fix getopt_long() argument handling and add tests

Hi all,

On Thu, Sep 10, 2026 at 11:46 AM Nathan Bossart
<nathandbossart@gmail.com> wrote:

On Sat, Aug 29, 2026 at 12:57:00PM -0400, Sehrope Sarkuni wrote:

While reworking the pg_waldump TAP test [1] I ran "pg_waldump --stats
--limit 5" and it failed on Windows CI with 'could not locate WAL file
"5"'. For a long option with optional_argument and no "=", the port
getopt_long() increments optind in the missing-argument branch and
again at the end of the long option path, so the next option is
skipped and its value becomes a positional argument. The same branch
returns BADARG when optstring starts with a colon although nothing is
missing.

[...]

0003 fixes a second problem the tests turned up. The port moves each
non-option to the end of argv as soon as it sees it, so by the time a
following option looks for its argument, a non-option that came before
it on the command line is already sitting after it and gets taken:
"pg_amcheck mydb --jobs" parses as --jobs=mydb rather than reporting
the missing argument. The system getopt_long() reports the missing
argument. The fix bounds the argument search by the start of the
moved block. 0004 adds the test cases for it, kept separate in case
0003 is backpatched without the test module.

Nice finds. I've attached what I have staged for commit. I've tried to
simplify the diffs a bit, and I also got rid of the new test module in
favor of adding cases to existing test files. I don't think we need to
bother back-patching 0001; the bug went undiscovered for ~23 years. I can
try back-patching 0002 to v17, though.

I tested both v2 patches on the latest master and was able to
reproduce both reported issues with the in-tree getopt_long()
implementation before applying the patches with the following
observations:

--stats --limit 5 incorrectly skipped --limit and left 5 as a
positional argument.
mydb --jobs incorrectly consumed mydb as the argument to --jobs.

After applying both patches, both cases behaved correctly. And I ran
the affected TAP tests and everything got passed. Also I reviewed the
changes in src/port/getopt_long.c and the corresponding test
additions. I did not find any issues with the v2 patches.
The patch looks good to me.

Regards,
Solai

#5Nathan Bossart
nathandbossart@gmail.com
In reply to: solai v (#4)
Re: [PATCH] Fix getopt_long() argument handling and add tests

On Thu, Sep 10, 2026 at 04:26:05PM +0530, solai v wrote:

After applying both patches, both cases behaved correctly. And I ran
the affected TAP tests and everything got passed. Also I reviewed the
changes in src/port/getopt_long.c and the corresponding test
additions. I did not find any issues with the v2 patches.
The patch looks good to me.

Thanks for reviewing. Committed.

--
nathan