Fix empty and whitespace-only input handling in option_parse_int()

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: pgsql-hackers mailing list <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Raja Sai pranav <rajasaipranav0(at)gmail(dot)com>
Subject: Fix empty and whitespace-only input handling in option_parse_int()
Date: 2026-11-27 06:13:46
Message-ID: E9B024DF-43B7-45FA-B8E0-D66FDE499D4A@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

While reviewing and testing patch [1] that replaces atoi() with option_parse_int() for CLI option parsing, I found that option_parse_int() doesn’t handle whitespace-only strings properly. It treats them as 0.

Basically, what option_parse_int() does is like:

1. Use strtoint() to parse an option value
2. Skip all trailing whitespace
3. Check if any characters remain. If yes, return an error. This step detects invalid values like “1a”, “1 a”, and “1 a ”
4. Check for out-of-range error

So, for a whitespace-only value, strtoint() returns 0 in step 1, step 2 skips all whitespace, step 3 won’t detect an error as no characters remain, and step 4 may pass because 0 is valid for many options.

To fix this bug, we need to check if strtoint() fails to convert the string. strtoint() internally calls strtol(). According to the POSIX doc for strtol() [2]:
```
Upon successful completion, these functions shall return the
converted value, if any. If no conversion could be performed, 0
shall be returned and errno may be set to [EINVAL].
```

When no conversion happens, strtol() MAY set errno to EINVAL, which implies that we should not rely on errno == EINVAL, as some implementations may not set errno.

So, we can just check endptr==optarg. When they are equal, it means that no conversion was performed. This covers both empty strings and whitespace-only cases.

Actually, when a user directly types a command line in a shell, it’s very unlikely to make such a mistake of passing whitespace as an option value, because the shell treats whitespace as a separator unless the user explicitly quotes it, like:
```
$ pg_dump --extra-float-digits=‘ ‘ ...
```

However, when debugging in an IDE like VSCode, which I’m using, where we configure command-line arguments in JSON format, it might be easier to mistakenly specify whitespace as an option value, as in the following configuration I’m using to debug this patch:
```
{
"name": "(lldb) Run pgbench",
"type": "cppdbg",
"request": "launch",
"program": "/usr/local/pgsql/bin/pgbench",
"args": ["-i", "-d", "evantest", "--partitions= "],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
},
```

[1] https://postgr.es/m/CAPHduZCSNYcPOmWgA5Kfo9DR-ShdOEw1SKg2dw_kXxx+zocZRA@mail.gmail.com
[2] https://man7.org/linux/man-pages/man3/strtol.3p.html

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachment Content-Type Size
v1-0001-Reject-empty-and-whitespace-only-integer-option-v.patch application/octet-stream 2.2 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2026-11-27 06:19:06 Re: Preserve conflict log table during upgrade
Previous Message Michael Paquier 2026-11-27 05:57:32 Re: [PATCH] libpq: Add PQpassfileLookup()