Hi, Two integer options are still read with atoi(), so a non-numeric value silently becomes 0:
pg_createsubscriber --recovery-timeout=abc timeout disabled (0 = none) pgbench --max-tries=abc unlimited tries pg_createsubscriber also accepts negative timeouts. The other client programs moved to option_parse_int() in 2021 [1]. pg_ctl was left out on purpose there (Windows event logging), but pg_createsubscriber did not exist yet and uses the common logging. The attached patch uses option_parse_int() for both, still allowing 0. For --max-tries the error for a negative value changes from "invalid number of maximum tries" to the usual "must be in range" message. Tests are added in https://www.google.com/url?q=http://040_pg_createsubscriber.pl&source=gmail&ust=1795804224713000&sa=E and https://www.google.com/url?q=http://002_pgbench_no_server.pl&source=gmail&ust=1795804224713000&sa=E. They fail without the change and pass with it. [1] https://www.google.com/url?q=https://postgr.es/m/calj2acxqdg9whqvoj9zyf-izt7sgk7szv5uss%[email protected]&source=gmail&ust=1795804224713000&sa=E Regards, Sai Pranav
From 46fe9352a25ae4b866bbd27c41cdbca9ff2df7bb Mon Sep 17 00:00:00 2001 From: Sai Pranav <[email protected]> Date: Thu, 26 Nov 2026 20:42:19 +0530 Subject: [PATCH v1] Use option_parse_int() for two more integer options pg_createsubscriber --recovery-timeout and pgbench --max-tries were read with atoi(), so a non-numeric value became 0: for --recovery-timeout that silently disables the timeout, and for --max-tries it means an unlimited number of tries. pg_createsubscriber also accepted negative timeouts. Use option_parse_int() like the other integer options, with 0 still allowed for both, as documented. --- src/bin/pg_basebackup/pg_createsubscriber.c | 6 +++++- src/bin/pg_basebackup/t/040_pg_createsubscriber.pl | 11 +++++++++++ src/bin/pgbench/pgbench.c | 7 ++++--- src/bin/pgbench/t/002_pgbench_no_server.pl | 7 ++++++- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/bin/pg_basebackup/pg_createsubscriber.c b/src/bin/pg_basebackup/pg_createsubscriber.c index 20b354aed5..1c4b670fbb 100644 --- a/src/bin/pg_basebackup/pg_createsubscriber.c +++ b/src/bin/pg_basebackup/pg_createsubscriber.c @@ -13,6 +13,7 @@ #include "postgres_fe.h" +#include <limits.h> #include <sys/stat.h> #include <sys/time.h> #include <sys/wait.h> @@ -26,6 +27,7 @@ #include "common/pg_prng.h" #include "common/restricted_token.h" #include "datatype/timestamp.h" +#include "fe_utils/option_utils.h" #include "fe_utils/recovery_gen.h" #include "fe_utils/simple_list.h" #include "fe_utils/string_utils.h" @@ -2366,7 +2368,9 @@ main(int argc, char **argv) canonicalize_path(opt.socket_dir); break; case 't': - opt.recovery_timeout = atoi(optarg); + if (!option_parse_int(optarg, "-t/--recovery-timeout", 0, INT_MAX, + &opt.recovery_timeout)) + exit(1); break; case 'T': opt.two_phase = true; diff --git a/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl b/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl index 9252d1c3c5..3adfb04ca4 100644 --- a/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl +++ b/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl @@ -42,6 +42,17 @@ sub generate_db return $dbname; } +# +# Test invalid option values +command_fails_like( + [ 'pg_createsubscriber', '--recovery-timeout' => 'abc' ], + qr/invalid value "abc" for option -t\/--recovery-timeout/, + 'non-numeric recovery timeout'); +command_fails_like( + [ 'pg_createsubscriber', '--recovery-timeout' => '-1' ], + qr/-t\/--recovery-timeout must be in range 0\.\.2147483647/, + 'negative recovery timeout'); + # # Test mandatory options command_fails(['pg_createsubscriber'], diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 06c43fd694..196187fe3f 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -7121,10 +7121,11 @@ main(int argc, char **argv) break; case 14: /* max-tries */ { - int32 max_tries_arg = atoi(optarg); + int32 max_tries_arg; - if (max_tries_arg < 0) - pg_fatal("invalid number of maximum tries: \"%s\"", optarg); + if (!option_parse_int(optarg, "--max-tries", 0, INT_MAX, + &max_tries_arg)) + exit(1); benchmarking_option_set = true; max_tries = (uint32) max_tries_arg; diff --git a/src/bin/pgbench/t/002_pgbench_no_server.pl b/src/bin/pgbench/t/002_pgbench_no_server.pl index 0e8d92f8ab..b440e77c35 100644 --- a/src/bin/pgbench/t/002_pgbench_no_server.pl +++ b/src/bin/pgbench/t/002_pgbench_no_server.pl @@ -190,7 +190,12 @@ my @options = ( [ 'bad maximum number of tries', '--max-tries -10', - [qr{invalid number of maximum tries: "-10"}] + [qr{--max-tries must be in range 0\.\.2147483647}] + ], + [ + 'non-numeric maximum number of tries', + '--max-tries abc', + [qr{invalid value "abc" for option --max-tries}] ], [ 'an infinite number of tries', -- 2.50.1 (Apple Git-155)
