Skip trailing whitespaces when parsing integer options
authorMichael Paquier <michael@paquier.xyz>
Tue, 27 Jul 2021 01:39:05 +0000 (10:39 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 27 Jul 2021 01:39:05 +0000 (10:39 +0900)
strtoint(), via strtol(), would skip leading whitespaces but the same
rule was not applied for trailing whitespaces, leading to an
inconsistent behavior.  Some tests are changed to cover more this area.

Author: Michael Paquier
Reviewed-by: Kyotaro Horiguchi
Discussion: https://postgr.es/m/YP5Pv0d13Ct+03ve@paquier.xyz

src/bin/pg_basebackup/t/020_pg_receivewal.pl
src/bin/pg_dump/t/001_basic.pl
src/fe_utils/option_utils.c

index 158f7d176fef5b73843133a93107dae38199d661..47c4ecb073b91ab7fc895f5eb19f2ab72d6f5fef 100644 (file)
@@ -88,10 +88,12 @@ SKIP:
    $primary->psql('postgres',
        'INSERT INTO test_table VALUES (generate_series(100,200));');
 
+   # Note the trailing whitespace after the value of --compress, that is
+   # a valid value.
    $primary->command_ok(
        [
            'pg_receivewal', '-D',     $stream_dir,  '--verbose',
-           '--endpos',      $nextlsn, '--compress', '1'
+           '--endpos',      $nextlsn, '--compress', '1 '
        ],
        "streaming some WAL using ZLIB compression");
 
index 59de6df7bad32938667747e75c218b0f9f2a80b8..d1a7e1db405ba4953dfc1685650fa67b358e4730 100644 (file)
@@ -101,8 +101,9 @@ command_fails_like(
    qr/\Qpg_dump: error: parallel backup only supported by the directory format\E/,
    'pg_dump: parallel backup only supported by the directory format');
 
+# Note the trailing whitespace for the value of --jobs, that is valid.
 command_fails_like(
-   [ 'pg_dump', '-j', '-1' ],
+   [ 'pg_dump', '-j', '-1 ' ],
    qr/\Qpg_dump: error: -j\/--jobs must be in range\E/,
    'pg_dump: -j/--jobs must be in range');
 
index 3e7e512ad9112502bd3b2c3a4c95fed6cda631b9..bcfe7365fd32c5e7483b0e476106e74e672e06a2 100644 (file)
@@ -57,7 +57,14 @@ option_parse_int(const char *optarg, const char *optname,
    errno = 0;
    val = strtoint(optarg, &endptr, 10);
 
-   if (*endptr)
+   /*
+    * Skip any trailing whitespace; if anything but whitespace remains before
+    * the terminating character, fail.
+    */
+   while (*endptr != '\0' && isspace((unsigned char) *endptr))
+       endptr++;
+
+   if (*endptr != '\0')
    {
        pg_log_error("invalid value \"%s\" for option %s",
                     optarg, optname);