Skip to content

Commit fb55e95

Browse files
committed
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
1 parent 1fb2d78 commit fb55e95

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/bin/psql/startup.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,15 +619,18 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
619619
options->single_txn = true;
620620
break;
621621
case '?':
622-
/* Actual help option given */
623-
if (strcmp(argv[optind - 1], "-?") == 0)
622+
if (optind <= argc &&
623+
strcmp(argv[optind - 1], "-?") == 0)
624624
{
625+
/* actual help option given */
625626
usage(NOPAGER);
626627
exit(EXIT_SUCCESS);
627628
}
628-
/* unknown option reported by getopt */
629629
else
630+
{
631+
/* getopt error (unknown option or missing argument) */
630632
goto unknown_option;
633+
}
631634
break;
632635
case 1:
633636
{

0 commit comments

Comments
 (0)