aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2019-08-25 15:04:04 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2019-08-25 15:04:04 -0400
commit65b1cad5a0929e685e79f7e84f56e0f28a54107a (patch)
treeefb2e0f95685a32191a222e179d5b35ba845e0ed
parent9f4bf6d2af1ef4a69a789b65c9763fd13bca52d5 (diff)
downloadpostgresql-65b1cad5a0929e685e79f7e84f56e0f28a54107a.tar.gz
postgresql-65b1cad5a0929e685e79f7e84f56e0f28a54107a.zip
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
-rw-r--r--src/bin/psql/startup.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 0c090d15c2e..142ec76e10f 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -571,15 +571,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:
{