json_pointer: convert index to size_t type
The index cannot be negative when parsing in is_valid_index(), because we don't allow the '-' character in a string before we get to the strtol() function. So, might as well remove the negative check (for idx) in is_valid_index() and convert it to size_t. That may allow for higher values for the index (which can be insane, but some people may want to try it). Signed-off-by: Alexandru Ardelean <ardeleanalex@gmail.com>
This commit is contained in:
committed by
Eric Hawicz
parent
d5c5b2caec
commit
43d3118935
@@ -44,7 +44,6 @@ static void string_replace_all_occurrences_with_char(char *s, const char *occur,
|
|||||||
static int is_valid_index(struct json_object *jo, const char *path, size_t *idx)
|
static int is_valid_index(struct json_object *jo, const char *path, size_t *idx)
|
||||||
{
|
{
|
||||||
size_t i, len = strlen(path);
|
size_t i, len = strlen(path);
|
||||||
long int idx_val = -1;
|
|
||||||
/* this code-path optimizes a bit, for when we reference the 0-9 index range
|
/* this code-path optimizes a bit, for when we reference the 0-9 index range
|
||||||
* in a JSON array and because leading zeros not allowed
|
* in a JSON array and because leading zeros not allowed
|
||||||
*/
|
*/
|
||||||
@@ -74,14 +73,9 @@ static int is_valid_index(struct json_object *jo, const char *path, size_t *idx)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
idx_val = strtol(path, NULL, 10);
|
// We know it's all digits, so the only error case here is overflow,
|
||||||
if (idx_val < 0)
|
// but ULLONG_MAX will be longer than any array length so that's ok.
|
||||||
{
|
*idx = strtoull(path, NULL, 10);
|
||||||
errno = EINVAL;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
*idx = idx_val;
|
|
||||||
|
|
||||||
check_oob:
|
check_oob:
|
||||||
len = json_object_array_length(jo);
|
len = json_object_array_length(jo);
|
||||||
if (*idx >= len)
|
if (*idx >= len)
|
||||||
|
|||||||
Reference in New Issue
Block a user