Reject NaN and trailing garbage in client-side floating-point options
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]https://www.google.com/url?q=https://postgr.es/m/CALj2ACXqdG9WhqVoJ9zYf-iZt7sgK7Szv5USs%3Dhe6NnWQ2ofTA@mail.gmail.com&source=gmail&ust=1795804223232000&sa=E; 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
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