Hi, Several client programs parse floating-point option values with atof() or strtod() and then range-check them with comparisons such as "x <= 0". NaN fails every comparison, so "nan" gets through, and atof() also stops at the first bad character. On master (3b249c728f):
pg_basebackup --max-rate=nan backup runs without any rate limit pgbench -R nan runs; "rate limit schedule lag: avg 234546389 ms" pgbench -L nan latency limit silently off pgbench -R 10abc / -L 5abc taken as 10 and 5 pgbench --sampling-rate=nan accepted pg_test_timing --cutoff=nan accepted psql: SELECT 1 \watch nan runs with no delay psql: SELECT 1 \watch i=inf "could not set timer: Invalid argument" In pg_basebackup the NaN is then converted to an integer, which is undefined behavior in C (here it came out as 0, meaning no limit). The attached patch: - pgbench -L, -R and --sampling-rate use pgbench's existing strtodouble() instead of atof(), so trailing garbage is rejected, and reject NaN and infinity; - pg_basebackup --max-rate rejects NaN as "not a valid value" (infinity was already rejected by the integer range check); - pg_test_timing --cutoff and psql's \watch interval reject NaN, and \watch also rejects infinity, which only failed later at setitimer(). One behavior change: pgbench options that atof() used to accept with trailing characters, including trailing spaces, are now rejected. When integer options moved to option_parse_int() in 2021, the pgbench float options were left as they were [1]; the NaN cases above (an unthrottled backup, a garbage rate schedule) seem worth fixing even if the trailing-garbage part is not wanted. I can split that out if preferred. [1] https://www.google.com/url?q=https://postgr.es/m/calj2acxqdg9whqvoj9zyf-izt7sgk7szv5uss%[email protected]&source=gmail&ust=1795804223232000&sa=E Tests are added next to the existing ones for each option. Each new test fails without the code change and passes with it. The pgbench, psql, pg_basebackup and pg_test_timing TAP suites pass. Regards, Sai Pranav
From f56033572190fb1034dacb836210114a53da0380 Mon Sep 17 00:00:00 2001 From: Sai Pranav <[email protected]> Date: Thu, 26 Nov 2026 20:23:06 +0530 Subject: [PATCH v1] Reject NaN and trailing garbage in client-side floating-point options Several client programs read floating-point option values with atof() or strtod() and then range-check them with comparisons like "x <= 0". NaN fails every comparison, so "nan" was accepted, and atof() silently ignored anything after the number. For example, pg_basebackup --max-rate=nan ran without any rate limit (after converting NaN to an integer, which is undefined behavior), pgbench -R nan produced a meaningless schedule, pgbench -R 10abc was taken as 10, and psql's \watch i=inf failed only later with "could not set timer". pgbench's -L, -R and --sampling-rate now use its existing strtodouble() and reject NaN and infinity. pg_basebackup --max-rate, pg_test_timing --cutoff and psql's \watch interval reject NaN, and \watch also rejects infinity. --- src/bin/pg_basebackup/pg_basebackup.c | 3 ++- src/bin/pg_basebackup/t/010_pg_basebackup.pl | 8 ++++++++ src/bin/pg_test_timing/pg_test_timing.c | 2 +- src/bin/pg_test_timing/t/001_basic.pl | 4 ++++ src/bin/pgbench/pgbench.c | 15 ++++++++------- src/bin/pgbench/t/002_pgbench_no_server.pl | 12 ++++++++++++ src/bin/psql/command.c | 5 +++-- src/bin/psql/t/001_basic.pl | 10 ++++++++++ 8 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 4daf985ab8..bcb3216c7e 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -16,6 +16,7 @@ #include <unistd.h> #include <dirent.h> #include <limits.h> +#include <math.h> #include <sys/select.h> #include <sys/stat.h> #include <sys/wait.h> @@ -904,7 +905,7 @@ parse_max_rate(char *src) errno = 0; result = strtod(src, &after_num); - if (src == after_num) + if (src == after_num || isnan(result)) pg_fatal("transfer rate \"%s\" is not a valid value", src); if (errno != 0) pg_fatal("invalid transfer rate \"%s\": %m", src); diff --git a/src/bin/pg_basebackup/t/010_pg_basebackup.pl b/src/bin/pg_basebackup/t/010_pg_basebackup.pl index 23a757b1c8..a3cb6370b0 100644 --- a/src/bin/pg_basebackup/t/010_pg_basebackup.pl +++ b/src/bin/pg_basebackup/t/010_pg_basebackup.pl @@ -55,6 +55,14 @@ $node->command_fails_like( ], qr/\Qunrecognized compression algorithm: "none+"/, 'failure on incorrect separator to define compression level'); +$node->command_fails_like( + [ + 'pg_basebackup', + '--pgdata' => "$tempdir/backup", + '--max-rate' => 'nan' + ], + qr/\Qtransfer rate "nan" is not a valid value/, + 'failure on NaN transfer rate'); # Some Windows ANSI code pages may reject this filename, in which case we # quietly proceed without this bit of test coverage. diff --git a/src/bin/pg_test_timing/pg_test_timing.c b/src/bin/pg_test_timing/pg_test_timing.c index d63018f270..7adc758168 100644 --- a/src/bin/pg_test_timing/pg_test_timing.c +++ b/src/bin/pg_test_timing/pg_test_timing.c @@ -103,7 +103,7 @@ handle_args(int argc, char *argv[]) exit(1); } - if (max_rprct < 0 || max_rprct > 100) + if (!(max_rprct >= 0 && max_rprct <= 100)) { fprintf(stderr, _("%s: %s must be in range %u..%u\n"), progname, "--cutoff", 0, 100); diff --git a/src/bin/pg_test_timing/t/001_basic.pl b/src/bin/pg_test_timing/t/001_basic.pl index 430d1ed817..f8e07d814f 100644 --- a/src/bin/pg_test_timing/t/001_basic.pl +++ b/src/bin/pg_test_timing/t/001_basic.pl @@ -29,6 +29,10 @@ command_fails_like( [ 'pg_test_timing', '--cutoff' => '101' ], qr/\Qpg_test_timing: --cutoff must be in range 0..100\E/, 'pg_test_timing: --cutoff must be in range'); +command_fails_like( + [ 'pg_test_timing', '--cutoff' => 'nan' ], + qr/\Qpg_test_timing: --cutoff must be in range 0..100\E/, + 'pg_test_timing: --cutoff rejects NaN'); ######################################### # We obviously can't check for specific output, but we can diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 06c43fd694..d8c57843de 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -6966,9 +6966,10 @@ main(int argc, char **argv) break; case 'L': { - double limit_ms = atof(optarg); + double limit_ms; - if (limit_ms <= 0.0) + if (!strtodouble(optarg, true, &limit_ms) || + !(limit_ms > 0.0) || isinf(limit_ms)) pg_fatal("invalid latency limit: \"%s\"", optarg); benchmarking_option_set = true; latency_limit = (int64) (limit_ms * 1000); @@ -7009,12 +7010,12 @@ main(int argc, char **argv) break; case 'R': { - /* get a double from the beginning of option value */ - double throttle_value = atof(optarg); + double throttle_value; benchmarking_option_set = true; - if (throttle_value <= 0.0) + if (!strtodouble(optarg, true, &throttle_value) || + !(throttle_value > 0.0) || isinf(throttle_value)) pg_fatal("invalid rate limit: \"%s\"", optarg); /* Invert rate limit into per-transaction delay in usec */ throttle_delay = 1000000.0 / throttle_value; @@ -7064,8 +7065,8 @@ 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) + if (!strtodouble(optarg, true, &sample_rate) || + !(sample_rate > 0.0 && sample_rate <= 1.0)) pg_fatal("invalid sampling rate: \"%s\"", optarg); break; case 5: /* aggregate-interval */ diff --git a/src/bin/pgbench/t/002_pgbench_no_server.pl b/src/bin/pgbench/t/002_pgbench_no_server.pl index 0e8d92f8ab..422309f0c1 100644 --- a/src/bin/pgbench/t/002_pgbench_no_server.pl +++ b/src/bin/pgbench/t/002_pgbench_no_server.pl @@ -128,11 +128,23 @@ my @options = ( [qr{-P/--progress must be in range}] ], [ 'invalid rate', '--rate=0.0', [qr{invalid rate limit}] ], + [ 'invalid rate NaN', '--rate=nan', [qr{invalid rate limit}] ], + [ 'invalid rate infinite', '--rate=inf', [qr{invalid rate limit}] ], + [ 'invalid rate trailing garbage', '--rate=10abc', [qr{invalid rate limit}] ], [ 'invalid latency', '--latency-limit=0.0', [qr{invalid latency limit}] ], + [ 'invalid latency NaN', '--latency-limit=nan', [qr{invalid latency limit}] ], + [ + 'invalid latency trailing garbage', '--latency-limit=5abc', + [qr{invalid latency limit}] + ], [ 'invalid sampling rate', '--sampling-rate=0', [qr{invalid sampling rate}] ], + [ + 'invalid sampling rate NaN', '--sampling-rate=nan', + [qr{invalid sampling rate}] + ], [ 'invalid aggregate interval', '--aggregate-interval=-3', diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 407d263fb2..9db73dabbd 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -8,6 +8,7 @@ #include "postgres_fe.h" #include <ctype.h> +#include <math.h> #include <time.h> #include <pwd.h> #include <utime.h> @@ -3433,7 +3434,7 @@ exec_command_watch(PsqlScanState scan_state, bool active_branch, have_sleep = true; errno = 0; sleep = strtod(valptr, &opt_end); - if (sleep < 0 || *opt_end || errno == ERANGE) + if (!(sleep >= 0) || isinf(sleep) || *opt_end || errno == ERANGE) { pg_log_error("\\watch: incorrect interval value \"%s\"", valptr); success = false; @@ -3499,7 +3500,7 @@ exec_command_watch(PsqlScanState scan_state, bool active_branch, have_sleep = true; errno = 0; sleep = strtod(opt, &opt_end); - if (sleep < 0 || *opt_end || errno == ERANGE) + if (!(sleep >= 0) || isinf(sleep) || *opt_end || errno == ERANGE) { pg_log_error("\\watch: incorrect interval value \"%s\"", opt); success = false; diff --git a/src/bin/psql/t/001_basic.pl b/src/bin/psql/t/001_basic.pl index 028df33ce8..2a37150e44 100644 --- a/src/bin/psql/t/001_basic.pl +++ b/src/bin/psql/t/001_basic.pl @@ -424,6 +424,16 @@ psql_fails_like( 'SELECT 1 \watch 10e400', qr/incorrect interval value "10e400"/, '\watch, out-of-range interval'); +psql_fails_like( + $node, + 'SELECT 1 \watch i=nan c=1', + qr/incorrect interval value "nan"/, + '\watch, NaN interval'); +psql_fails_like( + $node, + 'SELECT 1 \watch i=inf c=1', + qr/incorrect interval value "inf"/, + '\watch, infinite interval'); psql_fails_like( $node, 'SELECT 1 \watch 1 1', -- 2.50.1 (Apple Git-155)
