json-builder: do better json number check

If we are going to produce valid JSON we need to do a better JSON number
check because our own float and int parser can accept more variants
and will let through numbers that are not valid JSON.

See #5161
This commit is contained in:
Wim Taymans 2026-03-09 13:33:20 +01:00
parent 48c22e2aa7
commit 3a2d16a3bc
3 changed files with 77 additions and 16 deletions

View file

@ -708,21 +708,39 @@ PWTEST(json_float_check)
struct {
const char *str;
int res;
int jsonres;
} val[] = {
{ "0.0", 1 },
{ ".0", 1 },
{ "+.0E0", 1 },
{ "-.0e0", 1 },
{ "0.0", 1, 1},
{ ".0", 1, 0 },
{ "+.0E0", 1, 0 },
{ "-.0e0", 1, 0 },
{ "0,0", 0, 0 },
{ "0.0.5", 0, 0 },
{ "0x0", 0, 0 },
{ "0x0.0", 0, 0 },
{ "E10", 0, 0 },
{ "e20", 0, 0 },
{ " 0.0", 0, 0 },
{ "0.0 ", 0, 0 },
{ " 0.0 ", 0, 0 },
{ "+", 0, 0 },
{ "+0", 1, 0 },
{ "-", 0, 0 },
{ "-0", 1, 1 },
{ "-0", 1, 1 },
{ "-01", 1, 0 },
{ "-00", 1, 0 },
{ "-1", 1, 1 },
{ "-10", 1, 1 },
{ "-.", 0, 0 },
{ "-0.", 1, 0 },
{ "-01.", 1, 0 },
{ "-1.", 1, 0 },
{ "-.0", 1, 0 },
{ "0,0", 0 },
{ "0.0.5", 0 },
{ "0x0", 0 },
{ "0x0.0", 0 },
{ "E10", 0 },
{ "e20", 0 },
{ " 0.0", 0 },
{ "0.0 ", 0 },
{ " 0.0 ", 0 },
};
unsigned i;
float v;
@ -730,6 +748,9 @@ PWTEST(json_float_check)
for (i = 0; i < SPA_N_ELEMENTS(val); i++) {
pwtest_int_eq(spa_json_parse_float(val[i].str, strlen(val[i].str), &v), val[i].res);
}
for (i = 0; i < SPA_N_ELEMENTS(val); i++) {
pwtest_int_eq(spa_json_is_json_number(val[i].str, strlen(val[i].str)), val[i].jsonres);
}
return PWTEST_PASS;
}