Add json_variant_unref_and_replace and use it where appropriate

JSON_VARIANT_REPLACE is similar, but doesn't try to nullify the second
arg, so it can be used with an inline expression.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-05-08 11:36:43 +02:00
parent f3ee3510a3
commit 88d7e44392
9 changed files with 30 additions and 61 deletions
+2 -6
View File
@@ -1038,8 +1038,7 @@ static int apply_identity_changes(sd_json_variant **_v) {
if (r < 0)
return log_error_errno(r, "Failed to allocate new perMachine array: %m");
sd_json_variant_unref(per_machine);
per_machine = TAKE_PTR(npm);
json_variant_unref_and_replace(per_machine, npm);
} else {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *positive = sd_json_variant_ref(arg_identity_extra_this_machine),
*negative = sd_json_variant_ref(arg_identity_extra_other_machines);
@@ -1122,10 +1121,7 @@ static int apply_identity_changes(sd_json_variant **_v) {
}
}
sd_json_variant_unref(*_v);
*_v = TAKE_PTR(v);
return 0;
return json_variant_unref_and_replace(*_v, v);
}
static int add_disposition(sd_json_variant **v) {
+6 -26
View File
@@ -376,8 +376,7 @@ int user_record_add_binding(
if (r < 0)
return r;
sd_json_variant_unref(new_binding_entry);
new_binding_entry = TAKE_PTR(be);
json_variant_unref_and_replace(new_binding_entry, be);
}
}
@@ -787,12 +786,8 @@ int user_record_update_last_changed(UserRecord *h, bool with_password) {
}
h->last_change_usec = n;
sd_json_variant_unref(h->json);
h->json = TAKE_PTR(v);
h->mask |= USER_RECORD_REGULAR;
return 0;
return json_variant_unref_and_replace(h->json, v);
}
int user_record_make_hashed_password(UserRecord *h, char **secret, bool extend) {
@@ -1131,12 +1126,8 @@ int user_record_set_password_change_now(UserRecord *h, int b) {
SET_FLAG(h->mask, USER_RECORD_PER_MACHINE, !sd_json_variant_is_blank_array(array));
}
sd_json_variant_unref(h->json);
h->json = TAKE_PTR(w);
h->password_change_now = b;
return 0;
return json_variant_unref_and_replace(h->json, w);
}
int user_record_merge_secret(UserRecord *h, UserRecord *secret) {
@@ -1227,14 +1218,10 @@ int user_record_good_authentication(UserRecord *h) {
if (r < 0)
return r;
sd_json_variant_unref(h->json);
h->json = TAKE_PTR(v);
h->good_authentication_counter = counter;
h->last_good_authentication_usec = usec;
h->mask |= USER_RECORD_STATUS;
return 0;
return json_variant_unref_and_replace(h->json, v);
}
int user_record_bad_authentication(UserRecord *h) {
@@ -1282,14 +1269,10 @@ int user_record_bad_authentication(UserRecord *h) {
if (r < 0)
return r;
sd_json_variant_unref(h->json);
h->json = TAKE_PTR(v);
h->bad_authentication_counter = counter;
h->last_bad_authentication_usec = usec;
h->mask |= USER_RECORD_STATUS;
return 0;
return json_variant_unref_and_replace(h->json, v);
}
int user_record_ratelimit(UserRecord *h) {
@@ -1344,13 +1327,10 @@ int user_record_ratelimit(UserRecord *h) {
if (r < 0)
return r;
sd_json_variant_unref(h->json);
h->json = TAKE_PTR(v);
h->ratelimit_begin_usec = new_ratelimit_begin_usec;
h->ratelimit_count = new_ratelimit_count;
h->mask |= USER_RECORD_STATUS;
json_variant_unref_and_replace(h->json, v);
return 1;
}
+8 -5
View File
@@ -9,14 +9,17 @@
#include "sd-forward.h"
#include "string-util.h" /* IWYU pragma: keep */
#define JSON_VARIANT_REPLACE(v, q) \
do { \
typeof(v)* _v = &(v); \
typeof(q) _q = (q); \
#define JSON_VARIANT_REPLACE(v, q) \
do { \
typeof(v)* _v = &(v); \
typeof(q) _q = (q); \
sd_json_variant_unref(*_v); \
*_v = _q; \
*_v = _q; \
} while(false)
#define json_variant_unref_and_replace(a, b) \
free_and_replace_full(a, b, sd_json_variant_unref)
static inline int json_variant_set_field_non_null(sd_json_variant **v, const char *field, sd_json_variant *value) {
return value && !sd_json_variant_is_null(value) ? sd_json_variant_set_field(v, field, value) : 0;
}
+7 -9
View File
@@ -1974,7 +1974,7 @@ _public_ int sd_json_variant_filter(sd_json_variant **v, char **to_remove) {
return r;
json_variant_propagate_sensitive(*v, w);
JSON_VARIANT_REPLACE(*v, TAKE_PTR(w));
json_variant_unref_and_replace(*v, w);
return (int) n;
}
@@ -2043,7 +2043,7 @@ _public_ int sd_json_variant_set_field(sd_json_variant **v, const char *field, s
return r;
json_variant_propagate_sensitive(*v, w);
JSON_VARIANT_REPLACE(*v, TAKE_PTR(w));
json_variant_unref_and_replace(*v, w);
return 1;
}
@@ -2183,7 +2183,7 @@ _public_ int sd_json_variant_merge_object(sd_json_variant **v, sd_json_variant *
json_variant_propagate_sensitive(*v, w);
json_variant_propagate_sensitive(m, w);
JSON_VARIANT_REPLACE(*v, TAKE_PTR(w));
json_variant_unref_and_replace(*v, w);
return 1;
}
@@ -2262,9 +2262,7 @@ _public_ int sd_json_variant_append_array(sd_json_variant **v, sd_json_variant *
}
json_variant_propagate_sensitive(*v, nv);
JSON_VARIANT_REPLACE(*v, TAKE_PTR(nv));
return 0;
return json_variant_unref_and_replace(*v, nv);
}
_public_ int sd_json_variant_append_arrayb(sd_json_variant **v, ...) {
@@ -2511,7 +2509,7 @@ static int json_variant_set_source(sd_json_variant **v, JsonSource *source, unsi
w->line = line;
w->column = column;
JSON_VARIANT_REPLACE(*v, w);
json_variant_unref_and_replace(*v, w);
return 1;
}
@@ -5855,7 +5853,7 @@ _public_ int sd_json_variant_sort(sd_json_variant **v) {
if (!n->sorted) /* Check if this worked. This will fail if there are multiple identical keys used. */
return -ENOTUNIQ;
JSON_VARIANT_REPLACE(*v, TAKE_PTR(n));
json_variant_unref_and_replace(*v, n);
return 1;
}
@@ -5910,7 +5908,7 @@ _public_ int sd_json_variant_normalize(sd_json_variant **v) {
goto finish;
}
JSON_VARIANT_REPLACE(*v, TAKE_PTR(n));
json_variant_unref_and_replace(*v, n);
r = 1;
+1 -2
View File
@@ -548,8 +548,7 @@ TEST(depth) {
assert_se(r >= 0);
sd_json_variant_unref(v);
v = TAKE_PTR(w);
json_variant_unref_and_replace(v, w);
}
sd_json_variant_dump(v, 0, stdout, NULL);
+1 -4
View File
@@ -772,10 +772,7 @@ static int varlink_sanitize_incoming_parameters(sd_json_variant **v) {
r = sd_json_variant_new_object(&empty, NULL, 0);
if (r < 0)
return r;
/* sd_json_variant_unref() is a NOP if *v is NULL */
sd_json_variant_unref(*v);
*v = TAKE_PTR(empty);
return 0;
return json_variant_unref_and_replace(*v, empty);
}
/* Ensure we have an object */
+2 -4
View File
@@ -1251,8 +1251,7 @@ static int status_json_filter_links(sd_json_variant **configuration, char **link
return r;
}
JSON_VARIANT_REPLACE(*configuration, TAKE_PTR(v));
return 0;
return json_variant_unref_and_replace(*configuration, v);
}
static int status_json_filter_fields(sd_json_variant **configuration, StatusMode mode) {
@@ -1281,8 +1280,7 @@ static int status_json_filter_fields(sd_json_variant **configuration, StatusMode
return r;
}
JSON_VARIANT_REPLACE(*configuration, TAKE_PTR(v));
return 0;
return json_variant_unref_and_replace(*configuration, v);
}
static int format_dns_server_one(DNSConfiguration *configuration, DNSServer *s, char **ret) {
+1 -1
View File
@@ -2335,7 +2335,7 @@ int manager_send_dns_configuration_changed(Manager *m, Link *l, bool reset) {
if (sd_json_variant_equal(configuration, m->dns_configuration_json))
return 0;
JSON_VARIANT_REPLACE(m->dns_configuration_json, TAKE_PTR(configuration));
json_variant_unref_and_replace(m->dns_configuration_json, configuration);
r = varlink_many_notify(m->varlink_dns_configuration_subscription, m->dns_configuration_json);
if (r < 0)
+2 -4
View File
@@ -2374,8 +2374,7 @@ static int remove_self_modifiable_json_fields_common(UserRecord *current, sd_jso
return r;
}
JSON_VARIANT_REPLACE(*target, TAKE_PTR(v));
return 0;
return json_variant_unref_and_replace(*target, v);
}
static int remove_self_modifiable_json_fields(UserRecord *current, UserRecord *h, sd_json_variant **ret) {
@@ -2460,8 +2459,7 @@ static int remove_self_modifiable_json_fields(UserRecord *current, UserRecord *h
return r;
}
JSON_VARIANT_REPLACE(*ret, TAKE_PTR(v));
return 0;
return json_variant_unref_and_replace(*ret, v);
}
int user_record_self_changes_allowed(UserRecord *current, UserRecord *incoming) {