# HG changeset patch # User Mike Becker # Date 1734269559 -3600 # Node ID 56eb7da4f3e1b47abf4c4f32fd3b2f9896c30fec # Parent add8358fc3c34bf38ae86de98817a475b152fe62 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 diff -r add8358fc3c3 -r 56eb7da4f3e1 src/json.c --- 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 #include #include +#include #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; }