Merge pull request #958 from dxbjavid/uint64-negative-whitespace

reject negative after whitespace in json_parse_uint64
This commit is contained in:
Eric Hawicz
2026-08-20 09:36:43 -04:00
committed by GitHub
3 changed files with 13 additions and 1 deletions
+7 -1
View File
@@ -263,7 +263,13 @@ int json_parse_uint64(const char *buf, uint64_t *retval)
uint64_t val; uint64_t val;
errno = 0; errno = 0;
while (*buf == ' ') /* strtoull() skips leading whitespace and then quietly negates a
* leading '-', wrapping the result into a huge value. We only skipped
* spaces here, so a '-' behind a tab/newline/etc slipped past the check
* below. Skip the same whitespace set strtoull() does so the rejection
* actually holds. */
while (*buf == ' ' || *buf == '\t' || *buf == '\n' || *buf == '\v' || *buf == '\f' ||
*buf == '\r')
buf++; buf++;
if (*buf == '-') if (*buf == '-')
return 1; /* error: uint cannot be negative */ return 1; /* error: uint cannot be negative */
+5
View File
@@ -145,6 +145,11 @@ int main(int argc, char **argv)
strcpy(buf, "-9223372036854775808"); strcpy(buf, "-9223372036854775808");
checkit_uint(buf); checkit_uint(buf);
// A negative value hidden behind non-space whitespace must still be
// rejected; strtoull() skips this whitespace and would wrap the '-'.
strcpy(buf, "\t-1");
checkit_uint(buf);
strcpy(buf, " 1"); strcpy(buf, " 1");
checkit_uint(buf); checkit_uint(buf);
+1
View File
@@ -41,6 +41,7 @@ buf=1 parseit=0, value=1
buf=2147483647 parseit=0, value=2147483647 buf=2147483647 parseit=0, value=2147483647
buf=-1 parseit=1, value=666 buf=-1 parseit=1, value=666
buf=-9223372036854775808 parseit=1, value=666 buf=-9223372036854775808 parseit=1, value=666
buf= -1 parseit=1, value=666
buf= 1 parseit=0, value=1 buf= 1 parseit=0, value=1
buf=00001234 parseit=0, value=1234 buf=00001234 parseit=0, value=1234
buf=0001234x parseit=0, value=1234 buf=0001234x parseit=0, value=1234