Apply some of the fixes from PR #740, although by using size_t instead of castings.

This commit is contained in:
Eric Haszlakiewicz
2022-07-31 19:26:12 +00:00
parent 4b0c6de760
commit bdd5e03d6e
4 changed files with 33 additions and 28 deletions

View File

@@ -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);