From ddf63e0863b6e872d61a269459f2f88714928f12 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Mon, 9 Mar 2026 16:48:05 +0100 Subject: [PATCH] json: relax float parsing some more We already support more float variants than standard JSON in the relaxed format, adding extra restrictions does not actually help much. If you need to know if this is a value JSON number, there is now a function to check that instead. --- spa/include/spa/utils/json-core.h | 14 ++------------ test/test-spa-json.c | 12 +++++++++--- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/spa/include/spa/utils/json-core.h b/spa/include/spa/utils/json-core.h index 68309aaa9..6d9cbe566 100644 --- a/spa/include/spa/utils/json-core.h +++ b/spa/include/spa/utils/json-core.h @@ -425,20 +425,11 @@ SPA_API_JSON bool spa_json_is_null(const char *val, int len) /* float */ SPA_API_JSON int spa_json_parse_float(const char *val, int len, float *result) { - char buf[96]; - char *end; - int pos; + char buf[96], *end; if (len <= 0 || len >= (int)sizeof(buf)) return 0; - for (pos = 0; pos < len; ++pos) { - switch (val[pos]) { - case '+': case '-': case '0' ... '9': case '.': case 'e': case 'E': break; - default: return 0; - } - } - memcpy(buf, val, len); buf[len] = '\0'; @@ -466,8 +457,7 @@ SPA_API_JSON char *spa_json_format_float(char *str, int size, float val) /* int */ SPA_API_JSON int spa_json_parse_int(const char *val, int len, int *result) { - char buf[64]; - char *end; + char buf[64], *end; if (len <= 0 || len >= (int)sizeof(buf)) return 0; diff --git a/test/test-spa-json.c b/test/test-spa-json.c index 9e38dde18..907192844 100644 --- a/test/test-spa-json.c +++ b/test/test-spa-json.c @@ -717,11 +717,11 @@ PWTEST(json_float_check) { "0,0", 0, 0 }, { "0.0.5", 0, 0 }, - { "0x0", 0, 0 }, - { "0x0.0", 0, 0 }, + { "0x0", 1, 0 }, + { "0x0.0", 1, 0 }, { "E10", 0, 0 }, { "e20", 0, 0 }, - { " 0.0", 0, 0 }, + { " 0.0", 1, 0 }, { "0.0 ", 0, 0 }, { " 0.0 ", 0, 0 }, @@ -1002,6 +1002,12 @@ PWTEST(json_data) "n_number_-2..json", "n_number_hex_1_digit.json", "n_number_hex_2_digits.json", + "n_number_infinity.json", + "n_number_+Inf.json", + "n_number_Inf.json", + "n_number_minus_infinity.json", + "n_number_-NaN.json", + "n_number_NaN.json", "n_number_neg_int_starting_with_zero.json", "n_number_neg_real_without_int_part.json", "n_number_real_without_fractional_part.json",