Add json_object_object_add_by_uint_by_ref helper.

Function is similar to json_object_object_add_by_uint but
it unsets *jobj_val_ref pointer if the function ends with
success.

It helps to create cleaner error patch and avoids eventual
double free corruption if **jobj_val_ref object changed
ownership.
This commit is contained in:
Ondrej Kozina
2023-01-19 12:54:32 +01:00
parent b12e9534c3
commit 1c65c1c3d1
2 changed files with 15 additions and 0 deletions

View File

@@ -62,6 +62,7 @@ uint32_t crypt_jobj_get_uint32(json_object *jobj);
json_object *crypt_jobj_new_uint64(uint64_t value);
int json_object_object_add_by_uint(json_object *jobj, unsigned key, json_object *jobj_val);
int json_object_object_add_by_uint_by_ref(json_object *jobj, unsigned key, json_object **jobj_val_ref);
void json_object_object_del_by_uint(json_object *jobj, unsigned key);
int json_object_copy(json_object *jobj_src, json_object **jobj_dst);

View File

@@ -2867,6 +2867,20 @@ int json_object_object_add_by_uint(json_object *jobj, unsigned key, json_object
#endif
}
int json_object_object_add_by_uint_by_ref(json_object *jobj, unsigned key, json_object **jobj_val_ref)
{
int r;
assert(jobj);
assert(jobj_val_ref);
r = json_object_object_add_by_uint(jobj, key, *jobj_val_ref);
if (!r)
*jobj_val_ref = NULL;
return r;
}
/* jobj_dst must contain pointer initialized to NULL (see json-c json_object_deep_copy API) */
int json_object_copy(json_object *jobj_src, json_object **jobj_dst)
{