option_parse_int() passes the argument to strtoint() and only checks
that nothing but whitespace follows the number.  For an empty or
all-whitespace string strtoint() consumes nothing and returns 0, so the
check passes and the option is silently accepted as 0.  Report it as an invalid 
value, as
is already done for other non-numeric input.
---
Hi,

I may have missed something, so please correct me where I am wrong.

option_parse_int() accepts an empty string and returns 0. For example, on 
current master:

  $ pg_recvlogical --start -F ''
  (starts, with an fsync interval of 0)

The reason is that strtoint() consumes nothing for "", leaves endptr at the 
start of the string, and the "only whitespace remains" check then passes. The 
same happens for a string of only spaces. I saw "" accepted as 0 in 
pg_recvlogical (-F, -s), pg_basebackup -s, pg_receivewal -s, pg_dump 
--extra-float-digits, vacuumdb -P, pgbench --partitions and createuser -c. For 
options with a minimum of 1, such input after the patch changes from a "must be 
in range" error to "invalid value".

The attached patch rejects the case when endptr == optarg, with the same 
message the function already uses for other invalid input. Inputs such as " 5" 
and "5 " behave as before.

I tested on master (8303f2e13c) with --enable-cassert, rebuilding the binaries 
with "make clean" in src/bin first. After the patch, all 8 tools I tried (the 7 
above and pg_checksums) print: invalid value "" for option ..., with exit code 
1. "make check" passed in src/bin/pg_basebackup, src/bin/scripts, 
src/bin/pg_dump, src/bin/pg_amcheck and src/test/regress, and pgindent shows no 
diff. I found no existing test or caller that passes an empty string on purpose.

What I did not test: Windows/MSVC, meson, translations, and the other tools 
that use option_parse_int(). I did not add a regression test. I searched the 
pgsql-hackers archive and the git history for a previous report or fix and 
found none, but I could not search the Commitfest, so this may be a duplicate.

Does this make sense? If you would rather see a test, or a different behavior, 
I am happy to change the patch.

Thank you for your time.
 src/fe_utils/option_utils.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/fe_utils/option_utils.c b/src/fe_utils/option_utils.c
index c8bba075c1..3ce916bd6a 100644
--- a/src/fe_utils/option_utils.c
+++ b/src/fe_utils/option_utils.c
@@ -57,6 +57,14 @@ option_parse_int(const char *optarg, const char *optname,
        errno = 0;
        val = strtoint(optarg, &endptr, 10);
 
+       /* An empty or all-whitespace string is not a valid integer. */
+       if (endptr == optarg)
+       {
+               pg_log_error("invalid value \"%s\" for option %s",
+                                        optarg, optname);
+               return false;
+       }
+
        /*
         * Skip any trailing whitespace; if anything but whitespace remains 
before
         * the terminating character, fail.
-- 
2.39.5



Reply via email to