1
0
mirror of https://github.com/postgres/postgres.git synced 2026-10-23 13:29:09 +03:00

Fix optional-argument handling in in-tree getopt_long().

If a long option that accepts an optional argument is given without
one, optind is advanced twice, and the following argument is
skipped.  For example, "pg_waldump --stats --limit 5" skips --limit
and interprets "5" as a non-option, so the command complains that
it cannot location WAL file "5".  The same path also returns BADARG
when optstring starts with a colon, even though nothing is missing.
To fix, handle optional arguments before the missing-argument code,
which then only needs to deal with required arguments.

This is a bug fix and could be back-patched, but since this issue
went unnoticed for 23 years, I'm not going to bother.

Author: Sehrope Sarkuni <sehrope@jackdb.com>
Reviewed-by: solai v <solai.cdac@gmail.com>
Discussion: https://postgr.es/m/CAH7T-arxDuVCSkorO%3Dk7%2BM-_JV0JFzMpN_EtKMyD2K0RDqZ2OA%40mail.gmail.com
This commit is contained in:
Nathan Bossart
2026-09-11 14:37:51 -05:00
parent 3a3524e9ac
commit 61aa17b92e
2 changed files with 6 additions and 8 deletions

View File

@@ -340,11 +340,10 @@ sub test_pg_waldump
my ($stdout, $stderr);
my $result = IPC::Run::run [
'pg_waldump',
'pg_waldump', @opts,
'--start' => $startlsn,
'--end' => $endlsn,
'--path' => $path,
@opts
],
'>' => \$stdout,
'2>' => \$stderr;

View File

@@ -137,8 +137,9 @@ retry:
{
if (place[namelen] == '=')
optarg = place + namelen + 1;
else if (optind < argc - 1 &&
has_arg == required_argument)
else if (has_arg == optional_argument)
optarg = NULL;
else if (optind < argc - 1)
{
optind++;
optarg = argv[optind];
@@ -152,16 +153,14 @@ retry:
return BADARG;
}
if (opterr && has_arg == required_argument)
if (opterr)
fprintf(stderr,
"%s: option requires an argument -- %s\n",
argv[0], place);
place = EMSG;
if (has_arg == required_argument)
return BADCH;
optarg = NULL;
return BADCH;
}
}
else