agora inbox for pgsql-hackers@postgresql.orghelp / color / mirror / Atom feed
[PATCH v3 1/3] Be strict in numeric parameters on command line 7+ messages / 2 participants [nested] [flat]
* [PATCH v3 1/3] Be strict in numeric parameters on command line @ 2021-07-08 06:08 Kyotaro Horiguchi <horikyota.ntt@gmail.com> 0 siblings, 0 replies; 7+ messages in thread From: Kyotaro Horiguchi @ 2021-07-08 06:08 UTC (permalink / raw) Some numeric command line parameters are tolerant of valid values followed by garbage like "123xyz". Be strict to reject such invalid values. Do the same for psql meta command parameters. --- src/bin/pg_amcheck/pg_amcheck.c | 15 ++- src/bin/pg_basebackup/pg_basebackup.c | 24 +++- src/bin/pg_basebackup/pg_receivewal.c | 39 +++++-- src/bin/pg_basebackup/pg_recvlogical.c | 44 +++++-- src/bin/pg_checksums/pg_checksums.c | 17 ++- src/bin/pg_ctl/pg_ctl.c | 42 ++++++- src/bin/pg_dump/pg_dump.c | 57 ++++++--- src/bin/pg_dump/pg_restore.c | 42 ++++--- src/bin/pg_upgrade/option.c | 30 ++++- src/bin/pgbench/pgbench.c | 154 +++++++++++++++++++------ src/bin/psql/command.c | 73 +++++++++++- src/bin/scripts/reindexdb.c | 15 ++- src/bin/scripts/vacuumdb.c | 59 ++++++++-- 13 files changed, 484 insertions(+), 127 deletions(-) diff --git a/src/bin/pg_amcheck/pg_amcheck.c b/src/bin/pg_amcheck/pg_amcheck.c index 4bde16fb4b..f40d58ac96 100644 --- a/src/bin/pg_amcheck/pg_amcheck.c +++ b/src/bin/pg_amcheck/pg_amcheck.c @@ -17,6 +17,7 @@ #include "catalog/pg_am_d.h" #include "catalog/pg_namespace_d.h" #include "common/logging.h" +#include "common/string.h" #include "common/username.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" @@ -326,10 +327,18 @@ main(int argc, char *argv[]) append_btree_pattern(&opts.exclude, optarg, encoding); break; case 'j': - opts.jobs = atoi(optarg); - if (opts.jobs < 1) + errno = 0; + opts.jobs = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("number of parallel jobs must be at least 1"); + pg_log_error("number of parallel jobs out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || opts.jobs < 1) + { + pg_log_error("number of parallel jobs must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 8bb0acf498..c30005f569 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -2287,6 +2287,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "CD:F:r:RS:T:X:l:nNzZ:d:c:h:p:U:s:wWkvP", long_options, &option_index)) != -1) { + char *endptr; + switch (c) { case 'C': @@ -2371,10 +2373,12 @@ main(int argc, char **argv) #endif break; case 'Z': - compresslevel = atoi(optarg); - if (compresslevel < 0 || compresslevel > 9) + errno = 0; + compresslevel = strtoint(optarg, &endptr, 10); + if (*endptr || + errno == ERANGE || compresslevel < 0 || compresslevel > 9) { - pg_log_error("invalid compression level \"%s\"", optarg); + pg_log_error("compression level must be a digit in range 0..9: \"%s\"", optarg); exit(1); } break; @@ -2409,10 +2413,18 @@ main(int argc, char **argv) dbgetpassword = 1; break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid status interval \"%s\"", optarg); + pg_log_error("status interval out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || standby_message_timeout < 0) + { + pg_log_error("status interval must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; diff --git a/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c index c1334fad35..fb03147fe7 100644 --- a/src/bin/pg_basebackup/pg_receivewal.c +++ b/src/bin/pg_basebackup/pg_receivewal.c @@ -22,6 +22,7 @@ #include "access/xlog_internal.h" #include "common/file_perm.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "libpq-fe.h" #include "receivelog.h" @@ -520,6 +521,9 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "D:d:E:h:p:U:s:S:nwWvZ:", long_options, &option_index)) != -1) { + char *endptr; + int v; + switch (c) { case 'D': @@ -532,9 +536,17 @@ main(int argc, char **argv) dbhost = pg_strdup(optarg); break; case 'p': - if (atoi(optarg) <= 0) + errno = 0; + v = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid port number \"%s\"", optarg); + pg_log_error("port number out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || v < 1) + { + pg_log_error("port number must be an integer greater than zero: \"%s\"", + optarg); exit(1); } dbport = pg_strdup(optarg); @@ -549,10 +561,18 @@ main(int argc, char **argv) dbgetpassword = 1; break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid status interval \"%s\"", optarg); + pg_log_error("status interval out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || standby_message_timeout < 0) + { + pg_log_error("status interval must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; @@ -574,10 +594,13 @@ main(int argc, char **argv) verbose++; break; case 'Z': - compresslevel = atoi(optarg); - if (compresslevel < 0 || compresslevel > 9) + errno = 0; + compresslevel = strtoint(optarg, &endptr, 10); + if (*endptr || + errno == ERANGE || compresslevel < 0 || compresslevel > 9) { - pg_log_error("invalid compression level \"%s\"", optarg); + pg_log_error("compression level must be a digit in range 0..9: \"%s\"", + optarg); exit(1); } break; diff --git a/src/bin/pg_basebackup/pg_recvlogical.c b/src/bin/pg_basebackup/pg_recvlogical.c index 76bd153fac..9bc4902033 100644 --- a/src/bin/pg_basebackup/pg_recvlogical.c +++ b/src/bin/pg_basebackup/pg_recvlogical.c @@ -23,6 +23,7 @@ #include "common/fe_memutils.h" #include "common/file_perm.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "libpq-fe.h" #include "libpq/pqsignal.h" @@ -732,6 +733,9 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "E:f:F:nvtd:h:p:U:wWI:o:P:s:S:", long_options, &option_index)) != -1) { + char *endptr; + int v; + switch (c) { /* general options */ @@ -739,10 +743,18 @@ main(int argc, char **argv) outfile = pg_strdup(optarg); break; case 'F': - fsync_interval = atoi(optarg) * 1000; - if (fsync_interval < 0) + errno = 0; + fsync_interval = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid fsync interval \"%s\"", optarg); + pg_log_error("fsync interval out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || fsync_interval < 0) + { + pg_log_error("fsync interval must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; @@ -763,9 +775,17 @@ main(int argc, char **argv) dbhost = pg_strdup(optarg); break; case 'p': - if (atoi(optarg) <= 0) + errno = 0; + v = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid port number \"%s\"", optarg); + pg_log_error("port number out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || v < 1) + { + pg_log_error("port number must be an integer greater than zero: \"%s\"", + optarg); exit(1); } dbport = pg_strdup(optarg); @@ -820,10 +840,18 @@ main(int argc, char **argv) plugin = pg_strdup(optarg); break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid status interval \"%s\"", optarg); + pg_log_error("status interval out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || standby_message_timeout < 0) + { + pg_log_error("status interval must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c index 3c326906e2..78a1d4ef38 100644 --- a/src/bin/pg_checksums/pg_checksums.c +++ b/src/bin/pg_checksums/pg_checksums.c @@ -24,6 +24,7 @@ #include "common/file_perm.h" #include "common/file_utils.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "pg_getopt.h" #include "storage/bufpage.h" @@ -506,6 +507,9 @@ main(int argc, char *argv[]) while ((c = getopt_long(argc, argv, "cD:deNPf:v", long_options, &option_index)) != -1) { + char *endptr; + int v; + switch (c) { case 'c': @@ -518,9 +522,18 @@ main(int argc, char *argv[]) mode = PG_MODE_ENABLE; break; case 'f': - if (atoi(optarg) == 0) + errno = 0; + v = strtoint(optarg, &endptr, 10); + if(*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid filenode specification, must be numeric: %s", optarg); + pg_log_error("filenode specification out of range: %s", + optarg); + exit(1); + } + if(*endptr || v < 1) + { + pg_log_error("filenode specification must be an integer greater than zero: %s", + optarg); exit(1); } only_filenode = pstrdup(optarg); diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 7985da0a94..0f72ef016b 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -76,6 +76,7 @@ typedef enum #define WAITS_PER_SEC 10 /* should divide USEC_PER_SEC evenly */ +static bool do_wait_arg = false; static bool do_wait = true; static int wait_seconds = DEFAULT_WAIT; static bool wait_seconds_arg = false; @@ -2331,6 +2332,8 @@ main(int argc, char **argv) /* process command-line options */ while (optind < argc) { + char *endptr; + while ((c = getopt_long(argc, argv, "cD:e:l:m:N:o:p:P:sS:t:U:wW", long_options, &option_index)) != -1) { @@ -2396,7 +2399,20 @@ main(int argc, char **argv) #endif break; case 't': - wait_seconds = atoi(optarg); + errno = 0; + wait_seconds = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + { + pg_log_error("timeout value out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || wait_seconds < 0) + { + pg_log_error("timeout value must be a non-negative integer: \"%s\"", + optarg); + exit(1); + } wait_seconds_arg = true; break; case 'U': @@ -2408,6 +2424,7 @@ main(int argc, char **argv) break; case 'w': do_wait = true; + do_wait_arg = true; break; case 'W': do_wait = false; @@ -2459,7 +2476,20 @@ main(int argc, char **argv) } ctl_command = KILL_COMMAND; set_sig(argv[++optind]); - killproc = atol(argv[++optind]); + errno = 0; + killproc = strtol(argv[++optind], &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + { + pg_log_error("process ID out of range: \"%s\"", + argv[optind]); + exit(1); + } + if (*endptr || killproc < 0) + { + pg_log_error("process ID must be a non-negative integer: \"%s\"", + argv[optind]); + exit(1); + } } #ifdef WIN32 else if (strcmp(argv[optind], "register") == 0) @@ -2514,6 +2544,14 @@ main(int argc, char **argv) do_wait = false; } + if (wait_seconds == 0 && do_wait) + { + /* Warn if user instructed to wait but we actually don't */ + if (!silent_mode && do_wait_arg) + write_stderr(_("%s: WARNING: -w is ignored because timeout is set to 0\n"), progname); + do_wait = false; + } + if (pg_data) { snprintf(postopts_file, MAXPGPATH, "%s/postmaster.opts", pg_data); diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 321152151d..8ef29b37f6 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -54,6 +54,7 @@ #include "catalog/pg_trigger_d.h" #include "catalog/pg_type_d.h" #include "common/connect.h" +#include "common/string.h" #include "dumputils.h" #include "fe_utils/string_utils.h" #include "getopt_long.h" @@ -103,6 +104,17 @@ static Oid g_last_builtin_oid; /* value of the last builtin oid */ /* The specified names/patterns should to match at least one entity */ static int strict_names = 0; +/* + * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS (= 64 usually) + * parallel jobs because that's the maximum limit for the + * WaitForMultipleObjects() call. + */ +#ifndef WIN32 +#define MAX_NUM_WORKERS INT_MAX +#else +#define MAX_NUM_WORKERS MAXIMUM_WAIT_OBJECTS +#endif + /* * Object inclusion/exclusion lists * @@ -486,7 +498,21 @@ main(int argc, char **argv) break; case 'j': /* number of dump jobs */ - numWorkers = atoi(optarg); + errno = 0; + numWorkers = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && + (errno == ERANGE || numWorkers > MAX_NUM_WORKERS)) + { + pg_log_error("number of parallel jobs out of range: \"%s\"", + optarg); + exit_nicely(1); + } + if (*endptr || numWorkers <= 0) + { + pg_log_error("number of parallel jobs must be an integer greater than zero: \"%s\"", + optarg); + exit_nicely(1); + } break; case 'n': /* include schema(s) */ @@ -549,10 +575,12 @@ main(int argc, char **argv) break; case 'Z': /* Compression Level */ - compressLevel = atoi(optarg); - if (compressLevel < 0 || compressLevel > 9) + errno = 0; + compressLevel = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + compressLevel < 0 || compressLevel > 9) { - pg_log_error("compression level must be in range 0..9"); + pg_log_error("compression level must be a digit in range 0..9: \"%s\"", optarg); exit_nicely(1); } break; @@ -587,10 +615,13 @@ main(int argc, char **argv) case 8: have_extra_float_digits = true; - extra_float_digits = atoi(optarg); - if (extra_float_digits < -15 || extra_float_digits > 3) + errno = 0; + extra_float_digits = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + extra_float_digits < -15 || extra_float_digits > 3) { - pg_log_error("extra_float_digits must be in range -15..3"); + pg_log_error("extra_float_digits must be an integer in range -15..3: \"%s\"", + optarg); exit_nicely(1); } break; @@ -719,18 +750,6 @@ main(int argc, char **argv) if (!plainText) dopt.outputCreateDB = 1; - /* - * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS (= 64 usually) - * parallel jobs because that's the maximum limit for the - * WaitForMultipleObjects() call. - */ - if (numWorkers <= 0 -#ifdef WIN32 - || numWorkers > MAXIMUM_WAIT_OBJECTS -#endif - ) - fatal("invalid number of parallel jobs"); - /* Parallel backup only in the directory archive format so far */ if (archiveFormat != archDirectory && numWorkers > 1) fatal("parallel backup only supported by the directory format"); diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 589b4aed53..3bb5a48c55 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -39,6 +39,7 @@ *------------------------------------------------------------------------- */ #include "postgres_fe.h" +#include "common/string.h" #include <ctype.h> #ifdef HAVE_TERMIOS_H @@ -52,6 +53,13 @@ static void usage(const char *progname); +/* See comments in pg_dump.c */ +#ifndef WIN32 +#define MAX_NUM_WORKERS INT_MAX +#else +#define MAX_NUM_WORKERS MAXIMUM_WAIT_OBJECTS +#endif + int main(int argc, char **argv) { @@ -151,6 +159,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "acCd:ef:F:h:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1", cmdopts, NULL)) != -1) { + char *endptr; + switch (c) { case 'a': /* Dump data only */ @@ -181,7 +191,21 @@ main(int argc, char **argv) break; case 'j': /* number of restore jobs */ - numWorkers = atoi(optarg); + errno = 0; + numWorkers = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && + (errno == ERANGE || numWorkers > MAX_NUM_WORKERS)) + { + pg_log_error("number of parallel jobs out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || numWorkers <= 0) + { + pg_log_error("number of parallel jobs must be an integer greater than zero: \"%s\"", + optarg); + exit(1); + } break; case 'l': /* Dump the TOC summary */ @@ -344,22 +368,6 @@ main(int argc, char **argv) exit_nicely(1); } - if (numWorkers <= 0) - { - pg_log_error("invalid number of parallel jobs"); - exit(1); - } - - /* See comments in pg_dump.c */ -#ifdef WIN32 - if (numWorkers > MAXIMUM_WAIT_OBJECTS) - { - pg_log_error("maximum number of parallel jobs is %d", - MAXIMUM_WAIT_OBJECTS); - exit(1); - } -#endif - /* Can't do single-txn mode with multiple connections */ if (opts->single_txn && numWorkers > 1) { diff --git a/src/bin/pg_upgrade/option.c b/src/bin/pg_upgrade/option.c index 64bbda5650..c014bbca0d 100644 --- a/src/bin/pg_upgrade/option.c +++ b/src/bin/pg_upgrade/option.c @@ -104,6 +104,8 @@ parseCommandLine(int argc, char *argv[]) while ((option = getopt_long(argc, argv, "d:D:b:B:cj:ko:O:p:P:rs:U:v", long_options, &optindex)) != -1) { + char *endptr; + switch (option) { case 'b': @@ -127,7 +129,15 @@ parseCommandLine(int argc, char *argv[]) break; case 'j': - user_opts.jobs = atoi(optarg); + errno = 0; + user_opts.jobs = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + pg_fatal("number of parallel jobs out of range: \"%s\"\n", + optarg); + if (*endptr || user_opts.jobs < 1) + pg_fatal("number of parallel jobs must be an integer greater than zero: \"%s\"\n", + optarg); + break; case 'k': @@ -166,13 +176,23 @@ parseCommandLine(int argc, char *argv[]) * supported on all old/new versions (added in PG 9.2). */ case 'p': - if ((old_cluster.port = atoi(optarg)) <= 0) - pg_fatal("invalid old port number\n"); + errno = 0; + old_cluster.port = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + pg_fatal("old port number out of range: \"%s\"\n", optarg); + if (*endptr || old_cluster.port <= 0) + pg_fatal("old port number must be an integer greater than zero: \"%s\"\n", + optarg); break; case 'P': - if ((new_cluster.port = atoi(optarg)) <= 0) - pg_fatal("invalid new port number\n"); + errno = 0; + new_cluster.port = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + pg_fatal("new port number out of range: \"%s\"\n", optarg); + if (*endptr || new_cluster.port <= 0) + pg_fatal("new port number must be an integer greater than zero: \"%s\"\n", + optarg); break; case 'r': diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 364b5a2e47..1c3b5836c1 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -5856,6 +5856,7 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "iI:h:nvp:dqb:SNc:j:Crs:t:T:U:lf:D:F:M:P:R:L:", long_options, &optindex)) != -1) { char *script; + char *endptr; switch (c) { @@ -5887,10 +5888,18 @@ main(int argc, char **argv) break; case 'c': benchmarking_option_set = true; - nclients = atoi(optarg); - if (nclients <= 0) + errno = 0; + nclients = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid number of clients: \"%s\"", optarg); + pg_log_fatal("number of clients out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || nclients <= 0) + { + pg_log_fatal("number of clients must be an integer greater than zero: \"%s\"", + optarg); exit(1); } #ifdef HAVE_GETRLIMIT @@ -5914,10 +5923,18 @@ main(int argc, char **argv) break; case 'j': /* jobs */ benchmarking_option_set = true; - nthreads = atoi(optarg); - if (nthreads <= 0) + errno = 0; + nthreads = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid number of threads: \"%s\"", optarg); + pg_log_fatal("number of threads out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || nthreads <= 0) + { + pg_log_fatal("number of threads must be an integer greater than zero: \"%s\"", + optarg); exit(1); } #ifndef ENABLE_THREAD_SAFETY @@ -5938,28 +5955,50 @@ main(int argc, char **argv) break; case 's': scale_given = true; - scale = atoi(optarg); - if (scale <= 0) + errno = 0; + scale = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid scaling factor: \"%s\"", optarg); + pg_log_fatal("scaling factor out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || scale <= 0) + { + pg_log_fatal("scaling factor must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; case 't': benchmarking_option_set = true; - nxacts = atoi(optarg); - if (nxacts <= 0) + errno = 0; + nxacts = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid number of transactions: \"%s\"", optarg); + pg_log_fatal("number of transactions out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || nxacts <= 0) + { + pg_log_fatal("number of transactions must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; case 'T': benchmarking_option_set = true; - duration = atoi(optarg); - if (duration <= 0) + errno = 0; + duration = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid duration: \"%s\"", optarg); + pg_log_fatal("duration out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || duration <= 0) + { + pg_log_fatal("duration must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; @@ -6019,10 +6058,13 @@ main(int argc, char **argv) break; case 'F': initialization_option_set = true; - fillfactor = atoi(optarg); - if (fillfactor < 10 || fillfactor > 100) + errno = 0; + fillfactor = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + fillfactor < 10 || fillfactor > 100) { - pg_log_fatal("invalid fillfactor: \"%s\"", optarg); + pg_log_fatal("fillfactor must be an ineger between 10 and 100: \"%s\"", + optarg); exit(1); } break; @@ -6039,23 +6081,38 @@ main(int argc, char **argv) break; case 'P': benchmarking_option_set = true; - progress = atoi(optarg); - if (progress <= 0) + errno = 0; + progress = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid thread progress delay: \"%s\"", optarg); + pg_log_fatal("thread progress delay out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || progress <= 0) + { + pg_log_fatal("thread progress delay must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; case 'R': { /* get a double from the beginning of option value */ - double throttle_value = atof(optarg); + double throttle_value; + errno = 0; + throttle_value = strtod(optarg, &endptr); benchmarking_option_set = true; - if (throttle_value <= 0.0) + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid rate limit: \"%s\"", optarg); + pg_log_fatal("rate limit out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || throttle_value <= 0.0) + { + pg_log_fatal("rate limit must be a real number greater than zero: \"%s\"", optarg); exit(1); } /* Invert rate limit into per-transaction delay in usec */ @@ -6064,11 +6121,20 @@ main(int argc, char **argv) break; case 'L': { - double limit_ms = atof(optarg); + double limit_ms; - if (limit_ms <= 0.0) + errno = 0; + limit_ms = strtod(optarg, &endptr); + + if (*endptr == 0 && errno == ERANGE) + { + pg_log_fatal("latency limit out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || limit_ms <= 0.0) { - pg_log_fatal("invalid latency limit: \"%s\"", optarg); + pg_log_fatal("latency limit must be a real number greater than zero: \"%s\"", optarg); exit(1); } benchmarking_option_set = true; @@ -6089,19 +6155,27 @@ main(int argc, char **argv) break; case 4: /* sampling-rate */ benchmarking_option_set = true; - sample_rate = atof(optarg); - if (sample_rate <= 0.0 || sample_rate > 1.0) + errno = 0; + sample_rate = strtod(optarg, &endptr); + if (*endptr || errno == ERANGE || + sample_rate <= 0.0 || sample_rate > 1.0) { - pg_log_fatal("invalid sampling rate: \"%s\"", optarg); + pg_log_fatal("sampling rate must be an real number between 0.0 and 1.0: \"%s\"", optarg); exit(1); } break; case 5: /* aggregate-interval */ benchmarking_option_set = true; - agg_interval = atoi(optarg); - if (agg_interval <= 0) + errno = 0; + agg_interval = strtod(optarg, &endptr); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid number of seconds for aggregation: \"%s\"", optarg); + pg_log_fatal("aggregate interval out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || agg_interval <= 0) + { + pg_log_fatal("aggregate interval must be a real number greater than zero: \"%s\"", optarg); exit(1); } break; @@ -6135,10 +6209,18 @@ main(int argc, char **argv) break; case 11: /* partitions */ initialization_option_set = true; - partitions = atoi(optarg); - if (partitions < 0) + errno = 0; + partitions = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid number of partitions: \"%s\"", optarg); + pg_log_fatal("number of partitions out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || partitions < 0) + { + pg_log_fatal("number of partitions must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index d704c4220c..13074051b3 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -1040,10 +1040,18 @@ exec_command_edit(PsqlScanState scan_state, bool active_branch, } if (ln) { - lineno = atoi(ln); - if (lineno < 1) + char *endptr; + + errno = 0; + lineno = strtoint(ln, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid line number: %s", ln); + pg_log_error("line number out of range: %s", ln); + status = PSQL_CMD_ERROR; + } + if (*endptr || lineno < 1) + { + pg_log_error("line number must be an integer greater than zero: %s", ln); status = PSQL_CMD_ERROR; } } @@ -4284,7 +4292,25 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "border") == 0) { if (value) - popt->topt.border = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr == 0 && (errno == ERANGE || new_value > 65535)) + { + pg_log_error("\\pset: border out of range"); + return false; + } + if (*endptr || new_value < 0) + { + pg_log_error("\\pset: border must be an integer greater than zero"); + return false; + } + + popt->topt.border = new_value; + } } /* set expanded/vertical mode */ @@ -4440,7 +4466,25 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "pager_min_lines") == 0) { if (value) - popt->topt.pager_min_lines = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + { + pg_log_error("\\pset: pager_min_lines out of range"); + return false; + } + if (*endptr || new_value < 0) + { + pg_log_error("\\pset: pager_min_lines must be a non-negative integer"); + return false; + } + + popt->topt.pager_min_lines = new_value; + } } /* disable "(x rows)" footer */ @@ -4456,7 +4500,24 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "columns") == 0) { if (value) - popt->topt.columns = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + { + pg_log_error("\\pset: column out of range"); + return false; + } + if (*endptr || new_value < 0) + { + pg_log_error("\\pset: column must be a non-negative integer"); + return false; + } + popt->topt.columns = new_value; + } } else { diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c index fc0681538a..42d4d20768 100644 --- a/src/bin/scripts/reindexdb.c +++ b/src/bin/scripts/reindexdb.c @@ -15,6 +15,7 @@ #include "common.h" #include "common/connect.h" #include "common/logging.h" +#include "common/string.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" #include "fe_utils/parallel_slot.h" @@ -109,6 +110,8 @@ main(int argc, char *argv[]) /* process command-line options */ while ((c = getopt_long(argc, argv, "h:p:U:wWeqS:d:ast:i:j:v", long_options, &optindex)) != -1) { + char *endptr; + switch (c) { case 'h': @@ -151,10 +154,16 @@ main(int argc, char *argv[]) simple_string_list_append(&indexes, optarg); break; case 'j': - concurrentCons = atoi(optarg); - if (concurrentCons <= 0) + errno = 0; + concurrentCons = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("number of parallel jobs must be at least 1"); + pg_log_error("number of parallel jobs out of range: %s", optarg); + exit(1); + } + if (*endptr || concurrentCons <= 0) + { + pg_log_error("number of parallel jobs must be an integer greater than zero: %s", optarg); exit(1); } break; diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c index 61974baa78..6b2a34edd0 100644 --- a/src/bin/scripts/vacuumdb.c +++ b/src/bin/scripts/vacuumdb.c @@ -17,6 +17,7 @@ #include "common.h" #include "common/connect.h" #include "common/logging.h" +#include "common/string.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" #include "fe_utils/parallel_slot.h" @@ -141,6 +142,8 @@ main(int argc, char *argv[]) while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:zZFat:fvj:P:", long_options, &optindex)) != -1) { + char *endptr; + switch (c) { case 'h': @@ -192,18 +195,34 @@ main(int argc, char *argv[]) vacopts.verbose = true; break; case 'j': - concurrentCons = atoi(optarg); - if (concurrentCons <= 0) + errno = 0; + concurrentCons = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("number of parallel jobs must be at least 1"); + pg_log_error("number of parallel jobs out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || concurrentCons <= 0) + { + pg_log_error("number of parallel jobs must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; case 'P': - vacopts.parallel_workers = atoi(optarg); - if (vacopts.parallel_workers < 0) + errno = 0; + vacopts.parallel_workers = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("parallel workers for vacuum must be greater than or equal to zero"); + pg_log_error("parallel workers out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || vacopts.parallel_workers < 0) + { + pg_log_error("parallel workers for vacuum must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; @@ -220,18 +239,34 @@ main(int argc, char *argv[]) vacopts.skip_locked = true; break; case 6: - vacopts.min_xid_age = atoi(optarg); - if (vacopts.min_xid_age <= 0) + errno = 0; + vacopts.min_xid_age = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("minimum transaction ID age must be at least 1"); + pg_log_error("minimum transaction ID age out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || vacopts.min_xid_age <= 0) + { + pg_log_error("minimum transaction ID age must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; case 7: - vacopts.min_mxid_age = atoi(optarg); - if (vacopts.min_mxid_age <= 0) + errno = 0; + vacopts.min_mxid_age = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("minimum multixact ID age must be at least 1"); + pg_log_error("minimum multixact ID age out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || vacopts.min_mxid_age <= 0) + { + pg_log_error("minimum multixact ID age must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; -- 2.27.0 ----Next_Part(Wed_Jul_14_10_35_56_2021_265)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="v3-0002-Make-complain-for-invalid-numeirc-values-in-envir.patch" ^ permalink raw reply [nested|flat] 7+ messages in thread
* [PATCH 1/2] Be strict in numeric parameters on command line @ 2021-07-08 06:08 Kyotaro Horiguchi <horikyota.ntt@gmail.com> 0 siblings, 0 replies; 7+ messages in thread From: Kyotaro Horiguchi @ 2021-07-08 06:08 UTC (permalink / raw) Some numeric command line parameters are tolerant of valid values followed by garbage like "123xyz". Be strict to reject such invalid values. Do the same for psql meta command parameters. --- src/bin/pg_amcheck/pg_amcheck.c | 6 ++- src/bin/pg_basebackup/pg_basebackup.c | 13 +++-- src/bin/pg_basebackup/pg_receivewal.c | 18 +++++-- src/bin/pg_basebackup/pg_recvlogical.c | 17 +++++-- src/bin/pg_checksums/pg_checksums.c | 7 ++- src/bin/pg_ctl/pg_ctl.c | 18 ++++++- src/bin/pg_dump/pg_dump.c | 39 ++++++++------- src/bin/pg_dump/pg_restore.c | 17 ++++--- src/bin/pg_upgrade/option.c | 21 ++++++-- src/bin/pgbench/pgbench.c | 66 ++++++++++++++++---------- src/bin/psql/command.c | 52 ++++++++++++++++++-- src/bin/scripts/reindexdb.c | 10 ++-- src/bin/scripts/vacuumdb.c | 23 +++++---- 13 files changed, 219 insertions(+), 88 deletions(-) diff --git a/src/bin/pg_amcheck/pg_amcheck.c b/src/bin/pg_amcheck/pg_amcheck.c index 4bde16fb4b..71a82f9b75 100644 --- a/src/bin/pg_amcheck/pg_amcheck.c +++ b/src/bin/pg_amcheck/pg_amcheck.c @@ -17,6 +17,7 @@ #include "catalog/pg_am_d.h" #include "catalog/pg_namespace_d.h" #include "common/logging.h" +#include "common/string.h" #include "common/username.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" @@ -326,8 +327,9 @@ main(int argc, char *argv[]) append_btree_pattern(&opts.exclude, optarg, encoding); break; case 'j': - opts.jobs = atoi(optarg); - if (opts.jobs < 1) + errno = 0; + opts.jobs = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || opts.jobs < 1) { pg_log_error("number of parallel jobs must be at least 1"); exit(1); diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 8bb0acf498..29be95b96a 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -2287,6 +2287,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "CD:F:r:RS:T:X:l:nNzZ:d:c:h:p:U:s:wWkvP", long_options, &option_index)) != -1) { + char *endptr; + switch (c) { case 'C': @@ -2371,8 +2373,10 @@ main(int argc, char **argv) #endif break; case 'Z': - compresslevel = atoi(optarg); - if (compresslevel < 0 || compresslevel > 9) + errno = 0; + compresslevel = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + compresslevel < 0 || compresslevel > 9) { pg_log_error("invalid compression level \"%s\"", optarg); exit(1); @@ -2409,8 +2413,9 @@ main(int argc, char **argv) dbgetpassword = 1; break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr || errno == ERANGE || standby_message_timeout < 0) { pg_log_error("invalid status interval \"%s\"", optarg); exit(1); diff --git a/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c index c1334fad35..7fef925b99 100644 --- a/src/bin/pg_basebackup/pg_receivewal.c +++ b/src/bin/pg_basebackup/pg_receivewal.c @@ -22,6 +22,7 @@ #include "access/xlog_internal.h" #include "common/file_perm.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "libpq-fe.h" #include "receivelog.h" @@ -520,6 +521,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "D:d:E:h:p:U:s:S:nwWvZ:", long_options, &option_index)) != -1) { + char *endptr; + switch (c) { case 'D': @@ -532,7 +535,9 @@ main(int argc, char **argv) dbhost = pg_strdup(optarg); break; case 'p': - if (atoi(optarg) <= 0) + errno = 0; + if (strtoint(optarg, &endptr, 10) <= 0 || + *endptr || errno == ERANGE) { pg_log_error("invalid port number \"%s\"", optarg); exit(1); @@ -549,8 +554,9 @@ main(int argc, char **argv) dbgetpassword = 1; break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr || errno == ERANGE || standby_message_timeout < 0) { pg_log_error("invalid status interval \"%s\"", optarg); exit(1); @@ -574,8 +580,10 @@ main(int argc, char **argv) verbose++; break; case 'Z': - compresslevel = atoi(optarg); - if (compresslevel < 0 || compresslevel > 9) + errno = 0; + compresslevel = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + compresslevel < 0 || compresslevel > 9) { pg_log_error("invalid compression level \"%s\"", optarg); exit(1); diff --git a/src/bin/pg_basebackup/pg_recvlogical.c b/src/bin/pg_basebackup/pg_recvlogical.c index 76bd153fac..7be932d025 100644 --- a/src/bin/pg_basebackup/pg_recvlogical.c +++ b/src/bin/pg_basebackup/pg_recvlogical.c @@ -23,6 +23,7 @@ #include "common/fe_memutils.h" #include "common/file_perm.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "libpq-fe.h" #include "libpq/pqsignal.h" @@ -732,6 +733,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "E:f:F:nvtd:h:p:U:wWI:o:P:s:S:", long_options, &option_index)) != -1) { + char *endptr; + switch (c) { /* general options */ @@ -739,8 +742,9 @@ main(int argc, char **argv) outfile = pg_strdup(optarg); break; case 'F': - fsync_interval = atoi(optarg) * 1000; - if (fsync_interval < 0) + errno = 0; + fsync_interval = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr || errno == ERANGE || fsync_interval < 0) { pg_log_error("invalid fsync interval \"%s\"", optarg); exit(1); @@ -763,7 +767,9 @@ main(int argc, char **argv) dbhost = pg_strdup(optarg); break; case 'p': - if (atoi(optarg) <= 0) + errno = 0; + if (strtoint(optarg, &endptr, 10) <= 0 || + *endptr || errno == ERANGE) { pg_log_error("invalid port number \"%s\"", optarg); exit(1); @@ -820,8 +826,9 @@ main(int argc, char **argv) plugin = pg_strdup(optarg); break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr || errno == ERANGE || standby_message_timeout < 0) { pg_log_error("invalid status interval \"%s\"", optarg); exit(1); diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c index 3c326906e2..1c4e5b9d85 100644 --- a/src/bin/pg_checksums/pg_checksums.c +++ b/src/bin/pg_checksums/pg_checksums.c @@ -24,6 +24,7 @@ #include "common/file_perm.h" #include "common/file_utils.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "pg_getopt.h" #include "storage/bufpage.h" @@ -506,6 +507,8 @@ main(int argc, char *argv[]) while ((c = getopt_long(argc, argv, "cD:deNPf:v", long_options, &option_index)) != -1) { + char *endptr; + switch (c) { case 'c': @@ -518,7 +521,9 @@ main(int argc, char *argv[]) mode = PG_MODE_ENABLE; break; case 'f': - if (atoi(optarg) == 0) + errno = 0; + if (strtoint(optarg, &endptr, 10) == 0 + || *endptr || errno == ERANGE) { pg_log_error("invalid filenode specification, must be numeric: %s", optarg); exit(1); diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 7985da0a94..78882dec48 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -2331,6 +2331,8 @@ main(int argc, char **argv) /* process command-line options */ while (optind < argc) { + char *endptr; + while ((c = getopt_long(argc, argv, "cD:e:l:m:N:o:p:P:sS:t:U:wW", long_options, &option_index)) != -1) { @@ -2396,7 +2398,13 @@ main(int argc, char **argv) #endif break; case 't': - wait_seconds = atoi(optarg); + errno = 0; + wait_seconds = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || wait_seconds < 0) + { + pg_log_error("invalid timeout \"%s\"", optarg); + exit(1); + } wait_seconds_arg = true; break; case 'U': @@ -2459,7 +2467,13 @@ main(int argc, char **argv) } ctl_command = KILL_COMMAND; set_sig(argv[++optind]); - killproc = atol(argv[++optind]); + errno = 0; + killproc = strtol(argv[++optind], &endptr, 10); + if (*endptr || errno == ERANGE || killproc < 0) + { + pg_log_error("invalid process ID \"%s\"", argv[optind]); + exit(1); + } } #ifdef WIN32 else if (strcmp(argv[optind], "register") == 0) diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 321152151d..793f4b3509 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -54,6 +54,7 @@ #include "catalog/pg_trigger_d.h" #include "catalog/pg_type_d.h" #include "common/connect.h" +#include "common/string.h" #include "dumputils.h" #include "fe_utils/string_utils.h" #include "getopt_long.h" @@ -486,7 +487,19 @@ main(int argc, char **argv) break; case 'j': /* number of dump jobs */ - numWorkers = atoi(optarg); + errno = 0; + numWorkers = strtoint(optarg, &endptr, 10); + /* + * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS + * (= 64 usually) parallel jobs because that's the maximum + * limit for the WaitForMultipleObjects() call. + */ + if (*endptr || errno == ERANGE || numWorkers <= 0 +#ifdef WIN32 + || numWorkers > MAXIMUM_WAIT_OBJECTS +#endif + ) + fatal("invalid number of parallel jobs %s", optarg); break; case 'n': /* include schema(s) */ @@ -549,8 +562,10 @@ main(int argc, char **argv) break; case 'Z': /* Compression Level */ - compressLevel = atoi(optarg); - if (compressLevel < 0 || compressLevel > 9) + errno = 0; + compressLevel = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + compressLevel < 0 || compressLevel > 9) { pg_log_error("compression level must be in range 0..9"); exit_nicely(1); @@ -587,8 +602,10 @@ main(int argc, char **argv) case 8: have_extra_float_digits = true; - extra_float_digits = atoi(optarg); - if (extra_float_digits < -15 || extra_float_digits > 3) + errno = 0; + extra_float_digits = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + extra_float_digits < -15 || extra_float_digits > 3) { pg_log_error("extra_float_digits must be in range -15..3"); exit_nicely(1); @@ -719,18 +736,6 @@ main(int argc, char **argv) if (!plainText) dopt.outputCreateDB = 1; - /* - * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS (= 64 usually) - * parallel jobs because that's the maximum limit for the - * WaitForMultipleObjects() call. - */ - if (numWorkers <= 0 -#ifdef WIN32 - || numWorkers > MAXIMUM_WAIT_OBJECTS -#endif - ) - fatal("invalid number of parallel jobs"); - /* Parallel backup only in the directory archive format so far */ if (archiveFormat != archDirectory && numWorkers > 1) fatal("parallel backup only supported by the directory format"); diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 589b4aed53..285a09aaac 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -39,6 +39,7 @@ *------------------------------------------------------------------------- */ #include "postgres_fe.h" +#include "common/string.h" #include <ctype.h> #ifdef HAVE_TERMIOS_H @@ -151,6 +152,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "acCd:ef:F:h:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1", cmdopts, NULL)) != -1) { + char *endptr; + switch (c) { case 'a': /* Dump data only */ @@ -181,7 +184,13 @@ main(int argc, char **argv) break; case 'j': /* number of restore jobs */ - numWorkers = atoi(optarg); + errno = 0; + numWorkers = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || numWorkers <= 0) + { + pg_log_error("invalid number of parallel jobs"); + exit(1); + } break; case 'l': /* Dump the TOC summary */ @@ -344,12 +353,6 @@ main(int argc, char **argv) exit_nicely(1); } - if (numWorkers <= 0) - { - pg_log_error("invalid number of parallel jobs"); - exit(1); - } - /* See comments in pg_dump.c */ #ifdef WIN32 if (numWorkers > MAXIMUM_WAIT_OBJECTS) diff --git a/src/bin/pg_upgrade/option.c b/src/bin/pg_upgrade/option.c index 64bbda5650..357ba04f4f 100644 --- a/src/bin/pg_upgrade/option.c +++ b/src/bin/pg_upgrade/option.c @@ -104,6 +104,8 @@ parseCommandLine(int argc, char *argv[]) while ((option = getopt_long(argc, argv, "d:D:b:B:cj:ko:O:p:P:rs:U:v", long_options, &optindex)) != -1) { + char *endptr; + switch (option) { case 'b': @@ -127,7 +129,12 @@ parseCommandLine(int argc, char *argv[]) break; case 'j': - user_opts.jobs = atoi(optarg); + errno = 0; + user_opts.jobs = strtoint(optarg, &endptr, 10); + /**/ + if (*endptr || errno == ERANGE) + pg_fatal("invalid number of jobs %s\n", optarg); + break; case 'k': @@ -166,13 +173,17 @@ parseCommandLine(int argc, char *argv[]) * supported on all old/new versions (added in PG 9.2). */ case 'p': - if ((old_cluster.port = atoi(optarg)) <= 0) - pg_fatal("invalid old port number\n"); + errno = 0; + if ((old_cluster.port = strtoint(optarg, &endptr, 10)) <= 0 || + *endptr || errno == ERANGE) + pg_fatal("invalid old port number %s\n", optarg); break; case 'P': - if ((new_cluster.port = atoi(optarg)) <= 0) - pg_fatal("invalid new port number\n"); + errno = 0; + if ((new_cluster.port = strtoint(optarg, &endptr, 10)) <= 0 || + *endptr || errno == ERANGE) + pg_fatal("invalid new port number %s\n", optarg); break; case 'r': diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 4aeccd93af..4020347585 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -5838,6 +5838,7 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "iI:h:nvp:dqb:SNc:j:Crs:t:T:U:lf:D:F:M:P:R:L:", long_options, &optindex)) != -1) { char *script; + char *endptr; switch (c) { @@ -5869,8 +5870,9 @@ main(int argc, char **argv) break; case 'c': benchmarking_option_set = true; - nclients = atoi(optarg); - if (nclients <= 0) + errno = 0; + nclients = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || nclients <= 0) { pg_log_fatal("invalid number of clients: \"%s\"", optarg); exit(1); @@ -5896,8 +5898,9 @@ main(int argc, char **argv) break; case 'j': /* jobs */ benchmarking_option_set = true; - nthreads = atoi(optarg); - if (nthreads <= 0) + errno = 0; + nthreads = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || nthreads <= 0) { pg_log_fatal("invalid number of threads: \"%s\"", optarg); exit(1); @@ -5920,8 +5923,9 @@ main(int argc, char **argv) break; case 's': scale_given = true; - scale = atoi(optarg); - if (scale <= 0) + errno = 0; + scale = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || scale <= 0) { pg_log_fatal("invalid scaling factor: \"%s\"", optarg); exit(1); @@ -5929,8 +5933,9 @@ main(int argc, char **argv) break; case 't': benchmarking_option_set = true; - nxacts = atoi(optarg); - if (nxacts <= 0) + errno = 0; + nxacts = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || nxacts <= 0) { pg_log_fatal("invalid number of transactions: \"%s\"", optarg); exit(1); @@ -5938,8 +5943,9 @@ main(int argc, char **argv) break; case 'T': benchmarking_option_set = true; - duration = atoi(optarg); - if (duration <= 0) + errno = 0; + duration = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || duration <= 0) { pg_log_fatal("invalid duration: \"%s\"", optarg); exit(1); @@ -6001,8 +6007,10 @@ main(int argc, char **argv) break; case 'F': initialization_option_set = true; - fillfactor = atoi(optarg); - if (fillfactor < 10 || fillfactor > 100) + errno = 0; + fillfactor = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + fillfactor < 10 || fillfactor > 100) { pg_log_fatal("invalid fillfactor: \"%s\"", optarg); exit(1); @@ -6021,8 +6029,9 @@ main(int argc, char **argv) break; case 'P': benchmarking_option_set = true; - progress = atoi(optarg); - if (progress <= 0) + errno = 0; + progress = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || progress <= 0) { pg_log_fatal("invalid thread progress delay: \"%s\"", optarg); exit(1); @@ -6031,11 +6040,13 @@ main(int argc, char **argv) case 'R': { /* get a double from the beginning of option value */ - double throttle_value = atof(optarg); + double throttle_value; + errno = 0; + throttle_value = strtod(optarg, &endptr); benchmarking_option_set = true; - if (throttle_value <= 0.0) + if (*endptr || errno == ERANGE || throttle_value <= 0.0) { pg_log_fatal("invalid rate limit: \"%s\"", optarg); exit(1); @@ -6046,9 +6057,12 @@ main(int argc, char **argv) break; case 'L': { - double limit_ms = atof(optarg); + double limit_ms; - if (limit_ms <= 0.0) + errno = 0; + limit_ms = strtod(optarg, &endptr); + + if (*endptr || errno == ERANGE || limit_ms <= 0.0) { pg_log_fatal("invalid latency limit: \"%s\"", optarg); exit(1); @@ -6071,8 +6085,10 @@ main(int argc, char **argv) break; case 4: /* sampling-rate */ benchmarking_option_set = true; - sample_rate = atof(optarg); - if (sample_rate <= 0.0 || sample_rate > 1.0) + errno = 0; + sample_rate = strtod(optarg, &endptr); + if (*endptr || errno == ERANGE || + sample_rate <= 0.0 || sample_rate > 1.0) { pg_log_fatal("invalid sampling rate: \"%s\"", optarg); exit(1); @@ -6080,8 +6096,9 @@ main(int argc, char **argv) break; case 5: /* aggregate-interval */ benchmarking_option_set = true; - agg_interval = atoi(optarg); - if (agg_interval <= 0) + errno = 0; + agg_interval = strtod(optarg, &endptr); + if (*endptr || errno == ERANGE || agg_interval <= 0) { pg_log_fatal("invalid number of seconds for aggregation: \"%s\"", optarg); exit(1); @@ -6117,8 +6134,9 @@ main(int argc, char **argv) break; case 11: /* partitions */ initialization_option_set = true; - partitions = atoi(optarg); - if (partitions < 0) + errno = 0; + partitions = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || partitions < 0) { pg_log_fatal("invalid number of partitions: \"%s\"", optarg); exit(1); diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 543401c6d6..aaed986ae1 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -1039,8 +1039,11 @@ exec_command_edit(PsqlScanState scan_state, bool active_branch, } if (ln) { - lineno = atoi(ln); - if (lineno < 1) + char *endptr; + + errno = 0; + lineno = strtoint(ln, &endptr, 10); + if (*endptr || errno == ERANGE || lineno < 1) { pg_log_error("invalid line number: %s", ln); status = PSQL_CMD_ERROR; @@ -4283,7 +4286,21 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "border") == 0) { if (value) - popt->topt.border = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr || errno == ERANGE || + new_value < 0 || new_value > 65535) + { + pg_log_error("\\pset: border is invalid or out of range"); + return false; + } + + popt->topt.border = new_value; + } } /* set expanded/vertical mode */ @@ -4439,7 +4456,20 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "pager_min_lines") == 0) { if (value) - popt->topt.pager_min_lines = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr || errno == ERANGE || new_value < 0) + { + pg_log_error("\\pset: pager_min_lines is invalid or out of range"); + return false; + } + + popt->topt.pager_min_lines = new_value; + } } /* disable "(x rows)" footer */ @@ -4455,7 +4485,19 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "columns") == 0) { if (value) - popt->topt.columns = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr || errno == ERANGE || new_value < 0) + { + pg_log_error("\\pset: column is invalid or out of range"); + return false; + } + popt->topt.columns = new_value; + } } else { diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c index fc0681538a..baa68d58d8 100644 --- a/src/bin/scripts/reindexdb.c +++ b/src/bin/scripts/reindexdb.c @@ -15,6 +15,7 @@ #include "common.h" #include "common/connect.h" #include "common/logging.h" +#include "common/string.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" #include "fe_utils/parallel_slot.h" @@ -109,6 +110,8 @@ main(int argc, char *argv[]) /* process command-line options */ while ((c = getopt_long(argc, argv, "h:p:U:wWeqS:d:ast:i:j:v", long_options, &optindex)) != -1) { + char *endptr; + switch (c) { case 'h': @@ -151,10 +154,11 @@ main(int argc, char *argv[]) simple_string_list_append(&indexes, optarg); break; case 'j': - concurrentCons = atoi(optarg); - if (concurrentCons <= 0) + errno = 0; + concurrentCons = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || concurrentCons <= 0) { - pg_log_error("number of parallel jobs must be at least 1"); + pg_log_error("number of parallel jobs must be at least 1: %s", optarg); exit(1); } break; diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c index 61974baa78..93b563998a 100644 --- a/src/bin/scripts/vacuumdb.c +++ b/src/bin/scripts/vacuumdb.c @@ -17,6 +17,7 @@ #include "common.h" #include "common/connect.h" #include "common/logging.h" +#include "common/string.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" #include "fe_utils/parallel_slot.h" @@ -141,6 +142,8 @@ main(int argc, char *argv[]) while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:zZFat:fvj:P:", long_options, &optindex)) != -1) { + char *endptr; + switch (c) { case 'h': @@ -192,16 +195,18 @@ main(int argc, char *argv[]) vacopts.verbose = true; break; case 'j': - concurrentCons = atoi(optarg); - if (concurrentCons <= 0) + errno = 0; + concurrentCons = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || concurrentCons <= 0) { pg_log_error("number of parallel jobs must be at least 1"); exit(1); } break; case 'P': - vacopts.parallel_workers = atoi(optarg); - if (vacopts.parallel_workers < 0) + errno = 0; + vacopts.parallel_workers = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || vacopts.parallel_workers < 0) { pg_log_error("parallel workers for vacuum must be greater than or equal to zero"); exit(1); @@ -220,16 +225,18 @@ main(int argc, char *argv[]) vacopts.skip_locked = true; break; case 6: - vacopts.min_xid_age = atoi(optarg); - if (vacopts.min_xid_age <= 0) + errno = 0; + vacopts.min_xid_age = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || vacopts.min_xid_age <= 0) { pg_log_error("minimum transaction ID age must be at least 1"); exit(1); } break; case 7: - vacopts.min_mxid_age = atoi(optarg); - if (vacopts.min_mxid_age <= 0) + errno = 0; + vacopts.min_mxid_age = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || vacopts.min_mxid_age <= 0) { pg_log_error("minimum multixact ID age must be at least 1"); exit(1); -- 2.27.0 ----Next_Part(Thu_Jul__8_17_30_23_2021_499)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="0002-Make-complain-for-invalid-numeirc-values-in-environe.patch" ^ permalink raw reply [nested|flat] 7+ messages in thread
* [PATCH v2 1/2] Be strict in numeric parameters on command line @ 2021-07-08 06:08 Kyotaro Horiguchi <horikyota.ntt@gmail.com> 0 siblings, 0 replies; 7+ messages in thread From: Kyotaro Horiguchi @ 2021-07-08 06:08 UTC (permalink / raw) Some numeric command line parameters are tolerant of valid values followed by garbage like "123xyz". Be strict to reject such invalid values. Do the same for psql meta command parameters. --- src/bin/pg_amcheck/pg_amcheck.c | 6 ++- src/bin/pg_basebackup/pg_basebackup.c | 13 +++-- src/bin/pg_basebackup/pg_receivewal.c | 18 +++++-- src/bin/pg_basebackup/pg_recvlogical.c | 17 +++++-- src/bin/pg_checksums/pg_checksums.c | 7 ++- src/bin/pg_ctl/pg_ctl.c | 18 ++++++- src/bin/pg_dump/pg_dump.c | 39 ++++++++------- src/bin/pg_dump/pg_restore.c | 17 ++++--- src/bin/pg_upgrade/option.c | 21 ++++++-- src/bin/pgbench/pgbench.c | 66 ++++++++++++++++---------- src/bin/psql/command.c | 52 ++++++++++++++++++-- src/bin/scripts/reindexdb.c | 10 ++-- src/bin/scripts/vacuumdb.c | 23 +++++---- 13 files changed, 219 insertions(+), 88 deletions(-) diff --git a/src/bin/pg_amcheck/pg_amcheck.c b/src/bin/pg_amcheck/pg_amcheck.c index 4bde16fb4b..71a82f9b75 100644 --- a/src/bin/pg_amcheck/pg_amcheck.c +++ b/src/bin/pg_amcheck/pg_amcheck.c @@ -17,6 +17,7 @@ #include "catalog/pg_am_d.h" #include "catalog/pg_namespace_d.h" #include "common/logging.h" +#include "common/string.h" #include "common/username.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" @@ -326,8 +327,9 @@ main(int argc, char *argv[]) append_btree_pattern(&opts.exclude, optarg, encoding); break; case 'j': - opts.jobs = atoi(optarg); - if (opts.jobs < 1) + errno = 0; + opts.jobs = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || opts.jobs < 1) { pg_log_error("number of parallel jobs must be at least 1"); exit(1); diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 8bb0acf498..29be95b96a 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -2287,6 +2287,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "CD:F:r:RS:T:X:l:nNzZ:d:c:h:p:U:s:wWkvP", long_options, &option_index)) != -1) { + char *endptr; + switch (c) { case 'C': @@ -2371,8 +2373,10 @@ main(int argc, char **argv) #endif break; case 'Z': - compresslevel = atoi(optarg); - if (compresslevel < 0 || compresslevel > 9) + errno = 0; + compresslevel = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + compresslevel < 0 || compresslevel > 9) { pg_log_error("invalid compression level \"%s\"", optarg); exit(1); @@ -2409,8 +2413,9 @@ main(int argc, char **argv) dbgetpassword = 1; break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr || errno == ERANGE || standby_message_timeout < 0) { pg_log_error("invalid status interval \"%s\"", optarg); exit(1); diff --git a/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c index c1334fad35..7fef925b99 100644 --- a/src/bin/pg_basebackup/pg_receivewal.c +++ b/src/bin/pg_basebackup/pg_receivewal.c @@ -22,6 +22,7 @@ #include "access/xlog_internal.h" #include "common/file_perm.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "libpq-fe.h" #include "receivelog.h" @@ -520,6 +521,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "D:d:E:h:p:U:s:S:nwWvZ:", long_options, &option_index)) != -1) { + char *endptr; + switch (c) { case 'D': @@ -532,7 +535,9 @@ main(int argc, char **argv) dbhost = pg_strdup(optarg); break; case 'p': - if (atoi(optarg) <= 0) + errno = 0; + if (strtoint(optarg, &endptr, 10) <= 0 || + *endptr || errno == ERANGE) { pg_log_error("invalid port number \"%s\"", optarg); exit(1); @@ -549,8 +554,9 @@ main(int argc, char **argv) dbgetpassword = 1; break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr || errno == ERANGE || standby_message_timeout < 0) { pg_log_error("invalid status interval \"%s\"", optarg); exit(1); @@ -574,8 +580,10 @@ main(int argc, char **argv) verbose++; break; case 'Z': - compresslevel = atoi(optarg); - if (compresslevel < 0 || compresslevel > 9) + errno = 0; + compresslevel = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + compresslevel < 0 || compresslevel > 9) { pg_log_error("invalid compression level \"%s\"", optarg); exit(1); diff --git a/src/bin/pg_basebackup/pg_recvlogical.c b/src/bin/pg_basebackup/pg_recvlogical.c index 76bd153fac..7be932d025 100644 --- a/src/bin/pg_basebackup/pg_recvlogical.c +++ b/src/bin/pg_basebackup/pg_recvlogical.c @@ -23,6 +23,7 @@ #include "common/fe_memutils.h" #include "common/file_perm.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "libpq-fe.h" #include "libpq/pqsignal.h" @@ -732,6 +733,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "E:f:F:nvtd:h:p:U:wWI:o:P:s:S:", long_options, &option_index)) != -1) { + char *endptr; + switch (c) { /* general options */ @@ -739,8 +742,9 @@ main(int argc, char **argv) outfile = pg_strdup(optarg); break; case 'F': - fsync_interval = atoi(optarg) * 1000; - if (fsync_interval < 0) + errno = 0; + fsync_interval = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr || errno == ERANGE || fsync_interval < 0) { pg_log_error("invalid fsync interval \"%s\"", optarg); exit(1); @@ -763,7 +767,9 @@ main(int argc, char **argv) dbhost = pg_strdup(optarg); break; case 'p': - if (atoi(optarg) <= 0) + errno = 0; + if (strtoint(optarg, &endptr, 10) <= 0 || + *endptr || errno == ERANGE) { pg_log_error("invalid port number \"%s\"", optarg); exit(1); @@ -820,8 +826,9 @@ main(int argc, char **argv) plugin = pg_strdup(optarg); break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr || errno == ERANGE || standby_message_timeout < 0) { pg_log_error("invalid status interval \"%s\"", optarg); exit(1); diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c index 3c326906e2..1c4e5b9d85 100644 --- a/src/bin/pg_checksums/pg_checksums.c +++ b/src/bin/pg_checksums/pg_checksums.c @@ -24,6 +24,7 @@ #include "common/file_perm.h" #include "common/file_utils.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "pg_getopt.h" #include "storage/bufpage.h" @@ -506,6 +507,8 @@ main(int argc, char *argv[]) while ((c = getopt_long(argc, argv, "cD:deNPf:v", long_options, &option_index)) != -1) { + char *endptr; + switch (c) { case 'c': @@ -518,7 +521,9 @@ main(int argc, char *argv[]) mode = PG_MODE_ENABLE; break; case 'f': - if (atoi(optarg) == 0) + errno = 0; + if (strtoint(optarg, &endptr, 10) == 0 + || *endptr || errno == ERANGE) { pg_log_error("invalid filenode specification, must be numeric: %s", optarg); exit(1); diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 7985da0a94..d6a39182cf 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -2331,6 +2331,8 @@ main(int argc, char **argv) /* process command-line options */ while (optind < argc) { + char *endptr; + while ((c = getopt_long(argc, argv, "cD:e:l:m:N:o:p:P:sS:t:U:wW", long_options, &option_index)) != -1) { @@ -2396,7 +2398,13 @@ main(int argc, char **argv) #endif break; case 't': - wait_seconds = atoi(optarg); + errno = 0; + wait_seconds = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || wait_seconds < 1) + { + pg_log_error("invalid timeout value \"%s\", use --no-wait to finish without waiting", optarg); + exit(1); + } wait_seconds_arg = true; break; case 'U': @@ -2459,7 +2467,13 @@ main(int argc, char **argv) } ctl_command = KILL_COMMAND; set_sig(argv[++optind]); - killproc = atol(argv[++optind]); + errno = 0; + killproc = strtol(argv[++optind], &endptr, 10); + if (*endptr || errno == ERANGE || killproc < 0) + { + pg_log_error("invalid process ID \"%s\"", argv[optind]); + exit(1); + } } #ifdef WIN32 else if (strcmp(argv[optind], "register") == 0) diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 321152151d..793f4b3509 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -54,6 +54,7 @@ #include "catalog/pg_trigger_d.h" #include "catalog/pg_type_d.h" #include "common/connect.h" +#include "common/string.h" #include "dumputils.h" #include "fe_utils/string_utils.h" #include "getopt_long.h" @@ -486,7 +487,19 @@ main(int argc, char **argv) break; case 'j': /* number of dump jobs */ - numWorkers = atoi(optarg); + errno = 0; + numWorkers = strtoint(optarg, &endptr, 10); + /* + * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS + * (= 64 usually) parallel jobs because that's the maximum + * limit for the WaitForMultipleObjects() call. + */ + if (*endptr || errno == ERANGE || numWorkers <= 0 +#ifdef WIN32 + || numWorkers > MAXIMUM_WAIT_OBJECTS +#endif + ) + fatal("invalid number of parallel jobs %s", optarg); break; case 'n': /* include schema(s) */ @@ -549,8 +562,10 @@ main(int argc, char **argv) break; case 'Z': /* Compression Level */ - compressLevel = atoi(optarg); - if (compressLevel < 0 || compressLevel > 9) + errno = 0; + compressLevel = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + compressLevel < 0 || compressLevel > 9) { pg_log_error("compression level must be in range 0..9"); exit_nicely(1); @@ -587,8 +602,10 @@ main(int argc, char **argv) case 8: have_extra_float_digits = true; - extra_float_digits = atoi(optarg); - if (extra_float_digits < -15 || extra_float_digits > 3) + errno = 0; + extra_float_digits = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + extra_float_digits < -15 || extra_float_digits > 3) { pg_log_error("extra_float_digits must be in range -15..3"); exit_nicely(1); @@ -719,18 +736,6 @@ main(int argc, char **argv) if (!plainText) dopt.outputCreateDB = 1; - /* - * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS (= 64 usually) - * parallel jobs because that's the maximum limit for the - * WaitForMultipleObjects() call. - */ - if (numWorkers <= 0 -#ifdef WIN32 - || numWorkers > MAXIMUM_WAIT_OBJECTS -#endif - ) - fatal("invalid number of parallel jobs"); - /* Parallel backup only in the directory archive format so far */ if (archiveFormat != archDirectory && numWorkers > 1) fatal("parallel backup only supported by the directory format"); diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 589b4aed53..285a09aaac 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -39,6 +39,7 @@ *------------------------------------------------------------------------- */ #include "postgres_fe.h" +#include "common/string.h" #include <ctype.h> #ifdef HAVE_TERMIOS_H @@ -151,6 +152,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "acCd:ef:F:h:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1", cmdopts, NULL)) != -1) { + char *endptr; + switch (c) { case 'a': /* Dump data only */ @@ -181,7 +184,13 @@ main(int argc, char **argv) break; case 'j': /* number of restore jobs */ - numWorkers = atoi(optarg); + errno = 0; + numWorkers = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || numWorkers <= 0) + { + pg_log_error("invalid number of parallel jobs"); + exit(1); + } break; case 'l': /* Dump the TOC summary */ @@ -344,12 +353,6 @@ main(int argc, char **argv) exit_nicely(1); } - if (numWorkers <= 0) - { - pg_log_error("invalid number of parallel jobs"); - exit(1); - } - /* See comments in pg_dump.c */ #ifdef WIN32 if (numWorkers > MAXIMUM_WAIT_OBJECTS) diff --git a/src/bin/pg_upgrade/option.c b/src/bin/pg_upgrade/option.c index 64bbda5650..f96f0d1e2a 100644 --- a/src/bin/pg_upgrade/option.c +++ b/src/bin/pg_upgrade/option.c @@ -104,6 +104,8 @@ parseCommandLine(int argc, char *argv[]) while ((option = getopt_long(argc, argv, "d:D:b:B:cj:ko:O:p:P:rs:U:v", long_options, &optindex)) != -1) { + char *endptr; + switch (option) { case 'b': @@ -127,7 +129,12 @@ parseCommandLine(int argc, char *argv[]) break; case 'j': - user_opts.jobs = atoi(optarg); + errno = 0; + user_opts.jobs = strtoint(optarg, &endptr, 10); + /**/ + if (*endptr || errno == ERANGE || user_opts.jobs < 1) + pg_fatal("invalid number of jobs %s\n", optarg); + break; case 'k': @@ -166,13 +173,17 @@ parseCommandLine(int argc, char *argv[]) * supported on all old/new versions (added in PG 9.2). */ case 'p': - if ((old_cluster.port = atoi(optarg)) <= 0) - pg_fatal("invalid old port number\n"); + errno = 0; + if ((old_cluster.port = strtoint(optarg, &endptr, 10)) <= 0 || + *endptr || errno == ERANGE) + pg_fatal("invalid old port number %s\n", optarg); break; case 'P': - if ((new_cluster.port = atoi(optarg)) <= 0) - pg_fatal("invalid new port number\n"); + errno = 0; + if ((new_cluster.port = strtoint(optarg, &endptr, 10)) <= 0 || + *endptr || errno == ERANGE) + pg_fatal("invalid new port number %s\n", optarg); break; case 'r': diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 4aeccd93af..4020347585 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -5838,6 +5838,7 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "iI:h:nvp:dqb:SNc:j:Crs:t:T:U:lf:D:F:M:P:R:L:", long_options, &optindex)) != -1) { char *script; + char *endptr; switch (c) { @@ -5869,8 +5870,9 @@ main(int argc, char **argv) break; case 'c': benchmarking_option_set = true; - nclients = atoi(optarg); - if (nclients <= 0) + errno = 0; + nclients = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || nclients <= 0) { pg_log_fatal("invalid number of clients: \"%s\"", optarg); exit(1); @@ -5896,8 +5898,9 @@ main(int argc, char **argv) break; case 'j': /* jobs */ benchmarking_option_set = true; - nthreads = atoi(optarg); - if (nthreads <= 0) + errno = 0; + nthreads = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || nthreads <= 0) { pg_log_fatal("invalid number of threads: \"%s\"", optarg); exit(1); @@ -5920,8 +5923,9 @@ main(int argc, char **argv) break; case 's': scale_given = true; - scale = atoi(optarg); - if (scale <= 0) + errno = 0; + scale = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || scale <= 0) { pg_log_fatal("invalid scaling factor: \"%s\"", optarg); exit(1); @@ -5929,8 +5933,9 @@ main(int argc, char **argv) break; case 't': benchmarking_option_set = true; - nxacts = atoi(optarg); - if (nxacts <= 0) + errno = 0; + nxacts = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || nxacts <= 0) { pg_log_fatal("invalid number of transactions: \"%s\"", optarg); exit(1); @@ -5938,8 +5943,9 @@ main(int argc, char **argv) break; case 'T': benchmarking_option_set = true; - duration = atoi(optarg); - if (duration <= 0) + errno = 0; + duration = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || duration <= 0) { pg_log_fatal("invalid duration: \"%s\"", optarg); exit(1); @@ -6001,8 +6007,10 @@ main(int argc, char **argv) break; case 'F': initialization_option_set = true; - fillfactor = atoi(optarg); - if (fillfactor < 10 || fillfactor > 100) + errno = 0; + fillfactor = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + fillfactor < 10 || fillfactor > 100) { pg_log_fatal("invalid fillfactor: \"%s\"", optarg); exit(1); @@ -6021,8 +6029,9 @@ main(int argc, char **argv) break; case 'P': benchmarking_option_set = true; - progress = atoi(optarg); - if (progress <= 0) + errno = 0; + progress = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || progress <= 0) { pg_log_fatal("invalid thread progress delay: \"%s\"", optarg); exit(1); @@ -6031,11 +6040,13 @@ main(int argc, char **argv) case 'R': { /* get a double from the beginning of option value */ - double throttle_value = atof(optarg); + double throttle_value; + errno = 0; + throttle_value = strtod(optarg, &endptr); benchmarking_option_set = true; - if (throttle_value <= 0.0) + if (*endptr || errno == ERANGE || throttle_value <= 0.0) { pg_log_fatal("invalid rate limit: \"%s\"", optarg); exit(1); @@ -6046,9 +6057,12 @@ main(int argc, char **argv) break; case 'L': { - double limit_ms = atof(optarg); + double limit_ms; - if (limit_ms <= 0.0) + errno = 0; + limit_ms = strtod(optarg, &endptr); + + if (*endptr || errno == ERANGE || limit_ms <= 0.0) { pg_log_fatal("invalid latency limit: \"%s\"", optarg); exit(1); @@ -6071,8 +6085,10 @@ main(int argc, char **argv) break; case 4: /* sampling-rate */ benchmarking_option_set = true; - sample_rate = atof(optarg); - if (sample_rate <= 0.0 || sample_rate > 1.0) + errno = 0; + sample_rate = strtod(optarg, &endptr); + if (*endptr || errno == ERANGE || + sample_rate <= 0.0 || sample_rate > 1.0) { pg_log_fatal("invalid sampling rate: \"%s\"", optarg); exit(1); @@ -6080,8 +6096,9 @@ main(int argc, char **argv) break; case 5: /* aggregate-interval */ benchmarking_option_set = true; - agg_interval = atoi(optarg); - if (agg_interval <= 0) + errno = 0; + agg_interval = strtod(optarg, &endptr); + if (*endptr || errno == ERANGE || agg_interval <= 0) { pg_log_fatal("invalid number of seconds for aggregation: \"%s\"", optarg); exit(1); @@ -6117,8 +6134,9 @@ main(int argc, char **argv) break; case 11: /* partitions */ initialization_option_set = true; - partitions = atoi(optarg); - if (partitions < 0) + errno = 0; + partitions = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || partitions < 0) { pg_log_fatal("invalid number of partitions: \"%s\"", optarg); exit(1); diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 543401c6d6..aaed986ae1 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -1039,8 +1039,11 @@ exec_command_edit(PsqlScanState scan_state, bool active_branch, } if (ln) { - lineno = atoi(ln); - if (lineno < 1) + char *endptr; + + errno = 0; + lineno = strtoint(ln, &endptr, 10); + if (*endptr || errno == ERANGE || lineno < 1) { pg_log_error("invalid line number: %s", ln); status = PSQL_CMD_ERROR; @@ -4283,7 +4286,21 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "border") == 0) { if (value) - popt->topt.border = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr || errno == ERANGE || + new_value < 0 || new_value > 65535) + { + pg_log_error("\\pset: border is invalid or out of range"); + return false; + } + + popt->topt.border = new_value; + } } /* set expanded/vertical mode */ @@ -4439,7 +4456,20 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "pager_min_lines") == 0) { if (value) - popt->topt.pager_min_lines = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr || errno == ERANGE || new_value < 0) + { + pg_log_error("\\pset: pager_min_lines is invalid or out of range"); + return false; + } + + popt->topt.pager_min_lines = new_value; + } } /* disable "(x rows)" footer */ @@ -4455,7 +4485,19 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "columns") == 0) { if (value) - popt->topt.columns = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr || errno == ERANGE || new_value < 0) + { + pg_log_error("\\pset: column is invalid or out of range"); + return false; + } + popt->topt.columns = new_value; + } } else { diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c index fc0681538a..baa68d58d8 100644 --- a/src/bin/scripts/reindexdb.c +++ b/src/bin/scripts/reindexdb.c @@ -15,6 +15,7 @@ #include "common.h" #include "common/connect.h" #include "common/logging.h" +#include "common/string.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" #include "fe_utils/parallel_slot.h" @@ -109,6 +110,8 @@ main(int argc, char *argv[]) /* process command-line options */ while ((c = getopt_long(argc, argv, "h:p:U:wWeqS:d:ast:i:j:v", long_options, &optindex)) != -1) { + char *endptr; + switch (c) { case 'h': @@ -151,10 +154,11 @@ main(int argc, char *argv[]) simple_string_list_append(&indexes, optarg); break; case 'j': - concurrentCons = atoi(optarg); - if (concurrentCons <= 0) + errno = 0; + concurrentCons = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || concurrentCons <= 0) { - pg_log_error("number of parallel jobs must be at least 1"); + pg_log_error("number of parallel jobs must be at least 1: %s", optarg); exit(1); } break; diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c index 61974baa78..93b563998a 100644 --- a/src/bin/scripts/vacuumdb.c +++ b/src/bin/scripts/vacuumdb.c @@ -17,6 +17,7 @@ #include "common.h" #include "common/connect.h" #include "common/logging.h" +#include "common/string.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" #include "fe_utils/parallel_slot.h" @@ -141,6 +142,8 @@ main(int argc, char *argv[]) while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:zZFat:fvj:P:", long_options, &optindex)) != -1) { + char *endptr; + switch (c) { case 'h': @@ -192,16 +195,18 @@ main(int argc, char *argv[]) vacopts.verbose = true; break; case 'j': - concurrentCons = atoi(optarg); - if (concurrentCons <= 0) + errno = 0; + concurrentCons = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || concurrentCons <= 0) { pg_log_error("number of parallel jobs must be at least 1"); exit(1); } break; case 'P': - vacopts.parallel_workers = atoi(optarg); - if (vacopts.parallel_workers < 0) + errno = 0; + vacopts.parallel_workers = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || vacopts.parallel_workers < 0) { pg_log_error("parallel workers for vacuum must be greater than or equal to zero"); exit(1); @@ -220,16 +225,18 @@ main(int argc, char *argv[]) vacopts.skip_locked = true; break; case 6: - vacopts.min_xid_age = atoi(optarg); - if (vacopts.min_xid_age <= 0) + errno = 0; + vacopts.min_xid_age = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || vacopts.min_xid_age <= 0) { pg_log_error("minimum transaction ID age must be at least 1"); exit(1); } break; case 7: - vacopts.min_mxid_age = atoi(optarg); - if (vacopts.min_mxid_age <= 0) + errno = 0; + vacopts.min_mxid_age = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || vacopts.min_mxid_age <= 0) { pg_log_error("minimum multixact ID age must be at least 1"); exit(1); -- 2.27.0 ----Next_Part(Fri_Jul__9_16_50_28_2021_228)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="v2-0002-Make-complain-for-invalid-numeirc-values-in-envir.patch" ^ permalink raw reply [nested|flat] 7+ messages in thread
* [PATCH v3 1/3] Be strict in numeric parameters on command line @ 2021-07-08 06:08 Kyotaro Horiguchi <horikyota.ntt@gmail.com> 0 siblings, 0 replies; 7+ messages in thread From: Kyotaro Horiguchi @ 2021-07-08 06:08 UTC (permalink / raw) Some numeric command line parameters are tolerant of valid values followed by garbage like "123xyz". Be strict to reject such invalid values. Do the same for psql meta command parameters. --- src/bin/pg_amcheck/pg_amcheck.c | 15 ++- src/bin/pg_basebackup/pg_basebackup.c | 24 +++- src/bin/pg_basebackup/pg_receivewal.c | 39 +++++-- src/bin/pg_basebackup/pg_recvlogical.c | 44 +++++-- src/bin/pg_checksums/pg_checksums.c | 17 ++- src/bin/pg_ctl/pg_ctl.c | 42 ++++++- src/bin/pg_dump/pg_dump.c | 57 ++++++--- src/bin/pg_dump/pg_restore.c | 42 ++++--- src/bin/pg_upgrade/option.c | 30 ++++- src/bin/pgbench/pgbench.c | 154 +++++++++++++++++++------ src/bin/psql/command.c | 73 +++++++++++- src/bin/scripts/reindexdb.c | 15 ++- src/bin/scripts/vacuumdb.c | 59 ++++++++-- 13 files changed, 484 insertions(+), 127 deletions(-) diff --git a/src/bin/pg_amcheck/pg_amcheck.c b/src/bin/pg_amcheck/pg_amcheck.c index 4bde16fb4b..f40d58ac96 100644 --- a/src/bin/pg_amcheck/pg_amcheck.c +++ b/src/bin/pg_amcheck/pg_amcheck.c @@ -17,6 +17,7 @@ #include "catalog/pg_am_d.h" #include "catalog/pg_namespace_d.h" #include "common/logging.h" +#include "common/string.h" #include "common/username.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" @@ -326,10 +327,18 @@ main(int argc, char *argv[]) append_btree_pattern(&opts.exclude, optarg, encoding); break; case 'j': - opts.jobs = atoi(optarg); - if (opts.jobs < 1) + errno = 0; + opts.jobs = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("number of parallel jobs must be at least 1"); + pg_log_error("number of parallel jobs out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || opts.jobs < 1) + { + pg_log_error("number of parallel jobs must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 8bb0acf498..c30005f569 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -2287,6 +2287,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "CD:F:r:RS:T:X:l:nNzZ:d:c:h:p:U:s:wWkvP", long_options, &option_index)) != -1) { + char *endptr; + switch (c) { case 'C': @@ -2371,10 +2373,12 @@ main(int argc, char **argv) #endif break; case 'Z': - compresslevel = atoi(optarg); - if (compresslevel < 0 || compresslevel > 9) + errno = 0; + compresslevel = strtoint(optarg, &endptr, 10); + if (*endptr || + errno == ERANGE || compresslevel < 0 || compresslevel > 9) { - pg_log_error("invalid compression level \"%s\"", optarg); + pg_log_error("compression level must be a digit in range 0..9: \"%s\"", optarg); exit(1); } break; @@ -2409,10 +2413,18 @@ main(int argc, char **argv) dbgetpassword = 1; break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid status interval \"%s\"", optarg); + pg_log_error("status interval out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || standby_message_timeout < 0) + { + pg_log_error("status interval must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; diff --git a/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c index c1334fad35..fb03147fe7 100644 --- a/src/bin/pg_basebackup/pg_receivewal.c +++ b/src/bin/pg_basebackup/pg_receivewal.c @@ -22,6 +22,7 @@ #include "access/xlog_internal.h" #include "common/file_perm.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "libpq-fe.h" #include "receivelog.h" @@ -520,6 +521,9 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "D:d:E:h:p:U:s:S:nwWvZ:", long_options, &option_index)) != -1) { + char *endptr; + int v; + switch (c) { case 'D': @@ -532,9 +536,17 @@ main(int argc, char **argv) dbhost = pg_strdup(optarg); break; case 'p': - if (atoi(optarg) <= 0) + errno = 0; + v = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid port number \"%s\"", optarg); + pg_log_error("port number out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || v < 1) + { + pg_log_error("port number must be an integer greater than zero: \"%s\"", + optarg); exit(1); } dbport = pg_strdup(optarg); @@ -549,10 +561,18 @@ main(int argc, char **argv) dbgetpassword = 1; break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid status interval \"%s\"", optarg); + pg_log_error("status interval out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || standby_message_timeout < 0) + { + pg_log_error("status interval must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; @@ -574,10 +594,13 @@ main(int argc, char **argv) verbose++; break; case 'Z': - compresslevel = atoi(optarg); - if (compresslevel < 0 || compresslevel > 9) + errno = 0; + compresslevel = strtoint(optarg, &endptr, 10); + if (*endptr || + errno == ERANGE || compresslevel < 0 || compresslevel > 9) { - pg_log_error("invalid compression level \"%s\"", optarg); + pg_log_error("compression level must be a digit in range 0..9: \"%s\"", + optarg); exit(1); } break; diff --git a/src/bin/pg_basebackup/pg_recvlogical.c b/src/bin/pg_basebackup/pg_recvlogical.c index 76bd153fac..9bc4902033 100644 --- a/src/bin/pg_basebackup/pg_recvlogical.c +++ b/src/bin/pg_basebackup/pg_recvlogical.c @@ -23,6 +23,7 @@ #include "common/fe_memutils.h" #include "common/file_perm.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "libpq-fe.h" #include "libpq/pqsignal.h" @@ -732,6 +733,9 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "E:f:F:nvtd:h:p:U:wWI:o:P:s:S:", long_options, &option_index)) != -1) { + char *endptr; + int v; + switch (c) { /* general options */ @@ -739,10 +743,18 @@ main(int argc, char **argv) outfile = pg_strdup(optarg); break; case 'F': - fsync_interval = atoi(optarg) * 1000; - if (fsync_interval < 0) + errno = 0; + fsync_interval = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid fsync interval \"%s\"", optarg); + pg_log_error("fsync interval out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || fsync_interval < 0) + { + pg_log_error("fsync interval must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; @@ -763,9 +775,17 @@ main(int argc, char **argv) dbhost = pg_strdup(optarg); break; case 'p': - if (atoi(optarg) <= 0) + errno = 0; + v = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid port number \"%s\"", optarg); + pg_log_error("port number out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || v < 1) + { + pg_log_error("port number must be an integer greater than zero: \"%s\"", + optarg); exit(1); } dbport = pg_strdup(optarg); @@ -820,10 +840,18 @@ main(int argc, char **argv) plugin = pg_strdup(optarg); break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid status interval \"%s\"", optarg); + pg_log_error("status interval out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || standby_message_timeout < 0) + { + pg_log_error("status interval must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c index 3c326906e2..78a1d4ef38 100644 --- a/src/bin/pg_checksums/pg_checksums.c +++ b/src/bin/pg_checksums/pg_checksums.c @@ -24,6 +24,7 @@ #include "common/file_perm.h" #include "common/file_utils.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "pg_getopt.h" #include "storage/bufpage.h" @@ -506,6 +507,9 @@ main(int argc, char *argv[]) while ((c = getopt_long(argc, argv, "cD:deNPf:v", long_options, &option_index)) != -1) { + char *endptr; + int v; + switch (c) { case 'c': @@ -518,9 +522,18 @@ main(int argc, char *argv[]) mode = PG_MODE_ENABLE; break; case 'f': - if (atoi(optarg) == 0) + errno = 0; + v = strtoint(optarg, &endptr, 10); + if(*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid filenode specification, must be numeric: %s", optarg); + pg_log_error("filenode specification out of range: %s", + optarg); + exit(1); + } + if(*endptr || v < 1) + { + pg_log_error("filenode specification must be an integer greater than zero: %s", + optarg); exit(1); } only_filenode = pstrdup(optarg); diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 7985da0a94..0f72ef016b 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -76,6 +76,7 @@ typedef enum #define WAITS_PER_SEC 10 /* should divide USEC_PER_SEC evenly */ +static bool do_wait_arg = false; static bool do_wait = true; static int wait_seconds = DEFAULT_WAIT; static bool wait_seconds_arg = false; @@ -2331,6 +2332,8 @@ main(int argc, char **argv) /* process command-line options */ while (optind < argc) { + char *endptr; + while ((c = getopt_long(argc, argv, "cD:e:l:m:N:o:p:P:sS:t:U:wW", long_options, &option_index)) != -1) { @@ -2396,7 +2399,20 @@ main(int argc, char **argv) #endif break; case 't': - wait_seconds = atoi(optarg); + errno = 0; + wait_seconds = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + { + pg_log_error("timeout value out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || wait_seconds < 0) + { + pg_log_error("timeout value must be a non-negative integer: \"%s\"", + optarg); + exit(1); + } wait_seconds_arg = true; break; case 'U': @@ -2408,6 +2424,7 @@ main(int argc, char **argv) break; case 'w': do_wait = true; + do_wait_arg = true; break; case 'W': do_wait = false; @@ -2459,7 +2476,20 @@ main(int argc, char **argv) } ctl_command = KILL_COMMAND; set_sig(argv[++optind]); - killproc = atol(argv[++optind]); + errno = 0; + killproc = strtol(argv[++optind], &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + { + pg_log_error("process ID out of range: \"%s\"", + argv[optind]); + exit(1); + } + if (*endptr || killproc < 0) + { + pg_log_error("process ID must be a non-negative integer: \"%s\"", + argv[optind]); + exit(1); + } } #ifdef WIN32 else if (strcmp(argv[optind], "register") == 0) @@ -2514,6 +2544,14 @@ main(int argc, char **argv) do_wait = false; } + if (wait_seconds == 0 && do_wait) + { + /* Warn if user instructed to wait but we actually don't */ + if (!silent_mode && do_wait_arg) + write_stderr(_("%s: WARNING: -w is ignored because timeout is set to 0\n"), progname); + do_wait = false; + } + if (pg_data) { snprintf(postopts_file, MAXPGPATH, "%s/postmaster.opts", pg_data); diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 321152151d..8ef29b37f6 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -54,6 +54,7 @@ #include "catalog/pg_trigger_d.h" #include "catalog/pg_type_d.h" #include "common/connect.h" +#include "common/string.h" #include "dumputils.h" #include "fe_utils/string_utils.h" #include "getopt_long.h" @@ -103,6 +104,17 @@ static Oid g_last_builtin_oid; /* value of the last builtin oid */ /* The specified names/patterns should to match at least one entity */ static int strict_names = 0; +/* + * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS (= 64 usually) + * parallel jobs because that's the maximum limit for the + * WaitForMultipleObjects() call. + */ +#ifndef WIN32 +#define MAX_NUM_WORKERS INT_MAX +#else +#define MAX_NUM_WORKERS MAXIMUM_WAIT_OBJECTS +#endif + /* * Object inclusion/exclusion lists * @@ -486,7 +498,21 @@ main(int argc, char **argv) break; case 'j': /* number of dump jobs */ - numWorkers = atoi(optarg); + errno = 0; + numWorkers = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && + (errno == ERANGE || numWorkers > MAX_NUM_WORKERS)) + { + pg_log_error("number of parallel jobs out of range: \"%s\"", + optarg); + exit_nicely(1); + } + if (*endptr || numWorkers <= 0) + { + pg_log_error("number of parallel jobs must be an integer greater than zero: \"%s\"", + optarg); + exit_nicely(1); + } break; case 'n': /* include schema(s) */ @@ -549,10 +575,12 @@ main(int argc, char **argv) break; case 'Z': /* Compression Level */ - compressLevel = atoi(optarg); - if (compressLevel < 0 || compressLevel > 9) + errno = 0; + compressLevel = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + compressLevel < 0 || compressLevel > 9) { - pg_log_error("compression level must be in range 0..9"); + pg_log_error("compression level must be a digit in range 0..9: \"%s\"", optarg); exit_nicely(1); } break; @@ -587,10 +615,13 @@ main(int argc, char **argv) case 8: have_extra_float_digits = true; - extra_float_digits = atoi(optarg); - if (extra_float_digits < -15 || extra_float_digits > 3) + errno = 0; + extra_float_digits = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + extra_float_digits < -15 || extra_float_digits > 3) { - pg_log_error("extra_float_digits must be in range -15..3"); + pg_log_error("extra_float_digits must be an integer in range -15..3: \"%s\"", + optarg); exit_nicely(1); } break; @@ -719,18 +750,6 @@ main(int argc, char **argv) if (!plainText) dopt.outputCreateDB = 1; - /* - * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS (= 64 usually) - * parallel jobs because that's the maximum limit for the - * WaitForMultipleObjects() call. - */ - if (numWorkers <= 0 -#ifdef WIN32 - || numWorkers > MAXIMUM_WAIT_OBJECTS -#endif - ) - fatal("invalid number of parallel jobs"); - /* Parallel backup only in the directory archive format so far */ if (archiveFormat != archDirectory && numWorkers > 1) fatal("parallel backup only supported by the directory format"); diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 589b4aed53..3bb5a48c55 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -39,6 +39,7 @@ *------------------------------------------------------------------------- */ #include "postgres_fe.h" +#include "common/string.h" #include <ctype.h> #ifdef HAVE_TERMIOS_H @@ -52,6 +53,13 @@ static void usage(const char *progname); +/* See comments in pg_dump.c */ +#ifndef WIN32 +#define MAX_NUM_WORKERS INT_MAX +#else +#define MAX_NUM_WORKERS MAXIMUM_WAIT_OBJECTS +#endif + int main(int argc, char **argv) { @@ -151,6 +159,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "acCd:ef:F:h:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1", cmdopts, NULL)) != -1) { + char *endptr; + switch (c) { case 'a': /* Dump data only */ @@ -181,7 +191,21 @@ main(int argc, char **argv) break; case 'j': /* number of restore jobs */ - numWorkers = atoi(optarg); + errno = 0; + numWorkers = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && + (errno == ERANGE || numWorkers > MAX_NUM_WORKERS)) + { + pg_log_error("number of parallel jobs out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || numWorkers <= 0) + { + pg_log_error("number of parallel jobs must be an integer greater than zero: \"%s\"", + optarg); + exit(1); + } break; case 'l': /* Dump the TOC summary */ @@ -344,22 +368,6 @@ main(int argc, char **argv) exit_nicely(1); } - if (numWorkers <= 0) - { - pg_log_error("invalid number of parallel jobs"); - exit(1); - } - - /* See comments in pg_dump.c */ -#ifdef WIN32 - if (numWorkers > MAXIMUM_WAIT_OBJECTS) - { - pg_log_error("maximum number of parallel jobs is %d", - MAXIMUM_WAIT_OBJECTS); - exit(1); - } -#endif - /* Can't do single-txn mode with multiple connections */ if (opts->single_txn && numWorkers > 1) { diff --git a/src/bin/pg_upgrade/option.c b/src/bin/pg_upgrade/option.c index 64bbda5650..c014bbca0d 100644 --- a/src/bin/pg_upgrade/option.c +++ b/src/bin/pg_upgrade/option.c @@ -104,6 +104,8 @@ parseCommandLine(int argc, char *argv[]) while ((option = getopt_long(argc, argv, "d:D:b:B:cj:ko:O:p:P:rs:U:v", long_options, &optindex)) != -1) { + char *endptr; + switch (option) { case 'b': @@ -127,7 +129,15 @@ parseCommandLine(int argc, char *argv[]) break; case 'j': - user_opts.jobs = atoi(optarg); + errno = 0; + user_opts.jobs = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + pg_fatal("number of parallel jobs out of range: \"%s\"\n", + optarg); + if (*endptr || user_opts.jobs < 1) + pg_fatal("number of parallel jobs must be an integer greater than zero: \"%s\"\n", + optarg); + break; case 'k': @@ -166,13 +176,23 @@ parseCommandLine(int argc, char *argv[]) * supported on all old/new versions (added in PG 9.2). */ case 'p': - if ((old_cluster.port = atoi(optarg)) <= 0) - pg_fatal("invalid old port number\n"); + errno = 0; + old_cluster.port = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + pg_fatal("old port number out of range: \"%s\"\n", optarg); + if (*endptr || old_cluster.port <= 0) + pg_fatal("old port number must be an integer greater than zero: \"%s\"\n", + optarg); break; case 'P': - if ((new_cluster.port = atoi(optarg)) <= 0) - pg_fatal("invalid new port number\n"); + errno = 0; + new_cluster.port = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + pg_fatal("new port number out of range: \"%s\"\n", optarg); + if (*endptr || new_cluster.port <= 0) + pg_fatal("new port number must be an integer greater than zero: \"%s\"\n", + optarg); break; case 'r': diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 364b5a2e47..1c3b5836c1 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -5856,6 +5856,7 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "iI:h:nvp:dqb:SNc:j:Crs:t:T:U:lf:D:F:M:P:R:L:", long_options, &optindex)) != -1) { char *script; + char *endptr; switch (c) { @@ -5887,10 +5888,18 @@ main(int argc, char **argv) break; case 'c': benchmarking_option_set = true; - nclients = atoi(optarg); - if (nclients <= 0) + errno = 0; + nclients = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid number of clients: \"%s\"", optarg); + pg_log_fatal("number of clients out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || nclients <= 0) + { + pg_log_fatal("number of clients must be an integer greater than zero: \"%s\"", + optarg); exit(1); } #ifdef HAVE_GETRLIMIT @@ -5914,10 +5923,18 @@ main(int argc, char **argv) break; case 'j': /* jobs */ benchmarking_option_set = true; - nthreads = atoi(optarg); - if (nthreads <= 0) + errno = 0; + nthreads = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid number of threads: \"%s\"", optarg); + pg_log_fatal("number of threads out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || nthreads <= 0) + { + pg_log_fatal("number of threads must be an integer greater than zero: \"%s\"", + optarg); exit(1); } #ifndef ENABLE_THREAD_SAFETY @@ -5938,28 +5955,50 @@ main(int argc, char **argv) break; case 's': scale_given = true; - scale = atoi(optarg); - if (scale <= 0) + errno = 0; + scale = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid scaling factor: \"%s\"", optarg); + pg_log_fatal("scaling factor out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || scale <= 0) + { + pg_log_fatal("scaling factor must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; case 't': benchmarking_option_set = true; - nxacts = atoi(optarg); - if (nxacts <= 0) + errno = 0; + nxacts = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid number of transactions: \"%s\"", optarg); + pg_log_fatal("number of transactions out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || nxacts <= 0) + { + pg_log_fatal("number of transactions must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; case 'T': benchmarking_option_set = true; - duration = atoi(optarg); - if (duration <= 0) + errno = 0; + duration = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid duration: \"%s\"", optarg); + pg_log_fatal("duration out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || duration <= 0) + { + pg_log_fatal("duration must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; @@ -6019,10 +6058,13 @@ main(int argc, char **argv) break; case 'F': initialization_option_set = true; - fillfactor = atoi(optarg); - if (fillfactor < 10 || fillfactor > 100) + errno = 0; + fillfactor = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + fillfactor < 10 || fillfactor > 100) { - pg_log_fatal("invalid fillfactor: \"%s\"", optarg); + pg_log_fatal("fillfactor must be an ineger between 10 and 100: \"%s\"", + optarg); exit(1); } break; @@ -6039,23 +6081,38 @@ main(int argc, char **argv) break; case 'P': benchmarking_option_set = true; - progress = atoi(optarg); - if (progress <= 0) + errno = 0; + progress = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid thread progress delay: \"%s\"", optarg); + pg_log_fatal("thread progress delay out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || progress <= 0) + { + pg_log_fatal("thread progress delay must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; case 'R': { /* get a double from the beginning of option value */ - double throttle_value = atof(optarg); + double throttle_value; + errno = 0; + throttle_value = strtod(optarg, &endptr); benchmarking_option_set = true; - if (throttle_value <= 0.0) + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid rate limit: \"%s\"", optarg); + pg_log_fatal("rate limit out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || throttle_value <= 0.0) + { + pg_log_fatal("rate limit must be a real number greater than zero: \"%s\"", optarg); exit(1); } /* Invert rate limit into per-transaction delay in usec */ @@ -6064,11 +6121,20 @@ main(int argc, char **argv) break; case 'L': { - double limit_ms = atof(optarg); + double limit_ms; - if (limit_ms <= 0.0) + errno = 0; + limit_ms = strtod(optarg, &endptr); + + if (*endptr == 0 && errno == ERANGE) + { + pg_log_fatal("latency limit out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || limit_ms <= 0.0) { - pg_log_fatal("invalid latency limit: \"%s\"", optarg); + pg_log_fatal("latency limit must be a real number greater than zero: \"%s\"", optarg); exit(1); } benchmarking_option_set = true; @@ -6089,19 +6155,27 @@ main(int argc, char **argv) break; case 4: /* sampling-rate */ benchmarking_option_set = true; - sample_rate = atof(optarg); - if (sample_rate <= 0.0 || sample_rate > 1.0) + errno = 0; + sample_rate = strtod(optarg, &endptr); + if (*endptr || errno == ERANGE || + sample_rate <= 0.0 || sample_rate > 1.0) { - pg_log_fatal("invalid sampling rate: \"%s\"", optarg); + pg_log_fatal("sampling rate must be an real number between 0.0 and 1.0: \"%s\"", optarg); exit(1); } break; case 5: /* aggregate-interval */ benchmarking_option_set = true; - agg_interval = atoi(optarg); - if (agg_interval <= 0) + errno = 0; + agg_interval = strtod(optarg, &endptr); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid number of seconds for aggregation: \"%s\"", optarg); + pg_log_fatal("aggregate interval out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || agg_interval <= 0) + { + pg_log_fatal("aggregate interval must be a real number greater than zero: \"%s\"", optarg); exit(1); } break; @@ -6135,10 +6209,18 @@ main(int argc, char **argv) break; case 11: /* partitions */ initialization_option_set = true; - partitions = atoi(optarg); - if (partitions < 0) + errno = 0; + partitions = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid number of partitions: \"%s\"", optarg); + pg_log_fatal("number of partitions out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || partitions < 0) + { + pg_log_fatal("number of partitions must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index d704c4220c..13074051b3 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -1040,10 +1040,18 @@ exec_command_edit(PsqlScanState scan_state, bool active_branch, } if (ln) { - lineno = atoi(ln); - if (lineno < 1) + char *endptr; + + errno = 0; + lineno = strtoint(ln, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid line number: %s", ln); + pg_log_error("line number out of range: %s", ln); + status = PSQL_CMD_ERROR; + } + if (*endptr || lineno < 1) + { + pg_log_error("line number must be an integer greater than zero: %s", ln); status = PSQL_CMD_ERROR; } } @@ -4284,7 +4292,25 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "border") == 0) { if (value) - popt->topt.border = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr == 0 && (errno == ERANGE || new_value > 65535)) + { + pg_log_error("\\pset: border out of range"); + return false; + } + if (*endptr || new_value < 0) + { + pg_log_error("\\pset: border must be an integer greater than zero"); + return false; + } + + popt->topt.border = new_value; + } } /* set expanded/vertical mode */ @@ -4440,7 +4466,25 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "pager_min_lines") == 0) { if (value) - popt->topt.pager_min_lines = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + { + pg_log_error("\\pset: pager_min_lines out of range"); + return false; + } + if (*endptr || new_value < 0) + { + pg_log_error("\\pset: pager_min_lines must be a non-negative integer"); + return false; + } + + popt->topt.pager_min_lines = new_value; + } } /* disable "(x rows)" footer */ @@ -4456,7 +4500,24 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "columns") == 0) { if (value) - popt->topt.columns = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + { + pg_log_error("\\pset: column out of range"); + return false; + } + if (*endptr || new_value < 0) + { + pg_log_error("\\pset: column must be a non-negative integer"); + return false; + } + popt->topt.columns = new_value; + } } else { diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c index fc0681538a..42d4d20768 100644 --- a/src/bin/scripts/reindexdb.c +++ b/src/bin/scripts/reindexdb.c @@ -15,6 +15,7 @@ #include "common.h" #include "common/connect.h" #include "common/logging.h" +#include "common/string.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" #include "fe_utils/parallel_slot.h" @@ -109,6 +110,8 @@ main(int argc, char *argv[]) /* process command-line options */ while ((c = getopt_long(argc, argv, "h:p:U:wWeqS:d:ast:i:j:v", long_options, &optindex)) != -1) { + char *endptr; + switch (c) { case 'h': @@ -151,10 +154,16 @@ main(int argc, char *argv[]) simple_string_list_append(&indexes, optarg); break; case 'j': - concurrentCons = atoi(optarg); - if (concurrentCons <= 0) + errno = 0; + concurrentCons = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("number of parallel jobs must be at least 1"); + pg_log_error("number of parallel jobs out of range: %s", optarg); + exit(1); + } + if (*endptr || concurrentCons <= 0) + { + pg_log_error("number of parallel jobs must be an integer greater than zero: %s", optarg); exit(1); } break; diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c index 61974baa78..6b2a34edd0 100644 --- a/src/bin/scripts/vacuumdb.c +++ b/src/bin/scripts/vacuumdb.c @@ -17,6 +17,7 @@ #include "common.h" #include "common/connect.h" #include "common/logging.h" +#include "common/string.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" #include "fe_utils/parallel_slot.h" @@ -141,6 +142,8 @@ main(int argc, char *argv[]) while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:zZFat:fvj:P:", long_options, &optindex)) != -1) { + char *endptr; + switch (c) { case 'h': @@ -192,18 +195,34 @@ main(int argc, char *argv[]) vacopts.verbose = true; break; case 'j': - concurrentCons = atoi(optarg); - if (concurrentCons <= 0) + errno = 0; + concurrentCons = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("number of parallel jobs must be at least 1"); + pg_log_error("number of parallel jobs out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || concurrentCons <= 0) + { + pg_log_error("number of parallel jobs must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; case 'P': - vacopts.parallel_workers = atoi(optarg); - if (vacopts.parallel_workers < 0) + errno = 0; + vacopts.parallel_workers = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("parallel workers for vacuum must be greater than or equal to zero"); + pg_log_error("parallel workers out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || vacopts.parallel_workers < 0) + { + pg_log_error("parallel workers for vacuum must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; @@ -220,18 +239,34 @@ main(int argc, char *argv[]) vacopts.skip_locked = true; break; case 6: - vacopts.min_xid_age = atoi(optarg); - if (vacopts.min_xid_age <= 0) + errno = 0; + vacopts.min_xid_age = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("minimum transaction ID age must be at least 1"); + pg_log_error("minimum transaction ID age out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || vacopts.min_xid_age <= 0) + { + pg_log_error("minimum transaction ID age must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; case 7: - vacopts.min_mxid_age = atoi(optarg); - if (vacopts.min_mxid_age <= 0) + errno = 0; + vacopts.min_mxid_age = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("minimum multixact ID age must be at least 1"); + pg_log_error("minimum multixact ID age out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || vacopts.min_mxid_age <= 0) + { + pg_log_error("minimum multixact ID age must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; -- 2.27.0 ----Next_Part(Wed_Jul_14_10_35_56_2021_265)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="v3-0002-Make-complain-for-invalid-numeirc-values-in-envir.patch" ^ permalink raw reply [nested|flat] 7+ messages in thread
* [PATCH v2 1/2] Be strict in numeric parameters on command line @ 2021-07-08 06:08 Kyotaro Horiguchi <horikyota.ntt@gmail.com> 0 siblings, 0 replies; 7+ messages in thread From: Kyotaro Horiguchi @ 2021-07-08 06:08 UTC (permalink / raw) Some numeric command line parameters are tolerant of valid values followed by garbage like "123xyz". Be strict to reject such invalid values. Do the same for psql meta command parameters. --- src/bin/pg_amcheck/pg_amcheck.c | 6 ++- src/bin/pg_basebackup/pg_basebackup.c | 13 +++-- src/bin/pg_basebackup/pg_receivewal.c | 18 +++++-- src/bin/pg_basebackup/pg_recvlogical.c | 17 +++++-- src/bin/pg_checksums/pg_checksums.c | 7 ++- src/bin/pg_ctl/pg_ctl.c | 18 ++++++- src/bin/pg_dump/pg_dump.c | 39 ++++++++------- src/bin/pg_dump/pg_restore.c | 17 ++++--- src/bin/pg_upgrade/option.c | 21 ++++++-- src/bin/pgbench/pgbench.c | 66 ++++++++++++++++---------- src/bin/psql/command.c | 52 ++++++++++++++++++-- src/bin/scripts/reindexdb.c | 10 ++-- src/bin/scripts/vacuumdb.c | 23 +++++---- 13 files changed, 219 insertions(+), 88 deletions(-) diff --git a/src/bin/pg_amcheck/pg_amcheck.c b/src/bin/pg_amcheck/pg_amcheck.c index 4bde16fb4b..71a82f9b75 100644 --- a/src/bin/pg_amcheck/pg_amcheck.c +++ b/src/bin/pg_amcheck/pg_amcheck.c @@ -17,6 +17,7 @@ #include "catalog/pg_am_d.h" #include "catalog/pg_namespace_d.h" #include "common/logging.h" +#include "common/string.h" #include "common/username.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" @@ -326,8 +327,9 @@ main(int argc, char *argv[]) append_btree_pattern(&opts.exclude, optarg, encoding); break; case 'j': - opts.jobs = atoi(optarg); - if (opts.jobs < 1) + errno = 0; + opts.jobs = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || opts.jobs < 1) { pg_log_error("number of parallel jobs must be at least 1"); exit(1); diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 8bb0acf498..29be95b96a 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -2287,6 +2287,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "CD:F:r:RS:T:X:l:nNzZ:d:c:h:p:U:s:wWkvP", long_options, &option_index)) != -1) { + char *endptr; + switch (c) { case 'C': @@ -2371,8 +2373,10 @@ main(int argc, char **argv) #endif break; case 'Z': - compresslevel = atoi(optarg); - if (compresslevel < 0 || compresslevel > 9) + errno = 0; + compresslevel = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + compresslevel < 0 || compresslevel > 9) { pg_log_error("invalid compression level \"%s\"", optarg); exit(1); @@ -2409,8 +2413,9 @@ main(int argc, char **argv) dbgetpassword = 1; break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr || errno == ERANGE || standby_message_timeout < 0) { pg_log_error("invalid status interval \"%s\"", optarg); exit(1); diff --git a/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c index c1334fad35..7fef925b99 100644 --- a/src/bin/pg_basebackup/pg_receivewal.c +++ b/src/bin/pg_basebackup/pg_receivewal.c @@ -22,6 +22,7 @@ #include "access/xlog_internal.h" #include "common/file_perm.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "libpq-fe.h" #include "receivelog.h" @@ -520,6 +521,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "D:d:E:h:p:U:s:S:nwWvZ:", long_options, &option_index)) != -1) { + char *endptr; + switch (c) { case 'D': @@ -532,7 +535,9 @@ main(int argc, char **argv) dbhost = pg_strdup(optarg); break; case 'p': - if (atoi(optarg) <= 0) + errno = 0; + if (strtoint(optarg, &endptr, 10) <= 0 || + *endptr || errno == ERANGE) { pg_log_error("invalid port number \"%s\"", optarg); exit(1); @@ -549,8 +554,9 @@ main(int argc, char **argv) dbgetpassword = 1; break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr || errno == ERANGE || standby_message_timeout < 0) { pg_log_error("invalid status interval \"%s\"", optarg); exit(1); @@ -574,8 +580,10 @@ main(int argc, char **argv) verbose++; break; case 'Z': - compresslevel = atoi(optarg); - if (compresslevel < 0 || compresslevel > 9) + errno = 0; + compresslevel = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + compresslevel < 0 || compresslevel > 9) { pg_log_error("invalid compression level \"%s\"", optarg); exit(1); diff --git a/src/bin/pg_basebackup/pg_recvlogical.c b/src/bin/pg_basebackup/pg_recvlogical.c index 76bd153fac..7be932d025 100644 --- a/src/bin/pg_basebackup/pg_recvlogical.c +++ b/src/bin/pg_basebackup/pg_recvlogical.c @@ -23,6 +23,7 @@ #include "common/fe_memutils.h" #include "common/file_perm.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "libpq-fe.h" #include "libpq/pqsignal.h" @@ -732,6 +733,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "E:f:F:nvtd:h:p:U:wWI:o:P:s:S:", long_options, &option_index)) != -1) { + char *endptr; + switch (c) { /* general options */ @@ -739,8 +742,9 @@ main(int argc, char **argv) outfile = pg_strdup(optarg); break; case 'F': - fsync_interval = atoi(optarg) * 1000; - if (fsync_interval < 0) + errno = 0; + fsync_interval = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr || errno == ERANGE || fsync_interval < 0) { pg_log_error("invalid fsync interval \"%s\"", optarg); exit(1); @@ -763,7 +767,9 @@ main(int argc, char **argv) dbhost = pg_strdup(optarg); break; case 'p': - if (atoi(optarg) <= 0) + errno = 0; + if (strtoint(optarg, &endptr, 10) <= 0 || + *endptr || errno == ERANGE) { pg_log_error("invalid port number \"%s\"", optarg); exit(1); @@ -820,8 +826,9 @@ main(int argc, char **argv) plugin = pg_strdup(optarg); break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr || errno == ERANGE || standby_message_timeout < 0) { pg_log_error("invalid status interval \"%s\"", optarg); exit(1); diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c index 3c326906e2..1c4e5b9d85 100644 --- a/src/bin/pg_checksums/pg_checksums.c +++ b/src/bin/pg_checksums/pg_checksums.c @@ -24,6 +24,7 @@ #include "common/file_perm.h" #include "common/file_utils.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "pg_getopt.h" #include "storage/bufpage.h" @@ -506,6 +507,8 @@ main(int argc, char *argv[]) while ((c = getopt_long(argc, argv, "cD:deNPf:v", long_options, &option_index)) != -1) { + char *endptr; + switch (c) { case 'c': @@ -518,7 +521,9 @@ main(int argc, char *argv[]) mode = PG_MODE_ENABLE; break; case 'f': - if (atoi(optarg) == 0) + errno = 0; + if (strtoint(optarg, &endptr, 10) == 0 + || *endptr || errno == ERANGE) { pg_log_error("invalid filenode specification, must be numeric: %s", optarg); exit(1); diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 7985da0a94..d6a39182cf 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -2331,6 +2331,8 @@ main(int argc, char **argv) /* process command-line options */ while (optind < argc) { + char *endptr; + while ((c = getopt_long(argc, argv, "cD:e:l:m:N:o:p:P:sS:t:U:wW", long_options, &option_index)) != -1) { @@ -2396,7 +2398,13 @@ main(int argc, char **argv) #endif break; case 't': - wait_seconds = atoi(optarg); + errno = 0; + wait_seconds = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || wait_seconds < 1) + { + pg_log_error("invalid timeout value \"%s\", use --no-wait to finish without waiting", optarg); + exit(1); + } wait_seconds_arg = true; break; case 'U': @@ -2459,7 +2467,13 @@ main(int argc, char **argv) } ctl_command = KILL_COMMAND; set_sig(argv[++optind]); - killproc = atol(argv[++optind]); + errno = 0; + killproc = strtol(argv[++optind], &endptr, 10); + if (*endptr || errno == ERANGE || killproc < 0) + { + pg_log_error("invalid process ID \"%s\"", argv[optind]); + exit(1); + } } #ifdef WIN32 else if (strcmp(argv[optind], "register") == 0) diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 321152151d..793f4b3509 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -54,6 +54,7 @@ #include "catalog/pg_trigger_d.h" #include "catalog/pg_type_d.h" #include "common/connect.h" +#include "common/string.h" #include "dumputils.h" #include "fe_utils/string_utils.h" #include "getopt_long.h" @@ -486,7 +487,19 @@ main(int argc, char **argv) break; case 'j': /* number of dump jobs */ - numWorkers = atoi(optarg); + errno = 0; + numWorkers = strtoint(optarg, &endptr, 10); + /* + * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS + * (= 64 usually) parallel jobs because that's the maximum + * limit for the WaitForMultipleObjects() call. + */ + if (*endptr || errno == ERANGE || numWorkers <= 0 +#ifdef WIN32 + || numWorkers > MAXIMUM_WAIT_OBJECTS +#endif + ) + fatal("invalid number of parallel jobs %s", optarg); break; case 'n': /* include schema(s) */ @@ -549,8 +562,10 @@ main(int argc, char **argv) break; case 'Z': /* Compression Level */ - compressLevel = atoi(optarg); - if (compressLevel < 0 || compressLevel > 9) + errno = 0; + compressLevel = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + compressLevel < 0 || compressLevel > 9) { pg_log_error("compression level must be in range 0..9"); exit_nicely(1); @@ -587,8 +602,10 @@ main(int argc, char **argv) case 8: have_extra_float_digits = true; - extra_float_digits = atoi(optarg); - if (extra_float_digits < -15 || extra_float_digits > 3) + errno = 0; + extra_float_digits = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + extra_float_digits < -15 || extra_float_digits > 3) { pg_log_error("extra_float_digits must be in range -15..3"); exit_nicely(1); @@ -719,18 +736,6 @@ main(int argc, char **argv) if (!plainText) dopt.outputCreateDB = 1; - /* - * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS (= 64 usually) - * parallel jobs because that's the maximum limit for the - * WaitForMultipleObjects() call. - */ - if (numWorkers <= 0 -#ifdef WIN32 - || numWorkers > MAXIMUM_WAIT_OBJECTS -#endif - ) - fatal("invalid number of parallel jobs"); - /* Parallel backup only in the directory archive format so far */ if (archiveFormat != archDirectory && numWorkers > 1) fatal("parallel backup only supported by the directory format"); diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 589b4aed53..285a09aaac 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -39,6 +39,7 @@ *------------------------------------------------------------------------- */ #include "postgres_fe.h" +#include "common/string.h" #include <ctype.h> #ifdef HAVE_TERMIOS_H @@ -151,6 +152,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "acCd:ef:F:h:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1", cmdopts, NULL)) != -1) { + char *endptr; + switch (c) { case 'a': /* Dump data only */ @@ -181,7 +184,13 @@ main(int argc, char **argv) break; case 'j': /* number of restore jobs */ - numWorkers = atoi(optarg); + errno = 0; + numWorkers = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || numWorkers <= 0) + { + pg_log_error("invalid number of parallel jobs"); + exit(1); + } break; case 'l': /* Dump the TOC summary */ @@ -344,12 +353,6 @@ main(int argc, char **argv) exit_nicely(1); } - if (numWorkers <= 0) - { - pg_log_error("invalid number of parallel jobs"); - exit(1); - } - /* See comments in pg_dump.c */ #ifdef WIN32 if (numWorkers > MAXIMUM_WAIT_OBJECTS) diff --git a/src/bin/pg_upgrade/option.c b/src/bin/pg_upgrade/option.c index 64bbda5650..f96f0d1e2a 100644 --- a/src/bin/pg_upgrade/option.c +++ b/src/bin/pg_upgrade/option.c @@ -104,6 +104,8 @@ parseCommandLine(int argc, char *argv[]) while ((option = getopt_long(argc, argv, "d:D:b:B:cj:ko:O:p:P:rs:U:v", long_options, &optindex)) != -1) { + char *endptr; + switch (option) { case 'b': @@ -127,7 +129,12 @@ parseCommandLine(int argc, char *argv[]) break; case 'j': - user_opts.jobs = atoi(optarg); + errno = 0; + user_opts.jobs = strtoint(optarg, &endptr, 10); + /**/ + if (*endptr || errno == ERANGE || user_opts.jobs < 1) + pg_fatal("invalid number of jobs %s\n", optarg); + break; case 'k': @@ -166,13 +173,17 @@ parseCommandLine(int argc, char *argv[]) * supported on all old/new versions (added in PG 9.2). */ case 'p': - if ((old_cluster.port = atoi(optarg)) <= 0) - pg_fatal("invalid old port number\n"); + errno = 0; + if ((old_cluster.port = strtoint(optarg, &endptr, 10)) <= 0 || + *endptr || errno == ERANGE) + pg_fatal("invalid old port number %s\n", optarg); break; case 'P': - if ((new_cluster.port = atoi(optarg)) <= 0) - pg_fatal("invalid new port number\n"); + errno = 0; + if ((new_cluster.port = strtoint(optarg, &endptr, 10)) <= 0 || + *endptr || errno == ERANGE) + pg_fatal("invalid new port number %s\n", optarg); break; case 'r': diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 4aeccd93af..4020347585 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -5838,6 +5838,7 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "iI:h:nvp:dqb:SNc:j:Crs:t:T:U:lf:D:F:M:P:R:L:", long_options, &optindex)) != -1) { char *script; + char *endptr; switch (c) { @@ -5869,8 +5870,9 @@ main(int argc, char **argv) break; case 'c': benchmarking_option_set = true; - nclients = atoi(optarg); - if (nclients <= 0) + errno = 0; + nclients = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || nclients <= 0) { pg_log_fatal("invalid number of clients: \"%s\"", optarg); exit(1); @@ -5896,8 +5898,9 @@ main(int argc, char **argv) break; case 'j': /* jobs */ benchmarking_option_set = true; - nthreads = atoi(optarg); - if (nthreads <= 0) + errno = 0; + nthreads = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || nthreads <= 0) { pg_log_fatal("invalid number of threads: \"%s\"", optarg); exit(1); @@ -5920,8 +5923,9 @@ main(int argc, char **argv) break; case 's': scale_given = true; - scale = atoi(optarg); - if (scale <= 0) + errno = 0; + scale = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || scale <= 0) { pg_log_fatal("invalid scaling factor: \"%s\"", optarg); exit(1); @@ -5929,8 +5933,9 @@ main(int argc, char **argv) break; case 't': benchmarking_option_set = true; - nxacts = atoi(optarg); - if (nxacts <= 0) + errno = 0; + nxacts = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || nxacts <= 0) { pg_log_fatal("invalid number of transactions: \"%s\"", optarg); exit(1); @@ -5938,8 +5943,9 @@ main(int argc, char **argv) break; case 'T': benchmarking_option_set = true; - duration = atoi(optarg); - if (duration <= 0) + errno = 0; + duration = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || duration <= 0) { pg_log_fatal("invalid duration: \"%s\"", optarg); exit(1); @@ -6001,8 +6007,10 @@ main(int argc, char **argv) break; case 'F': initialization_option_set = true; - fillfactor = atoi(optarg); - if (fillfactor < 10 || fillfactor > 100) + errno = 0; + fillfactor = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + fillfactor < 10 || fillfactor > 100) { pg_log_fatal("invalid fillfactor: \"%s\"", optarg); exit(1); @@ -6021,8 +6029,9 @@ main(int argc, char **argv) break; case 'P': benchmarking_option_set = true; - progress = atoi(optarg); - if (progress <= 0) + errno = 0; + progress = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || progress <= 0) { pg_log_fatal("invalid thread progress delay: \"%s\"", optarg); exit(1); @@ -6031,11 +6040,13 @@ main(int argc, char **argv) case 'R': { /* get a double from the beginning of option value */ - double throttle_value = atof(optarg); + double throttle_value; + errno = 0; + throttle_value = strtod(optarg, &endptr); benchmarking_option_set = true; - if (throttle_value <= 0.0) + if (*endptr || errno == ERANGE || throttle_value <= 0.0) { pg_log_fatal("invalid rate limit: \"%s\"", optarg); exit(1); @@ -6046,9 +6057,12 @@ main(int argc, char **argv) break; case 'L': { - double limit_ms = atof(optarg); + double limit_ms; - if (limit_ms <= 0.0) + errno = 0; + limit_ms = strtod(optarg, &endptr); + + if (*endptr || errno == ERANGE || limit_ms <= 0.0) { pg_log_fatal("invalid latency limit: \"%s\"", optarg); exit(1); @@ -6071,8 +6085,10 @@ main(int argc, char **argv) break; case 4: /* sampling-rate */ benchmarking_option_set = true; - sample_rate = atof(optarg); - if (sample_rate <= 0.0 || sample_rate > 1.0) + errno = 0; + sample_rate = strtod(optarg, &endptr); + if (*endptr || errno == ERANGE || + sample_rate <= 0.0 || sample_rate > 1.0) { pg_log_fatal("invalid sampling rate: \"%s\"", optarg); exit(1); @@ -6080,8 +6096,9 @@ main(int argc, char **argv) break; case 5: /* aggregate-interval */ benchmarking_option_set = true; - agg_interval = atoi(optarg); - if (agg_interval <= 0) + errno = 0; + agg_interval = strtod(optarg, &endptr); + if (*endptr || errno == ERANGE || agg_interval <= 0) { pg_log_fatal("invalid number of seconds for aggregation: \"%s\"", optarg); exit(1); @@ -6117,8 +6134,9 @@ main(int argc, char **argv) break; case 11: /* partitions */ initialization_option_set = true; - partitions = atoi(optarg); - if (partitions < 0) + errno = 0; + partitions = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || partitions < 0) { pg_log_fatal("invalid number of partitions: \"%s\"", optarg); exit(1); diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 543401c6d6..aaed986ae1 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -1039,8 +1039,11 @@ exec_command_edit(PsqlScanState scan_state, bool active_branch, } if (ln) { - lineno = atoi(ln); - if (lineno < 1) + char *endptr; + + errno = 0; + lineno = strtoint(ln, &endptr, 10); + if (*endptr || errno == ERANGE || lineno < 1) { pg_log_error("invalid line number: %s", ln); status = PSQL_CMD_ERROR; @@ -4283,7 +4286,21 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "border") == 0) { if (value) - popt->topt.border = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr || errno == ERANGE || + new_value < 0 || new_value > 65535) + { + pg_log_error("\\pset: border is invalid or out of range"); + return false; + } + + popt->topt.border = new_value; + } } /* set expanded/vertical mode */ @@ -4439,7 +4456,20 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "pager_min_lines") == 0) { if (value) - popt->topt.pager_min_lines = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr || errno == ERANGE || new_value < 0) + { + pg_log_error("\\pset: pager_min_lines is invalid or out of range"); + return false; + } + + popt->topt.pager_min_lines = new_value; + } } /* disable "(x rows)" footer */ @@ -4455,7 +4485,19 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "columns") == 0) { if (value) - popt->topt.columns = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr || errno == ERANGE || new_value < 0) + { + pg_log_error("\\pset: column is invalid or out of range"); + return false; + } + popt->topt.columns = new_value; + } } else { diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c index fc0681538a..baa68d58d8 100644 --- a/src/bin/scripts/reindexdb.c +++ b/src/bin/scripts/reindexdb.c @@ -15,6 +15,7 @@ #include "common.h" #include "common/connect.h" #include "common/logging.h" +#include "common/string.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" #include "fe_utils/parallel_slot.h" @@ -109,6 +110,8 @@ main(int argc, char *argv[]) /* process command-line options */ while ((c = getopt_long(argc, argv, "h:p:U:wWeqS:d:ast:i:j:v", long_options, &optindex)) != -1) { + char *endptr; + switch (c) { case 'h': @@ -151,10 +154,11 @@ main(int argc, char *argv[]) simple_string_list_append(&indexes, optarg); break; case 'j': - concurrentCons = atoi(optarg); - if (concurrentCons <= 0) + errno = 0; + concurrentCons = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || concurrentCons <= 0) { - pg_log_error("number of parallel jobs must be at least 1"); + pg_log_error("number of parallel jobs must be at least 1: %s", optarg); exit(1); } break; diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c index 61974baa78..93b563998a 100644 --- a/src/bin/scripts/vacuumdb.c +++ b/src/bin/scripts/vacuumdb.c @@ -17,6 +17,7 @@ #include "common.h" #include "common/connect.h" #include "common/logging.h" +#include "common/string.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" #include "fe_utils/parallel_slot.h" @@ -141,6 +142,8 @@ main(int argc, char *argv[]) while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:zZFat:fvj:P:", long_options, &optindex)) != -1) { + char *endptr; + switch (c) { case 'h': @@ -192,16 +195,18 @@ main(int argc, char *argv[]) vacopts.verbose = true; break; case 'j': - concurrentCons = atoi(optarg); - if (concurrentCons <= 0) + errno = 0; + concurrentCons = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || concurrentCons <= 0) { pg_log_error("number of parallel jobs must be at least 1"); exit(1); } break; case 'P': - vacopts.parallel_workers = atoi(optarg); - if (vacopts.parallel_workers < 0) + errno = 0; + vacopts.parallel_workers = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || vacopts.parallel_workers < 0) { pg_log_error("parallel workers for vacuum must be greater than or equal to zero"); exit(1); @@ -220,16 +225,18 @@ main(int argc, char *argv[]) vacopts.skip_locked = true; break; case 6: - vacopts.min_xid_age = atoi(optarg); - if (vacopts.min_xid_age <= 0) + errno = 0; + vacopts.min_xid_age = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || vacopts.min_xid_age <= 0) { pg_log_error("minimum transaction ID age must be at least 1"); exit(1); } break; case 7: - vacopts.min_mxid_age = atoi(optarg); - if (vacopts.min_mxid_age <= 0) + errno = 0; + vacopts.min_mxid_age = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || vacopts.min_mxid_age <= 0) { pg_log_error("minimum multixact ID age must be at least 1"); exit(1); -- 2.27.0 ----Next_Part(Fri_Jul__9_16_50_28_2021_228)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="v2-0002-Make-complain-for-invalid-numeirc-values-in-envir.patch" ^ permalink raw reply [nested|flat] 7+ messages in thread
* [PATCH v3 1/3] Be strict in numeric parameters on command line @ 2021-07-08 06:08 Kyotaro Horiguchi <horikyota.ntt@gmail.com> 0 siblings, 0 replies; 7+ messages in thread From: Kyotaro Horiguchi @ 2021-07-08 06:08 UTC (permalink / raw) Some numeric command line parameters are tolerant of valid values followed by garbage like "123xyz". Be strict to reject such invalid values. Do the same for psql meta command parameters. --- src/bin/pg_amcheck/pg_amcheck.c | 15 ++- src/bin/pg_basebackup/pg_basebackup.c | 24 +++- src/bin/pg_basebackup/pg_receivewal.c | 39 +++++-- src/bin/pg_basebackup/pg_recvlogical.c | 44 +++++-- src/bin/pg_checksums/pg_checksums.c | 17 ++- src/bin/pg_ctl/pg_ctl.c | 42 ++++++- src/bin/pg_dump/pg_dump.c | 57 ++++++--- src/bin/pg_dump/pg_restore.c | 42 ++++--- src/bin/pg_upgrade/option.c | 30 ++++- src/bin/pgbench/pgbench.c | 154 +++++++++++++++++++------ src/bin/psql/command.c | 73 +++++++++++- src/bin/scripts/reindexdb.c | 15 ++- src/bin/scripts/vacuumdb.c | 59 ++++++++-- 13 files changed, 484 insertions(+), 127 deletions(-) diff --git a/src/bin/pg_amcheck/pg_amcheck.c b/src/bin/pg_amcheck/pg_amcheck.c index 4bde16fb4b..f40d58ac96 100644 --- a/src/bin/pg_amcheck/pg_amcheck.c +++ b/src/bin/pg_amcheck/pg_amcheck.c @@ -17,6 +17,7 @@ #include "catalog/pg_am_d.h" #include "catalog/pg_namespace_d.h" #include "common/logging.h" +#include "common/string.h" #include "common/username.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" @@ -326,10 +327,18 @@ main(int argc, char *argv[]) append_btree_pattern(&opts.exclude, optarg, encoding); break; case 'j': - opts.jobs = atoi(optarg); - if (opts.jobs < 1) + errno = 0; + opts.jobs = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("number of parallel jobs must be at least 1"); + pg_log_error("number of parallel jobs out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || opts.jobs < 1) + { + pg_log_error("number of parallel jobs must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 8bb0acf498..c30005f569 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -2287,6 +2287,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "CD:F:r:RS:T:X:l:nNzZ:d:c:h:p:U:s:wWkvP", long_options, &option_index)) != -1) { + char *endptr; + switch (c) { case 'C': @@ -2371,10 +2373,12 @@ main(int argc, char **argv) #endif break; case 'Z': - compresslevel = atoi(optarg); - if (compresslevel < 0 || compresslevel > 9) + errno = 0; + compresslevel = strtoint(optarg, &endptr, 10); + if (*endptr || + errno == ERANGE || compresslevel < 0 || compresslevel > 9) { - pg_log_error("invalid compression level \"%s\"", optarg); + pg_log_error("compression level must be a digit in range 0..9: \"%s\"", optarg); exit(1); } break; @@ -2409,10 +2413,18 @@ main(int argc, char **argv) dbgetpassword = 1; break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid status interval \"%s\"", optarg); + pg_log_error("status interval out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || standby_message_timeout < 0) + { + pg_log_error("status interval must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; diff --git a/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c index c1334fad35..fb03147fe7 100644 --- a/src/bin/pg_basebackup/pg_receivewal.c +++ b/src/bin/pg_basebackup/pg_receivewal.c @@ -22,6 +22,7 @@ #include "access/xlog_internal.h" #include "common/file_perm.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "libpq-fe.h" #include "receivelog.h" @@ -520,6 +521,9 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "D:d:E:h:p:U:s:S:nwWvZ:", long_options, &option_index)) != -1) { + char *endptr; + int v; + switch (c) { case 'D': @@ -532,9 +536,17 @@ main(int argc, char **argv) dbhost = pg_strdup(optarg); break; case 'p': - if (atoi(optarg) <= 0) + errno = 0; + v = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid port number \"%s\"", optarg); + pg_log_error("port number out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || v < 1) + { + pg_log_error("port number must be an integer greater than zero: \"%s\"", + optarg); exit(1); } dbport = pg_strdup(optarg); @@ -549,10 +561,18 @@ main(int argc, char **argv) dbgetpassword = 1; break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid status interval \"%s\"", optarg); + pg_log_error("status interval out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || standby_message_timeout < 0) + { + pg_log_error("status interval must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; @@ -574,10 +594,13 @@ main(int argc, char **argv) verbose++; break; case 'Z': - compresslevel = atoi(optarg); - if (compresslevel < 0 || compresslevel > 9) + errno = 0; + compresslevel = strtoint(optarg, &endptr, 10); + if (*endptr || + errno == ERANGE || compresslevel < 0 || compresslevel > 9) { - pg_log_error("invalid compression level \"%s\"", optarg); + pg_log_error("compression level must be a digit in range 0..9: \"%s\"", + optarg); exit(1); } break; diff --git a/src/bin/pg_basebackup/pg_recvlogical.c b/src/bin/pg_basebackup/pg_recvlogical.c index 76bd153fac..9bc4902033 100644 --- a/src/bin/pg_basebackup/pg_recvlogical.c +++ b/src/bin/pg_basebackup/pg_recvlogical.c @@ -23,6 +23,7 @@ #include "common/fe_memutils.h" #include "common/file_perm.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "libpq-fe.h" #include "libpq/pqsignal.h" @@ -732,6 +733,9 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "E:f:F:nvtd:h:p:U:wWI:o:P:s:S:", long_options, &option_index)) != -1) { + char *endptr; + int v; + switch (c) { /* general options */ @@ -739,10 +743,18 @@ main(int argc, char **argv) outfile = pg_strdup(optarg); break; case 'F': - fsync_interval = atoi(optarg) * 1000; - if (fsync_interval < 0) + errno = 0; + fsync_interval = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid fsync interval \"%s\"", optarg); + pg_log_error("fsync interval out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || fsync_interval < 0) + { + pg_log_error("fsync interval must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; @@ -763,9 +775,17 @@ main(int argc, char **argv) dbhost = pg_strdup(optarg); break; case 'p': - if (atoi(optarg) <= 0) + errno = 0; + v = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid port number \"%s\"", optarg); + pg_log_error("port number out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || v < 1) + { + pg_log_error("port number must be an integer greater than zero: \"%s\"", + optarg); exit(1); } dbport = pg_strdup(optarg); @@ -820,10 +840,18 @@ main(int argc, char **argv) plugin = pg_strdup(optarg); break; case 's': - standby_message_timeout = atoi(optarg) * 1000; - if (standby_message_timeout < 0) + errno = 0; + standby_message_timeout = strtoint(optarg, &endptr, 10) * 1000; + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid status interval \"%s\"", optarg); + pg_log_error("status interval out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || standby_message_timeout < 0) + { + pg_log_error("status interval must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c index 3c326906e2..78a1d4ef38 100644 --- a/src/bin/pg_checksums/pg_checksums.c +++ b/src/bin/pg_checksums/pg_checksums.c @@ -24,6 +24,7 @@ #include "common/file_perm.h" #include "common/file_utils.h" #include "common/logging.h" +#include "common/string.h" #include "getopt_long.h" #include "pg_getopt.h" #include "storage/bufpage.h" @@ -506,6 +507,9 @@ main(int argc, char *argv[]) while ((c = getopt_long(argc, argv, "cD:deNPf:v", long_options, &option_index)) != -1) { + char *endptr; + int v; + switch (c) { case 'c': @@ -518,9 +522,18 @@ main(int argc, char *argv[]) mode = PG_MODE_ENABLE; break; case 'f': - if (atoi(optarg) == 0) + errno = 0; + v = strtoint(optarg, &endptr, 10); + if(*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid filenode specification, must be numeric: %s", optarg); + pg_log_error("filenode specification out of range: %s", + optarg); + exit(1); + } + if(*endptr || v < 1) + { + pg_log_error("filenode specification must be an integer greater than zero: %s", + optarg); exit(1); } only_filenode = pstrdup(optarg); diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 7985da0a94..0f72ef016b 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -76,6 +76,7 @@ typedef enum #define WAITS_PER_SEC 10 /* should divide USEC_PER_SEC evenly */ +static bool do_wait_arg = false; static bool do_wait = true; static int wait_seconds = DEFAULT_WAIT; static bool wait_seconds_arg = false; @@ -2331,6 +2332,8 @@ main(int argc, char **argv) /* process command-line options */ while (optind < argc) { + char *endptr; + while ((c = getopt_long(argc, argv, "cD:e:l:m:N:o:p:P:sS:t:U:wW", long_options, &option_index)) != -1) { @@ -2396,7 +2399,20 @@ main(int argc, char **argv) #endif break; case 't': - wait_seconds = atoi(optarg); + errno = 0; + wait_seconds = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + { + pg_log_error("timeout value out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || wait_seconds < 0) + { + pg_log_error("timeout value must be a non-negative integer: \"%s\"", + optarg); + exit(1); + } wait_seconds_arg = true; break; case 'U': @@ -2408,6 +2424,7 @@ main(int argc, char **argv) break; case 'w': do_wait = true; + do_wait_arg = true; break; case 'W': do_wait = false; @@ -2459,7 +2476,20 @@ main(int argc, char **argv) } ctl_command = KILL_COMMAND; set_sig(argv[++optind]); - killproc = atol(argv[++optind]); + errno = 0; + killproc = strtol(argv[++optind], &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + { + pg_log_error("process ID out of range: \"%s\"", + argv[optind]); + exit(1); + } + if (*endptr || killproc < 0) + { + pg_log_error("process ID must be a non-negative integer: \"%s\"", + argv[optind]); + exit(1); + } } #ifdef WIN32 else if (strcmp(argv[optind], "register") == 0) @@ -2514,6 +2544,14 @@ main(int argc, char **argv) do_wait = false; } + if (wait_seconds == 0 && do_wait) + { + /* Warn if user instructed to wait but we actually don't */ + if (!silent_mode && do_wait_arg) + write_stderr(_("%s: WARNING: -w is ignored because timeout is set to 0\n"), progname); + do_wait = false; + } + if (pg_data) { snprintf(postopts_file, MAXPGPATH, "%s/postmaster.opts", pg_data); diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 321152151d..8ef29b37f6 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -54,6 +54,7 @@ #include "catalog/pg_trigger_d.h" #include "catalog/pg_type_d.h" #include "common/connect.h" +#include "common/string.h" #include "dumputils.h" #include "fe_utils/string_utils.h" #include "getopt_long.h" @@ -103,6 +104,17 @@ static Oid g_last_builtin_oid; /* value of the last builtin oid */ /* The specified names/patterns should to match at least one entity */ static int strict_names = 0; +/* + * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS (= 64 usually) + * parallel jobs because that's the maximum limit for the + * WaitForMultipleObjects() call. + */ +#ifndef WIN32 +#define MAX_NUM_WORKERS INT_MAX +#else +#define MAX_NUM_WORKERS MAXIMUM_WAIT_OBJECTS +#endif + /* * Object inclusion/exclusion lists * @@ -486,7 +498,21 @@ main(int argc, char **argv) break; case 'j': /* number of dump jobs */ - numWorkers = atoi(optarg); + errno = 0; + numWorkers = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && + (errno == ERANGE || numWorkers > MAX_NUM_WORKERS)) + { + pg_log_error("number of parallel jobs out of range: \"%s\"", + optarg); + exit_nicely(1); + } + if (*endptr || numWorkers <= 0) + { + pg_log_error("number of parallel jobs must be an integer greater than zero: \"%s\"", + optarg); + exit_nicely(1); + } break; case 'n': /* include schema(s) */ @@ -549,10 +575,12 @@ main(int argc, char **argv) break; case 'Z': /* Compression Level */ - compressLevel = atoi(optarg); - if (compressLevel < 0 || compressLevel > 9) + errno = 0; + compressLevel = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + compressLevel < 0 || compressLevel > 9) { - pg_log_error("compression level must be in range 0..9"); + pg_log_error("compression level must be a digit in range 0..9: \"%s\"", optarg); exit_nicely(1); } break; @@ -587,10 +615,13 @@ main(int argc, char **argv) case 8: have_extra_float_digits = true; - extra_float_digits = atoi(optarg); - if (extra_float_digits < -15 || extra_float_digits > 3) + errno = 0; + extra_float_digits = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + extra_float_digits < -15 || extra_float_digits > 3) { - pg_log_error("extra_float_digits must be in range -15..3"); + pg_log_error("extra_float_digits must be an integer in range -15..3: \"%s\"", + optarg); exit_nicely(1); } break; @@ -719,18 +750,6 @@ main(int argc, char **argv) if (!plainText) dopt.outputCreateDB = 1; - /* - * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS (= 64 usually) - * parallel jobs because that's the maximum limit for the - * WaitForMultipleObjects() call. - */ - if (numWorkers <= 0 -#ifdef WIN32 - || numWorkers > MAXIMUM_WAIT_OBJECTS -#endif - ) - fatal("invalid number of parallel jobs"); - /* Parallel backup only in the directory archive format so far */ if (archiveFormat != archDirectory && numWorkers > 1) fatal("parallel backup only supported by the directory format"); diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 589b4aed53..3bb5a48c55 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -39,6 +39,7 @@ *------------------------------------------------------------------------- */ #include "postgres_fe.h" +#include "common/string.h" #include <ctype.h> #ifdef HAVE_TERMIOS_H @@ -52,6 +53,13 @@ static void usage(const char *progname); +/* See comments in pg_dump.c */ +#ifndef WIN32 +#define MAX_NUM_WORKERS INT_MAX +#else +#define MAX_NUM_WORKERS MAXIMUM_WAIT_OBJECTS +#endif + int main(int argc, char **argv) { @@ -151,6 +159,8 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "acCd:ef:F:h:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1", cmdopts, NULL)) != -1) { + char *endptr; + switch (c) { case 'a': /* Dump data only */ @@ -181,7 +191,21 @@ main(int argc, char **argv) break; case 'j': /* number of restore jobs */ - numWorkers = atoi(optarg); + errno = 0; + numWorkers = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && + (errno == ERANGE || numWorkers > MAX_NUM_WORKERS)) + { + pg_log_error("number of parallel jobs out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || numWorkers <= 0) + { + pg_log_error("number of parallel jobs must be an integer greater than zero: \"%s\"", + optarg); + exit(1); + } break; case 'l': /* Dump the TOC summary */ @@ -344,22 +368,6 @@ main(int argc, char **argv) exit_nicely(1); } - if (numWorkers <= 0) - { - pg_log_error("invalid number of parallel jobs"); - exit(1); - } - - /* See comments in pg_dump.c */ -#ifdef WIN32 - if (numWorkers > MAXIMUM_WAIT_OBJECTS) - { - pg_log_error("maximum number of parallel jobs is %d", - MAXIMUM_WAIT_OBJECTS); - exit(1); - } -#endif - /* Can't do single-txn mode with multiple connections */ if (opts->single_txn && numWorkers > 1) { diff --git a/src/bin/pg_upgrade/option.c b/src/bin/pg_upgrade/option.c index 64bbda5650..c014bbca0d 100644 --- a/src/bin/pg_upgrade/option.c +++ b/src/bin/pg_upgrade/option.c @@ -104,6 +104,8 @@ parseCommandLine(int argc, char *argv[]) while ((option = getopt_long(argc, argv, "d:D:b:B:cj:ko:O:p:P:rs:U:v", long_options, &optindex)) != -1) { + char *endptr; + switch (option) { case 'b': @@ -127,7 +129,15 @@ parseCommandLine(int argc, char *argv[]) break; case 'j': - user_opts.jobs = atoi(optarg); + errno = 0; + user_opts.jobs = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + pg_fatal("number of parallel jobs out of range: \"%s\"\n", + optarg); + if (*endptr || user_opts.jobs < 1) + pg_fatal("number of parallel jobs must be an integer greater than zero: \"%s\"\n", + optarg); + break; case 'k': @@ -166,13 +176,23 @@ parseCommandLine(int argc, char *argv[]) * supported on all old/new versions (added in PG 9.2). */ case 'p': - if ((old_cluster.port = atoi(optarg)) <= 0) - pg_fatal("invalid old port number\n"); + errno = 0; + old_cluster.port = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + pg_fatal("old port number out of range: \"%s\"\n", optarg); + if (*endptr || old_cluster.port <= 0) + pg_fatal("old port number must be an integer greater than zero: \"%s\"\n", + optarg); break; case 'P': - if ((new_cluster.port = atoi(optarg)) <= 0) - pg_fatal("invalid new port number\n"); + errno = 0; + new_cluster.port = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + pg_fatal("new port number out of range: \"%s\"\n", optarg); + if (*endptr || new_cluster.port <= 0) + pg_fatal("new port number must be an integer greater than zero: \"%s\"\n", + optarg); break; case 'r': diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 364b5a2e47..1c3b5836c1 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -5856,6 +5856,7 @@ main(int argc, char **argv) while ((c = getopt_long(argc, argv, "iI:h:nvp:dqb:SNc:j:Crs:t:T:U:lf:D:F:M:P:R:L:", long_options, &optindex)) != -1) { char *script; + char *endptr; switch (c) { @@ -5887,10 +5888,18 @@ main(int argc, char **argv) break; case 'c': benchmarking_option_set = true; - nclients = atoi(optarg); - if (nclients <= 0) + errno = 0; + nclients = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid number of clients: \"%s\"", optarg); + pg_log_fatal("number of clients out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || nclients <= 0) + { + pg_log_fatal("number of clients must be an integer greater than zero: \"%s\"", + optarg); exit(1); } #ifdef HAVE_GETRLIMIT @@ -5914,10 +5923,18 @@ main(int argc, char **argv) break; case 'j': /* jobs */ benchmarking_option_set = true; - nthreads = atoi(optarg); - if (nthreads <= 0) + errno = 0; + nthreads = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid number of threads: \"%s\"", optarg); + pg_log_fatal("number of threads out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || nthreads <= 0) + { + pg_log_fatal("number of threads must be an integer greater than zero: \"%s\"", + optarg); exit(1); } #ifndef ENABLE_THREAD_SAFETY @@ -5938,28 +5955,50 @@ main(int argc, char **argv) break; case 's': scale_given = true; - scale = atoi(optarg); - if (scale <= 0) + errno = 0; + scale = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid scaling factor: \"%s\"", optarg); + pg_log_fatal("scaling factor out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || scale <= 0) + { + pg_log_fatal("scaling factor must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; case 't': benchmarking_option_set = true; - nxacts = atoi(optarg); - if (nxacts <= 0) + errno = 0; + nxacts = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid number of transactions: \"%s\"", optarg); + pg_log_fatal("number of transactions out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || nxacts <= 0) + { + pg_log_fatal("number of transactions must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; case 'T': benchmarking_option_set = true; - duration = atoi(optarg); - if (duration <= 0) + errno = 0; + duration = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid duration: \"%s\"", optarg); + pg_log_fatal("duration out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || duration <= 0) + { + pg_log_fatal("duration must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; @@ -6019,10 +6058,13 @@ main(int argc, char **argv) break; case 'F': initialization_option_set = true; - fillfactor = atoi(optarg); - if (fillfactor < 10 || fillfactor > 100) + errno = 0; + fillfactor = strtoint(optarg, &endptr, 10); + if (*endptr || errno == ERANGE || + fillfactor < 10 || fillfactor > 100) { - pg_log_fatal("invalid fillfactor: \"%s\"", optarg); + pg_log_fatal("fillfactor must be an ineger between 10 and 100: \"%s\"", + optarg); exit(1); } break; @@ -6039,23 +6081,38 @@ main(int argc, char **argv) break; case 'P': benchmarking_option_set = true; - progress = atoi(optarg); - if (progress <= 0) + errno = 0; + progress = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid thread progress delay: \"%s\"", optarg); + pg_log_fatal("thread progress delay out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || progress <= 0) + { + pg_log_fatal("thread progress delay must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; case 'R': { /* get a double from the beginning of option value */ - double throttle_value = atof(optarg); + double throttle_value; + errno = 0; + throttle_value = strtod(optarg, &endptr); benchmarking_option_set = true; - if (throttle_value <= 0.0) + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid rate limit: \"%s\"", optarg); + pg_log_fatal("rate limit out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || throttle_value <= 0.0) + { + pg_log_fatal("rate limit must be a real number greater than zero: \"%s\"", optarg); exit(1); } /* Invert rate limit into per-transaction delay in usec */ @@ -6064,11 +6121,20 @@ main(int argc, char **argv) break; case 'L': { - double limit_ms = atof(optarg); + double limit_ms; - if (limit_ms <= 0.0) + errno = 0; + limit_ms = strtod(optarg, &endptr); + + if (*endptr == 0 && errno == ERANGE) + { + pg_log_fatal("latency limit out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || limit_ms <= 0.0) { - pg_log_fatal("invalid latency limit: \"%s\"", optarg); + pg_log_fatal("latency limit must be a real number greater than zero: \"%s\"", optarg); exit(1); } benchmarking_option_set = true; @@ -6089,19 +6155,27 @@ main(int argc, char **argv) break; case 4: /* sampling-rate */ benchmarking_option_set = true; - sample_rate = atof(optarg); - if (sample_rate <= 0.0 || sample_rate > 1.0) + errno = 0; + sample_rate = strtod(optarg, &endptr); + if (*endptr || errno == ERANGE || + sample_rate <= 0.0 || sample_rate > 1.0) { - pg_log_fatal("invalid sampling rate: \"%s\"", optarg); + pg_log_fatal("sampling rate must be an real number between 0.0 and 1.0: \"%s\"", optarg); exit(1); } break; case 5: /* aggregate-interval */ benchmarking_option_set = true; - agg_interval = atoi(optarg); - if (agg_interval <= 0) + errno = 0; + agg_interval = strtod(optarg, &endptr); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid number of seconds for aggregation: \"%s\"", optarg); + pg_log_fatal("aggregate interval out of range: \"%s\"", optarg); + exit(1); + } + if (*endptr || agg_interval <= 0) + { + pg_log_fatal("aggregate interval must be a real number greater than zero: \"%s\"", optarg); exit(1); } break; @@ -6135,10 +6209,18 @@ main(int argc, char **argv) break; case 11: /* partitions */ initialization_option_set = true; - partitions = atoi(optarg); - if (partitions < 0) + errno = 0; + partitions = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_fatal("invalid number of partitions: \"%s\"", optarg); + pg_log_fatal("number of partitions out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || partitions < 0) + { + pg_log_fatal("number of partitions must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index d704c4220c..13074051b3 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -1040,10 +1040,18 @@ exec_command_edit(PsqlScanState scan_state, bool active_branch, } if (ln) { - lineno = atoi(ln); - if (lineno < 1) + char *endptr; + + errno = 0; + lineno = strtoint(ln, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("invalid line number: %s", ln); + pg_log_error("line number out of range: %s", ln); + status = PSQL_CMD_ERROR; + } + if (*endptr || lineno < 1) + { + pg_log_error("line number must be an integer greater than zero: %s", ln); status = PSQL_CMD_ERROR; } } @@ -4284,7 +4292,25 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "border") == 0) { if (value) - popt->topt.border = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr == 0 && (errno == ERANGE || new_value > 65535)) + { + pg_log_error("\\pset: border out of range"); + return false; + } + if (*endptr || new_value < 0) + { + pg_log_error("\\pset: border must be an integer greater than zero"); + return false; + } + + popt->topt.border = new_value; + } } /* set expanded/vertical mode */ @@ -4440,7 +4466,25 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "pager_min_lines") == 0) { if (value) - popt->topt.pager_min_lines = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + { + pg_log_error("\\pset: pager_min_lines out of range"); + return false; + } + if (*endptr || new_value < 0) + { + pg_log_error("\\pset: pager_min_lines must be a non-negative integer"); + return false; + } + + popt->topt.pager_min_lines = new_value; + } } /* disable "(x rows)" footer */ @@ -4456,7 +4500,24 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) else if (strcmp(param, "columns") == 0) { if (value) - popt->topt.columns = atoi(value); + { + char *endptr; + int new_value; + + errno = 0; + new_value = strtoint(value, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) + { + pg_log_error("\\pset: column out of range"); + return false; + } + if (*endptr || new_value < 0) + { + pg_log_error("\\pset: column must be a non-negative integer"); + return false; + } + popt->topt.columns = new_value; + } } else { diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c index fc0681538a..42d4d20768 100644 --- a/src/bin/scripts/reindexdb.c +++ b/src/bin/scripts/reindexdb.c @@ -15,6 +15,7 @@ #include "common.h" #include "common/connect.h" #include "common/logging.h" +#include "common/string.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" #include "fe_utils/parallel_slot.h" @@ -109,6 +110,8 @@ main(int argc, char *argv[]) /* process command-line options */ while ((c = getopt_long(argc, argv, "h:p:U:wWeqS:d:ast:i:j:v", long_options, &optindex)) != -1) { + char *endptr; + switch (c) { case 'h': @@ -151,10 +154,16 @@ main(int argc, char *argv[]) simple_string_list_append(&indexes, optarg); break; case 'j': - concurrentCons = atoi(optarg); - if (concurrentCons <= 0) + errno = 0; + concurrentCons = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("number of parallel jobs must be at least 1"); + pg_log_error("number of parallel jobs out of range: %s", optarg); + exit(1); + } + if (*endptr || concurrentCons <= 0) + { + pg_log_error("number of parallel jobs must be an integer greater than zero: %s", optarg); exit(1); } break; diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c index 61974baa78..6b2a34edd0 100644 --- a/src/bin/scripts/vacuumdb.c +++ b/src/bin/scripts/vacuumdb.c @@ -17,6 +17,7 @@ #include "common.h" #include "common/connect.h" #include "common/logging.h" +#include "common/string.h" #include "fe_utils/cancel.h" #include "fe_utils/option_utils.h" #include "fe_utils/parallel_slot.h" @@ -141,6 +142,8 @@ main(int argc, char *argv[]) while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:zZFat:fvj:P:", long_options, &optindex)) != -1) { + char *endptr; + switch (c) { case 'h': @@ -192,18 +195,34 @@ main(int argc, char *argv[]) vacopts.verbose = true; break; case 'j': - concurrentCons = atoi(optarg); - if (concurrentCons <= 0) + errno = 0; + concurrentCons = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("number of parallel jobs must be at least 1"); + pg_log_error("number of parallel jobs out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || concurrentCons <= 0) + { + pg_log_error("number of parallel jobs must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; case 'P': - vacopts.parallel_workers = atoi(optarg); - if (vacopts.parallel_workers < 0) + errno = 0; + vacopts.parallel_workers = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("parallel workers for vacuum must be greater than or equal to zero"); + pg_log_error("parallel workers out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || vacopts.parallel_workers < 0) + { + pg_log_error("parallel workers for vacuum must be a non-negative integer: \"%s\"", + optarg); exit(1); } break; @@ -220,18 +239,34 @@ main(int argc, char *argv[]) vacopts.skip_locked = true; break; case 6: - vacopts.min_xid_age = atoi(optarg); - if (vacopts.min_xid_age <= 0) + errno = 0; + vacopts.min_xid_age = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("minimum transaction ID age must be at least 1"); + pg_log_error("minimum transaction ID age out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || vacopts.min_xid_age <= 0) + { + pg_log_error("minimum transaction ID age must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; case 7: - vacopts.min_mxid_age = atoi(optarg); - if (vacopts.min_mxid_age <= 0) + errno = 0; + vacopts.min_mxid_age = strtoint(optarg, &endptr, 10); + if (*endptr == 0 && errno == ERANGE) { - pg_log_error("minimum multixact ID age must be at least 1"); + pg_log_error("minimum multixact ID age out of range: \"%s\"", + optarg); + exit(1); + } + if (*endptr || vacopts.min_mxid_age <= 0) + { + pg_log_error("minimum multixact ID age must be an integer greater than zero: \"%s\"", + optarg); exit(1); } break; -- 2.27.0 ----Next_Part(Wed_Jul_14_10_35_56_2021_265)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="v3-0002-Make-complain-for-invalid-numeirc-values-in-envir.patch" ^ permalink raw reply [nested|flat] 7+ messages in thread
* [PATCH v18 2/8] Row pattern recognition patch (parse/analysis). @ 2024-05-11 07:11 Tatsuo Ishii <ishii@postgresql.org> 0 siblings, 0 replies; 7+ messages in thread From: Tatsuo Ishii @ 2024-05-11 07:11 UTC (permalink / raw) --- src/backend/parser/parse_agg.c | 7 + src/backend/parser/parse_clause.c | 296 +++++++++++++++++++++++++++++- src/backend/parser/parse_expr.c | 4 + src/backend/parser/parse_func.c | 3 + 4 files changed, 309 insertions(+), 1 deletion(-) diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c index bee7d8346a..9bc22a836a 100644 --- a/src/backend/parser/parse_agg.c +++ b/src/backend/parser/parse_agg.c @@ -577,6 +577,10 @@ check_agglevels_and_constraints(ParseState *pstate, Node *expr) errkind = true; break; + case EXPR_KIND_RPR_DEFINE: + errkind = true; + break; + /* * There is intentionally no default: case here, so that the * compiler will warn if we add a new ParseExprKind without @@ -967,6 +971,9 @@ transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, case EXPR_KIND_CYCLE_MARK: errkind = true; break; + case EXPR_KIND_RPR_DEFINE: + errkind = true; + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c index 8118036495..9762dce81f 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -98,7 +98,14 @@ static WindowClause *findWindowClause(List *wclist, const char *name); static Node *transformFrameOffset(ParseState *pstate, int frameOptions, Oid rangeopfamily, Oid rangeopcintype, Oid *inRangeFunc, Node *clause); - +static void transformRPR(ParseState *pstate, WindowClause *wc, WindowDef *windef, + List **targetlist); +static List *transformDefineClause(ParseState *pstate, WindowClause *wc, WindowDef *windef, + List **targetlist); +static void transformPatternClause(ParseState *pstate, WindowClause *wc, + WindowDef *windef); +static List *transformMeasureClause(ParseState *pstate, WindowClause *wc, + WindowDef *windef); /* * transformFromClause - @@ -2956,6 +2963,10 @@ transformWindowDefinitions(ParseState *pstate, rangeopfamily, rangeopcintype, &wc->endInRangeFunc, windef->endOffset); + + /* Process Row Pattern Recognition related clauses */ + transformRPR(pstate, wc, windef, targetlist); + wc->winref = winref; result = lappend(result, wc); @@ -3820,3 +3831,286 @@ transformFrameOffset(ParseState *pstate, int frameOptions, return node; } + +/* + * transformRPR + * Process Row Pattern Recognition related clauses + */ +static void +transformRPR(ParseState *pstate, WindowClause *wc, WindowDef *windef, + List **targetlist) +{ + /* + * Window definition exists? + */ + if (windef == NULL) + return; + + /* + * Row Pattern Common Syntax clause exists? + */ + if (windef->rpCommonSyntax == NULL) + return; + + /* Check Frame option. Frame must start at current row */ + if ((wc->frameOptions & FRAMEOPTION_START_CURRENT_ROW) == 0) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("FRAME must start at current row when row patttern recognition is used"))); + + /* Transform AFTER MACH SKIP TO clause */ + wc->rpSkipTo = windef->rpCommonSyntax->rpSkipTo; + + /* Transform AFTER MACH SKIP TO variable */ + wc->rpSkipVariable = windef->rpCommonSyntax->rpSkipVariable; + + /* Transform SEEK or INITIAL clause */ + wc->initial = windef->rpCommonSyntax->initial; + + /* Transform DEFINE clause into list of TargetEntry's */ + wc->defineClause = transformDefineClause(pstate, wc, windef, targetlist); + + /* Check PATTERN clause and copy to patternClause */ + transformPatternClause(pstate, wc, windef); + + /* Transform MEASURE clause */ + transformMeasureClause(pstate, wc, windef); +} + +/* + * transformDefineClause Process DEFINE clause and transform ResTarget into + * list of TargetEntry. + * + * XXX we only support column reference in row pattern definition search + * condition, e.g. "price". <row pattern definition variable name>.<column + * reference> is not supported, e.g. "A.price". + */ +static List * +transformDefineClause(ParseState *pstate, WindowClause *wc, WindowDef *windef, + List **targetlist) +{ + /* DEFINE variable name initials */ + static char *defineVariableInitials = "abcdefghijklmnopqrstuvwxyz"; + + ListCell *lc, + *l; + ResTarget *restarget, + *r; + List *restargets; + List *defineClause; + char *name; + int initialLen; + int i; + + /* + * If Row Definition Common Syntax exists, DEFINE clause must exist. (the + * raw parser should have already checked it.) + */ + Assert(windef->rpCommonSyntax->rpDefs != NULL); + + /* + * Check and add "A AS A IS TRUE" if pattern variable is missing in DEFINE + * per the SQL standard. + */ + restargets = NIL; + foreach(lc, windef->rpCommonSyntax->rpPatterns) + { + A_Expr *a; + bool found = false; + + if (!IsA(lfirst(lc), A_Expr)) + ereport(ERROR, + errmsg("node type is not A_Expr")); + + a = (A_Expr *) lfirst(lc); + name = strVal(a->lexpr); + + foreach(l, windef->rpCommonSyntax->rpDefs) + { + restarget = (ResTarget *) lfirst(l); + + if (!strcmp(restarget->name, name)) + { + found = true; + break; + } + } + + if (!found) + { + /* + * "name" is missing. So create "name AS name IS TRUE" ResTarget + * node and add it to the temporary list. + */ + A_Const *n; + + restarget = makeNode(ResTarget); + n = makeNode(A_Const); + n->val.boolval.type = T_Boolean; + n->val.boolval.boolval = true; + n->location = -1; + restarget->name = pstrdup(name); + restarget->indirection = NIL; + restarget->val = (Node *) n; + restarget->location = -1; + restargets = lappend((List *) restargets, restarget); + } + } + + if (list_length(restargets) >= 1) + { + /* add missing DEFINEs */ + windef->rpCommonSyntax->rpDefs = + list_concat(windef->rpCommonSyntax->rpDefs, restargets); + list_free(restargets); + } + + /* + * Check for duplicate row pattern definition variables. The standard + * requires that no two row pattern definition variable names shall be + * equivalent. + */ + restargets = NIL; + foreach(lc, windef->rpCommonSyntax->rpDefs) + { + restarget = (ResTarget *) lfirst(lc); + name = restarget->name; + + /* + * Add DEFINE expression (Restarget->val) to the targetlist as a + * TargetEntry if it does not exist yet. Planner will add the column + * ref var node to the outer plan's target list later on. This makes + * DEFINE expression could access the outer tuple while evaluating + * PATTERN. + * + * XXX: adding whole expressions of DEFINE to the plan.targetlist is + * not so good, because it's not necessary to evalute the expression + * in the target list while running the plan. We should extract the + * var nodes only then add them to the plan.targetlist. + */ + findTargetlistEntrySQL99(pstate, (Node *) restarget->val, + targetlist, EXPR_KIND_RPR_DEFINE); + + /* + * Make sure that the row pattern definition search condition is a + * boolean expression. + */ + transformWhereClause(pstate, restarget->val, + EXPR_KIND_RPR_DEFINE, "DEFINE"); + + foreach(l, restargets) + { + char *n; + + r = (ResTarget *) lfirst(l); + n = r->name; + + if (!strcmp(n, name)) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("row pattern definition variable name \"%s\" appears more than once in DEFINE clause", + name), + parser_errposition(pstate, exprLocation((Node *) r)))); + } + restargets = lappend(restargets, restarget); + } + list_free(restargets); + + /* + * Create list of row pattern DEFINE variable name's initial. We assign + * [a-z] to them (up to 26 variable names are allowed). + */ + restargets = NIL; + i = 0; + initialLen = strlen(defineVariableInitials); + + foreach(lc, windef->rpCommonSyntax->rpDefs) + { + char initial[2]; + + restarget = (ResTarget *) lfirst(lc); + name = restarget->name; + + if (i >= initialLen) + { + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("number of row pattern definition variable names exceeds %d", + initialLen), + parser_errposition(pstate, + exprLocation((Node *) restarget)))); + } + initial[0] = defineVariableInitials[i++]; + initial[1] = '\0'; + wc->defineInitial = lappend(wc->defineInitial, + makeString(pstrdup(initial))); + } + + defineClause = transformTargetList(pstate, windef->rpCommonSyntax->rpDefs, + EXPR_KIND_RPR_DEFINE); + + /* mark column origins */ + markTargetListOrigins(pstate, defineClause); + + /* mark all nodes in the DEFINE clause tree with collation information */ + assign_expr_collations(pstate, (Node *) defineClause); + + return defineClause; +} + +/* + * transformPatternClause + * Process PATTERN clause and return PATTERN clause in the raw parse tree + */ +static void +transformPatternClause(ParseState *pstate, WindowClause *wc, + WindowDef *windef) +{ + ListCell *lc; + + /* + * Row Pattern Common Syntax clause exists? + */ + if (windef->rpCommonSyntax == NULL) + return; + + wc->patternVariable = NIL; + wc->patternRegexp = NIL; + foreach(lc, windef->rpCommonSyntax->rpPatterns) + { + A_Expr *a; + char *name; + char *regexp; + + if (!IsA(lfirst(lc), A_Expr)) + ereport(ERROR, + errmsg("node type is not A_Expr")); + + a = (A_Expr *) lfirst(lc); + name = strVal(a->lexpr); + + wc->patternVariable = lappend(wc->patternVariable, makeString(pstrdup(name))); + regexp = strVal(lfirst(list_head(a->name))); + + wc->patternRegexp = lappend(wc->patternRegexp, makeString(pstrdup(regexp))); + } +} + +/* + * transformMeasureClause + * Process MEASURE clause + * XXX MEASURE clause is not supported yet + */ +static List * +transformMeasureClause(ParseState *pstate, WindowClause *wc, + WindowDef *windef) +{ + if (windef->rowPatternMeasures == NIL) + return NIL; + + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("%s", "MEASURE clause is not supported yet"), + parser_errposition(pstate, exprLocation((Node *) windef->rowPatternMeasures)))); + return NIL; +} diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index aba3546ed1..eb138087bf 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -578,6 +578,7 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref) case EXPR_KIND_COPY_WHERE: case EXPR_KIND_GENERATED_COLUMN: case EXPR_KIND_CYCLE_MARK: + case EXPR_KIND_RPR_DEFINE: /* okay */ break; @@ -1817,6 +1818,7 @@ transformSubLink(ParseState *pstate, SubLink *sublink) case EXPR_KIND_VALUES: case EXPR_KIND_VALUES_SINGLE: case EXPR_KIND_CYCLE_MARK: + case EXPR_KIND_RPR_DEFINE: /* okay */ break; case EXPR_KIND_CHECK_CONSTRAINT: @@ -3197,6 +3199,8 @@ ParseExprKindName(ParseExprKind exprKind) return "GENERATED AS"; case EXPR_KIND_CYCLE_MARK: return "CYCLE"; + case EXPR_KIND_RPR_DEFINE: + return "DEFINE"; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_func.c b/src/backend/parser/parse_func.c index 9b23344a3b..4c482abb30 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -2658,6 +2658,9 @@ check_srf_call_placement(ParseState *pstate, Node *last_srf, int location) case EXPR_KIND_CYCLE_MARK: errkind = true; break; + case EXPR_KIND_RPR_DEFINE: + errkind = true; + break; /* * There is intentionally no default: case here, so that the -- 2.25.1 ----Next_Part(Sat_May_11_16_23_07_2024_789)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="v18-0003-Row-pattern-recognition-patch-rewriter.patch" ^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2024-05-11 07:11 UTC | newest] Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2021-07-08 06:08 [PATCH v3 1/3] Be strict in numeric parameters on command line Kyotaro Horiguchi <horikyota.ntt@gmail.com> 2021-07-08 06:08 [PATCH 1/2] Be strict in numeric parameters on command line Kyotaro Horiguchi <horikyota.ntt@gmail.com> 2021-07-08 06:08 [PATCH v2 1/2] Be strict in numeric parameters on command line Kyotaro Horiguchi <horikyota.ntt@gmail.com> 2021-07-08 06:08 [PATCH v3 1/3] Be strict in numeric parameters on command line Kyotaro Horiguchi <horikyota.ntt@gmail.com> 2021-07-08 06:08 [PATCH v2 1/2] Be strict in numeric parameters on command line Kyotaro Horiguchi <horikyota.ntt@gmail.com> 2021-07-08 06:08 [PATCH v3 1/3] Be strict in numeric parameters on command line Kyotaro Horiguchi <horikyota.ntt@gmail.com> 2024-05-11 07:11 [PATCH v18 2/8] Row pattern recognition patch (parse/analysis). Tatsuo Ishii <ishii@postgresql.org>
This inbox is served by agora; see mirroring instructions for how to clone and mirror all data and code used for this inbox