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

From: Nathan Bossart <nathandbossart(at)gmail(dot)com>
To: Sehrope Sarkuni <sehrope(at)jackdb(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [PATCH] Fix getopt_long() argument handling and add tests
Date: 2026-09-04 21:58:11
Message-ID: aps-8xDMYZfVQ5rQ@nathan
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Attachment Content-Type Size
v2-0001-Fix-optional-argument-handling-in-in-tree-getopt_.patch text/plain 2.4 KB
v2-0002-Fix-option-argument-lookup-in-in-tree-getopt_long.patch text/plain 3.0 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Zsolt Parragi 2026-09-04 22:01:14 Re: tablecmds: fix bug where index rebuild loses replica identity on partitions
Previous Message Zsolt Parragi 2026-09-04 21:48:59 Re: Introducing find_all_inheritors_ordered()