Skip to content

Commit

Permalink
Merge pull request #240 from jow-/stricter-number-conversion
Browse files Browse the repository at this point in the history
vallist: more thoroughly check for trailing garbage after numeric string
  • Loading branch information
jow- authored Oct 17, 2024
2 parents 9cf53dd + 4134e71 commit 07afe96
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
23 changes: 23 additions & 0 deletions tests/custom/99_bugs/49_trailing_garbage_string_as_number
Original file line number Diff line number Diff line change
@@ -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 --
10 changes: 8 additions & 2 deletions vallist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit 07afe96

Please sign in to comment.