From f7a9a3d4b24a4ad0de7992f01a0dd2a02ccd30a4 Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Tue, 27 Jul 2021 10:39:05 +0900 Subject: Skip trailing whitespaces when parsing integer options 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/fe_utils/option_utils.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/fe_utils/option_utils.c') diff --git a/src/fe_utils/option_utils.c b/src/fe_utils/option_utils.c index 3e7e512ad91..bcfe7365fd3 100644 --- a/src/fe_utils/option_utils.c +++ b/src/fe_utils/option_utils.c @@ -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); -- cgit v1.2.3