json: avoid converting random string to floats

Only allow the json float characters when converting to float.
This avoids NAN, INF, 0xffe4 etc to be seen as a json float.
This commit is contained in:
Wim Taymans 2022-07-11 11:50:58 +02:00
parent bdfde2fdf0
commit cbbc4baa3f
2 changed files with 3 additions and 1 deletions

View file

@ -240,6 +240,8 @@ static inline bool spa_json_is_null(const char *val, int len)
static inline int spa_json_parse_float(const char *val, int len, float *result)
{
char *end;
if (strspn(val, "+-0123456789.Ee") < (size_t)len)
return 0;
*result = spa_strtof(val, &end);
return len > 0 && end == val + len;
}

View file

@ -86,7 +86,7 @@ static void expect_float(struct spa_json *it, float val)
{
const char *value;
int len;
float f;
float f = 0.0f;
pwtest_int_gt((len = spa_json_next(it, &value)), 0);
check_type(TYPE_FLOAT, value, len);
pwtest_int_gt(spa_json_parse_float(value, len, &f), 0);