Reject NaN and trailing garbage in client-side floating-point options

From: Raja Sai pranav <rajasaipranav0(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Reject NaN and trailing garbage in client-side floating-point options
Date: 2026-11-26 18:30:23
Message-ID: CAPHduZDbk+eAGJESJMtaGtLwgeE8UP32WDWQvdz8aMcNMxGzrQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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%3Dhe6NnWQ2ofTA@mail.gmail.com&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

Attachment Content-Type Size
v1-0001-Reject-NaN-and-trailing-garbage-in-client-side-fl.patch text/x-patch 8.4 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Raja Sai pranav 2026-11-26 18:30:25 Use option_parse_int() for pg_createsubscriber --recovery-timeout and pgbench --max-tries
Previous Message Aleksander Alekseev 2026-11-26 18:29:40 Re: Write skew observed under serializable isolation