postgres.git / summary / log / commit / refs

commit    fb55e95396caa5f03dc37f72522313eb5c095ee1
Author:   Tom Lane <tgl@sss.pgh.pa.us>
Date:     Sun Aug 25 19:04:04 2019 +0000
First released in: 13.0, 9.4.25, 9.5.20, 9.6.16, 10.11, 11.6, 12.0

    Avoid platform-specific null pointer dereference in psql.
    
    POSIX permits getopt() to advance optind beyond argc when the last
    argv entry is an option that requires an argument and hasn't got one.
    It seems that no major platforms actually do that, but musl does,
    so that something like "psql -f" would crash with that libc.
    Add a check that optind is in range before trying to look at the
    possibly-bogus option.
    
    Report and fix by Quentin Rameau.  Back-patch to all supported
    branches.
    
    Discussion: https://postgr.es/m/20190825100617.GA6087@fifth.space


src/bin/psql/startup.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 1e48f4ad5ad..260b4ea55cd 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -619,15 +619,18 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options) options->single_txn = true; break; case '?': - /* Actual help option given */ - if (strcmp(argv[optind - 1], "-?") == 0) + if (optind <= argc && + strcmp(argv[optind - 1], "-?") == 0) { + /* actual help option given */ usage(NOPAGER); exit(EXIT_SUCCESS); } - /* unknown option reported by getopt */ else + { + /* getopt error (unknown option or missing argument) */ goto unknown_option; + } break; case 1: { [parent: 1fb2d78cb946]