Browse Source

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>
tags/json-c-0.17-20230812
Alexandru Ardelean Eric Hawicz 4 years ago
parent
commit
43d3118935
1 changed files with 3 additions and 9 deletions
  1. +3
    -9
      json_pointer.c

+ 3
- 9
json_pointer.c View File

@@ -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)
{
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
* 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);
if (idx_val < 0)
{
errno = EINVAL;
return 0;
}
*idx = idx_val;

// We know it's all digits, so the only error case here is overflow,
// but ULLONG_MAX will be longer than any array length so that's ok.
*idx = strtoull(path, NULL, 10);
check_oob:
len = json_object_array_length(jo);
if (*idx >= len)


Loading…
Cancel
Save