general callback, safer API & related tests

- Changed json_pointer_set_with_array_cb to json_pointer_set_with_cb, related cb updated
- Added tests(test_safe_json_pointer_set.*) for new-exported funcs, also updated cmake & meson

Signed-off-by: lone <lonechan314@qq.com>
This commit is contained in:
lone
2026-02-09 14:01:38 +08:00
parent bda8299eb1
commit c293d9b61c
10 changed files with 387 additions and 63 deletions

View File

@@ -108,8 +108,9 @@ static int json_patch_apply_remove(struct json_object **res, const char *path, s
return rc;
}
// callback for json_pointer_set_with_array_cb()
static int json_object_array_insert_idx_cb(struct json_object *parent, size_t idx,
// callback for json_pointer_set_with_cb()
// key is ignored because this callback is only for arrays
static int json_object_array_insert_idx_cb(struct json_object *parent, const char *key, size_t idx,
struct json_object *value, void *priv)
{
int rc;
@@ -117,7 +118,7 @@ static int json_object_array_insert_idx_cb(struct json_object *parent, size_t id
if (idx > json_object_array_length(parent))
{
// Note: will propagate back out through json_pointer_set_with_array_cb()
// Note: will propagate back out through json_pointer_set_with_cb()
errno = EINVAL;
return -1;
}
@@ -148,8 +149,8 @@ static int json_patch_apply_add_replace(struct json_object **res,
return -1;
}
rc = json_pointer_set_with_array_cb(res, path, json_object_get(value),
json_object_array_insert_idx_cb, &add);
rc = json_pointer_set_with_cb(res, path, json_object_get(value),
json_object_array_insert_idx_cb, 0, &add);
if (rc)
{
_set_err(errno, "Failed to set value at path referenced by 'path' field");
@@ -159,8 +160,9 @@ static int json_patch_apply_add_replace(struct json_object **res,
return rc;
}
// callback for json_pointer_set_with_array_cb()
static int json_object_array_move_cb(struct json_object *parent, size_t idx,
// callback for json_pointer_set_with_cb()
// key is ignored because this callback is only for arrays
static int json_object_array_move_cb(struct json_object *parent, const char *key, size_t idx,
struct json_object *value, void *priv)
{
int rc;
@@ -178,7 +180,7 @@ static int json_object_array_move_cb(struct json_object *parent, size_t idx,
if (idx > len)
{
// Note: will propagate back out through json_pointer_set_with_array_cb()
// Note: will propagate back out through json_pointer_set_with_cb()
errno = EINVAL;
return -1;
}
@@ -193,7 +195,7 @@ static int json_patch_apply_move_copy(struct json_object **res,
struct json_object *patch_elem,
const char *path, int move, struct json_patch_error *patch_error)
{
json_pointer_array_set_cb array_set_cb;
json_pointer_set_cb array_set_cb;
struct json_pointer_get_result from;
struct json_object *jfrom;
const char *from_s;
@@ -244,7 +246,7 @@ static int json_patch_apply_move_copy(struct json_object **res,
array_set_cb = json_object_array_move_cb;
}
rc = json_pointer_set_with_array_cb(res, path, from.obj, array_set_cb, &from);
rc = json_pointer_set_with_cb(res, path, from.obj, array_set_cb, 0, &from);
if (rc)
{
_set_err(errno, "Failed to set value at path referenced by 'path' field");