Sun, 15 Dec 2024 14:32:39 +0100
fix number parser not detecting integers out of range
Note: for doubles the same approach does not work, because
it takes a lot to push a double out of range (for strtod)
and long before that happens, the value gets insanely
imprecise.
relates to #431
src/json.c | file | annotate | diff | comparison | revisions |
--- a/src/json.c Sun Dec 15 13:44:08 2024 +0100 +++ b/src/json.c Sun Dec 15 14:32:39 2024 +0100 @@ -30,6 +30,7 @@ #include <ctype.h> #include <assert.h> #include <stdio.h> +#include <errno.h> #include "cx/json.h" @@ -38,8 +39,6 @@ * https://tools.ietf.org/html/rfc8259 */ -#define PARSER_READVALUE_ALLOC 32 - static CxJsonValue cx_json_value_nothing = {.type = CX_JSON_NOTHING}; static void token_destroy(CxJsonToken *token) { @@ -300,10 +299,15 @@ str.ptr[str.length] = 0; if (asint) { + errno = 0; long long v = strtoll(str.ptr, &endptr, 10); + if (errno == ERANGE) { + return 1; + } *((int64_t*)value) = (int64_t) v; } else { // TODO: proper JSON spec number parser + // TODO: also return an error when loss of precision is high double v = strtod(str.ptr, &endptr); *((double*)value) = v; }