From 9684e426954921e8b2bfa367f9e6a4cbbf4ce5ff Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Thu, 29 Aug 2019 16:19:35 +0200 Subject: [PATCH] Error out on too many command-line arguments Fix up oid2name, pg_upgrade, and pgbench to error out on too many command-line arguments. This makes it match the behavior of other PostgreSQL programs. Author: Peter Eisentraut, Ibrar Ahmed Discussion: https://www.postgresql.org/message-id/flat/f2554627-04e7-383a-ef01-ab99bb6a291c%402ndquadrant.com --- contrib/oid2name/oid2name.c | 8 ++++++++ src/bin/pg_upgrade/option.c | 3 +++ src/bin/pgbench/pgbench.c | 10 +++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/contrib/oid2name/oid2name.c b/contrib/oid2name/oid2name.c index d3a4a50005b..df4f9c062ff 100644 --- a/contrib/oid2name/oid2name.c +++ b/contrib/oid2name/oid2name.c @@ -185,6 +185,14 @@ get_opts(int argc, char **argv, struct options *my_opts) exit(1); } } + + if (optind < argc) + { + fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), + progname, argv[optind]); + fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); + exit(1); + } } static void diff --git a/src/bin/pg_upgrade/option.c b/src/bin/pg_upgrade/option.c index 8d66be085a4..28ff4c48ed3 100644 --- a/src/bin/pg_upgrade/option.c +++ b/src/bin/pg_upgrade/option.c @@ -218,6 +218,9 @@ parseCommandLine(int argc, char *argv[]) } } + if (optind < argc) + pg_fatal("too many command-line arguments (first is \"%s\")\n", argv[optind]); + if ((log_opts.internal = fopen_priv(INTERNAL_LOG_FILE, "a")) == NULL) pg_fatal("could not open log file \"%s\": %m\n", INTERNAL_LOG_FILE); diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 570cf3306af..ed7652bfbf6 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -5540,7 +5540,7 @@ main(int argc, char **argv) throttle_delay *= nthreads; if (argc > optind) - dbName = argv[optind]; + dbName = argv[optind++]; else { if ((env = getenv("PGDATABASE")) != NULL && *env != '\0') @@ -5551,6 +5551,14 @@ main(int argc, char **argv) dbName = ""; } + if (optind < argc) + { + fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), + progname, argv[optind]); + fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); + exit(1); + } + if (is_init_mode) { if (benchmarking_option_set)