From 4134e7182624b37bf17d91fc89b500f5cb443187 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Wed, 16 Oct 2024 12:09:31 +0200 Subject: vallist: more thoroughly check for trailing garbage after numeric string When converting numeric strings into numbers, ensure that only optional trailing whitespace follows and no other characters. Fixes: #231 Signed-off-by: Jo-Philipp Wich --- .../99_bugs/49_trailing_garbage_string_as_number | 23 ++++++++++++++++++++++ vallist.c | 10 ++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 tests/custom/99_bugs/49_trailing_garbage_string_as_number diff --git a/tests/custom/99_bugs/49_trailing_garbage_string_as_number b/tests/custom/99_bugs/49_trailing_garbage_string_as_number new file mode 100644 index 0000000..1b48146 --- /dev/null +++ b/tests/custom/99_bugs/49_trailing_garbage_string_as_number @@ -0,0 +1,23 @@ +Ensure that numeric strings followed by non-whitespace are treated as NaN. + +-- Testcase -- +{% +printf("%.J\n", [ + "1" == 1, + " 1" == 1, + "1 " == 1, + "1a" == 1, + "1 a" == 1 +]); +%} +-- End -- + +-- Expect stdout -- +[ + true, + true, + true, + false, + false +] +-- End -- diff --git a/vallist.c b/vallist.c index 886ede0..61a4a59 100644 --- a/vallist.c +++ b/vallist.c @@ -106,7 +106,10 @@ uc_number_parse_common(const char *buf, bool octal, char **end) if (base >= 10 && (**end == '.' || (**end|32) == 'e')) { d = strtod(p, end); - if (!isspace(**end) && **end != 0) + while (isspace(**end)) + (*end)++; + + if (**end != 0) return NULL; if (neg) @@ -115,7 +118,10 @@ uc_number_parse_common(const char *buf, bool octal, char **end) return ucv_double_new(d); } - if (!isspace(**end) && **end != 0) + while (isspace(**end)) + (*end)++; + + if (**end != 0) return NULL; if (neg) { -- cgit v1.2.3