Skip to content

Commit c693c5c

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 157b233 commit c693c5c

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/bin/psql/startup.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,15 +552,17 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
552552
options->single_txn = true;
553553
break;
554554
case '?':
555-
/* Actual help option given */
556-
if (strcmp(argv[optind - 1], "--help") == 0 || strcmp(argv[optind - 1], "-?") == 0)
555+
if (optind <= argc &&
556+
(strcmp(argv[optind - 1], "--help") == 0 ||
557+
strcmp(argv[optind - 1], "-?") == 0))
557558
{
559+
/* actual help option given */
558560
usage();
559561
exit(EXIT_SUCCESS);
560562
}
561-
/* unknown option reported by getopt */
562563
else
563564
{
565+
/* getopt error (unknown option or missing argument) */
564566
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
565567
pset.progname);
566568
exit(EXIT_FAILURE);

0 commit comments

Comments
 (0)