]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
json_pointer: convert index to size_t type
authorAlexandru Ardelean <ardeleanalex@gmail.com>
Sat, 24 Apr 2021 14:06:17 +0000 (17:06 +0300)
committerEric Hawicz <erh+git@nimenees.com>
Tue, 1 Aug 2023 02:17:30 +0000 (22:17 -0400)
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>
json_pointer.c

index 5abccdb9dfc1fb69ccd07643f5375304fa0ccce2..15f502619715023932573600ccd9e240aa0d15ea 100644 (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)