diff options
author | Eric Haszlakiewicz <erh+git@nimenees.com> | 2022-07-31 19:26:12 +0000 |
---|---|---|
committer | Eric Haszlakiewicz <erh+git@nimenees.com> | 2022-07-31 19:28:48 +0000 |
commit | bdd5e03d6ed19b165bfe5e9309f8a596dbda45f4 (patch) | |
tree | 4c1a87a5b87f3efff8d5ba6fb612ac791a6a9779 /json_pointer.c | |
parent | 4b0c6de760f0ab0655e846cf9ab7073861541407 (diff) | |
download | json-c-bdd5e03d6ed19b165bfe5e9309f8a596dbda45f4.tar.gz |
Apply some of the fixes from PR #740, although by using size_t instead of castings.
Diffstat (limited to 'json_pointer.c')
-rw-r--r-- | json_pointer.c | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/json_pointer.c b/json_pointer.c index 395567a..2e8d30c 100644 --- a/json_pointer.c +++ b/json_pointer.c @@ -29,8 +29,8 @@ static void string_replace_all_occurrences_with_char(char *s, const char *occur, char repl_char) { - int slen = strlen(s); - int skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */ + size_t slen = strlen(s); + size_t skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */ char *p = s; while ((p = strstr(p, occur))) { @@ -41,9 +41,9 @@ 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, int32_t *idx) +static int is_valid_index(struct json_object *jo, const char *path, size_t *idx) { - int i, len = strlen(path); + size_t i, len = strlen(path); /* 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 */ @@ -73,12 +73,14 @@ static int is_valid_index(struct json_object *jo, const char *path, int32_t *idx } } - *idx = strtol(path, NULL, 10); - if (*idx < 0) + long int idx_val = strtol(path, NULL, 10); + if (idx_val < 0) { errno = EINVAL; return 0; } + *idx = idx_val; + check_oob: len = json_object_array_length(jo); if (*idx >= len) @@ -95,7 +97,7 @@ static int json_pointer_get_single_path(struct json_object *obj, char *path, { if (json_object_is_type(obj, json_type_array)) { - int32_t idx; + size_t idx; if (!is_valid_index(obj, path, &idx)) return -1; obj = json_object_array_get_idx(obj, idx); @@ -128,7 +130,7 @@ static int json_pointer_set_single_path(struct json_object *parent, const char * { if (json_object_is_type(parent, json_type_array)) { - int32_t idx; + size_t idx; /* RFC (Chapter 4) states that '-' may be used to add new elements to an array */ if (path[0] == '-' && path[1] == '\0') return json_object_array_add(parent, value); |