}
}
-static int is_valid_index(struct json_object *jo, const char *path, size_t *idx)
+static int is_valid_index(const char *path, size_t *idx)
{
size_t i, len = strlen(path);
/* this code-path optimizes a bit, for when we reference the 0-9 index range
if (is_plain_digit(path[0]))
{
*idx = (path[0] - '0');
- goto check_oob;
+ return 1;
}
errno = EINVAL;
return 0;
// 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)
- {
- errno = ENOENT;
- return 0;
- }
return 1;
}
{
if (json_object_is_type(obj, json_type_array))
{
- if (!is_valid_index(obj, path, idx))
+ if (!is_valid_index(path, idx))
return -1;
+ if (*idx >= json_object_array_length(obj))
+ {
+ errno = ENOENT;
+ return -1;
+ }
+
obj = json_object_array_get_idx(obj, *idx);
if (obj)
{
/* 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);
- if (!is_valid_index(parent, path, &idx))
+ if (!is_valid_index(path, &idx))
return -1;
return json_object_array_put_idx(parent, idx, value);
}
printf("%s\n", json_object_get_string(jo1));
json_object_put(jo1);
+
+ jo1 = json_tokener_parse("[0, 1, 2, 3]");
+ jo2 = json_tokener_parse("[0, 1, 2, 3, null, null, null, 7]");
+
+ assert(0 == json_pointer_set(&jo1, "/7", json_object_new_int(7)));
+ assert(1 == json_object_equal(jo1, jo2));
+
+ json_object_put(jo1);
+
+ jo1 = json_tokener_parse("[0, 1, 2, 3]");
+
+ assert(0 == json_pointer_setf(&jo1, json_object_new_int(7), "/%u", 7));
+ assert(1 == json_object_equal(jo1, jo2));
+
+ json_object_put(jo1);
+ json_object_put(jo2);
}
static void test_wrong_inputs_set(void)