3
0
mirror of https://git.kernel.org/pub/scm/network/wireless/iwd.git synced 2024-12-22 13:02:44 +01:00

json: Tighten up json_iter_get_int/get_uint

- Ensure that input isn't an empty string

- Ensure that EINVAL errno (which could be optionally returned by
  strto{ul|l} is also checked.

- Since strtoul allows '+' and '-' characters in input, ensure that
  input which is expected to be an unsigned number doesn't start with
  '-'
This commit is contained in:
Denis Kenzior 2022-01-23 11:18:29 -06:00
parent 26b7446178
commit 30b04bc3bb

View File

@ -309,13 +309,16 @@ bool json_iter_get_int(struct json_iter *iter, int *i)
if (!iter_get_primitive_data(iter, &ptr, &len))
return false;
if (!len)
return false;
errno = 0;
t = r = strtol(ptr, &endp, 10);
if (endp != ptr + len)
return false;
if (errno == ERANGE || r != t)
if (errno == ERANGE || errno == EINVAL || r != t)
return false;
if (i)
@ -335,13 +338,16 @@ bool json_iter_get_uint(struct json_iter *iter, unsigned int *i)
if (!iter_get_primitive_data(iter, &ptr, &len))
return false;
if (!len || *((char *) ptr) == '-')
return false;
errno = 0;
t = r = strtoul(ptr, &endp, 10);
if (endp != ptr + len)
return false;
if (errno == ERANGE || r != t)
if (errno == ERANGE || errno == EINVAL || r != t)
return false;
if (i)