|
|
594167 |
From fb195ccc27d1643d4152ee874144c36c0104c56d Mon Sep 17 00:00:00 2001
|
|
|
594167 |
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
|
|
|
594167 |
Date: Mon, 9 May 2022 15:10:36 +0200
|
|
|
594167 |
Subject: [PATCH] shared/json: add helper to ref first, unref second
|
|
|
594167 |
|
|
|
594167 |
This normally wouldn't happen, but if some of those places were called
|
|
|
594167 |
with lhs and rhs being the same object, we could unref the last ref first,
|
|
|
594167 |
and then try to take the ref again. It's easier to be safe, and with the
|
|
|
594167 |
helper we save some lines too.
|
|
|
594167 |
|
|
|
594167 |
(cherry picked from commit ce913e0ec4c97651c7c1509b72fb81ee61d80c6a)
|
|
|
594167 |
Related: #2087652
|
|
|
594167 |
---
|
|
|
594167 |
src/shared/json.c | 36 ++++++++++--------------------------
|
|
|
594167 |
src/shared/json.h | 8 ++++++++
|
|
|
594167 |
2 files changed, 18 insertions(+), 26 deletions(-)
|
|
|
594167 |
|
|
|
594167 |
diff --git a/src/shared/json.c b/src/shared/json.c
|
|
|
594167 |
index fe05657dad..bb2363fd98 100644
|
|
|
594167 |
--- a/src/shared/json.c
|
|
|
594167 |
+++ b/src/shared/json.c
|
|
|
594167 |
@@ -1847,9 +1847,7 @@ int json_variant_filter(JsonVariant **v, char **to_remove) {
|
|
|
594167 |
return r;
|
|
|
594167 |
|
|
|
594167 |
json_variant_propagate_sensitive(*v, w);
|
|
|
594167 |
-
|
|
|
594167 |
- json_variant_unref(*v);
|
|
|
594167 |
- *v = TAKE_PTR(w);
|
|
|
594167 |
+ JSON_VARIANT_REPLACE(*v, TAKE_PTR(w));
|
|
|
594167 |
|
|
|
594167 |
return (int) n;
|
|
|
594167 |
}
|
|
|
594167 |
@@ -1918,9 +1916,7 @@ int json_variant_set_field(JsonVariant **v, const char *field, JsonVariant *valu
|
|
|
594167 |
return r;
|
|
|
594167 |
|
|
|
594167 |
json_variant_propagate_sensitive(*v, w);
|
|
|
594167 |
-
|
|
|
594167 |
- json_variant_unref(*v);
|
|
|
594167 |
- *v = TAKE_PTR(w);
|
|
|
594167 |
+ JSON_VARIANT_REPLACE(*v, TAKE_PTR(w));
|
|
|
594167 |
|
|
|
594167 |
return 1;
|
|
|
594167 |
}
|
|
|
594167 |
@@ -2001,8 +1997,7 @@ int json_variant_merge(JsonVariant **v, JsonVariant *m) {
|
|
|
594167 |
return 0; /* nothing to do */
|
|
|
594167 |
|
|
|
594167 |
if (v_blank) {
|
|
|
594167 |
- json_variant_unref(*v);
|
|
|
594167 |
- *v = json_variant_ref(m);
|
|
|
594167 |
+ JSON_VARIANT_REPLACE(*v, json_variant_ref(m));
|
|
|
594167 |
return 1;
|
|
|
594167 |
}
|
|
|
594167 |
|
|
|
594167 |
@@ -2039,9 +2034,7 @@ int json_variant_merge(JsonVariant **v, JsonVariant *m) {
|
|
|
594167 |
|
|
|
594167 |
json_variant_propagate_sensitive(*v, w);
|
|
|
594167 |
json_variant_propagate_sensitive(m, w);
|
|
|
594167 |
-
|
|
|
594167 |
- json_variant_unref(*v);
|
|
|
594167 |
- *v = TAKE_PTR(w);
|
|
|
594167 |
+ JSON_VARIANT_REPLACE(*v, TAKE_PTR(w));
|
|
|
594167 |
|
|
|
594167 |
return 1;
|
|
|
594167 |
}
|
|
|
594167 |
@@ -2081,9 +2074,7 @@ int json_variant_append_array(JsonVariant **v, JsonVariant *element) {
|
|
|
594167 |
return r;
|
|
|
594167 |
|
|
|
594167 |
json_variant_propagate_sensitive(*v, nv);
|
|
|
594167 |
-
|
|
|
594167 |
- json_variant_unref(*v);
|
|
|
594167 |
- *v = TAKE_PTR(nv);
|
|
|
594167 |
+ JSON_VARIANT_REPLACE(*v, TAKE_PTR(nv));
|
|
|
594167 |
|
|
|
594167 |
return 0;
|
|
|
594167 |
}
|
|
|
594167 |
@@ -2297,8 +2288,7 @@ static int json_variant_set_source(JsonVariant **v, JsonSource *source, unsigned
|
|
|
594167 |
w->line = line;
|
|
|
594167 |
w->column = column;
|
|
|
594167 |
|
|
|
594167 |
- json_variant_unref(*v);
|
|
|
594167 |
- *v = w;
|
|
|
594167 |
+ JSON_VARIANT_REPLACE(*v, w);
|
|
|
594167 |
|
|
|
594167 |
return 1;
|
|
|
594167 |
}
|
|
|
594167 |
@@ -4499,14 +4489,10 @@ int json_dispatch_strv(const char *name, JsonVariant *variant, JsonDispatchFlags
|
|
|
594167 |
}
|
|
|
594167 |
|
|
|
594167 |
int json_dispatch_variant(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
|
|
|
594167 |
- JsonVariant **p = userdata;
|
|
|
594167 |
-
|
|
|
594167 |
+ JsonVariant **p = ASSERT_PTR(userdata);
|
|
|
594167 |
assert(variant);
|
|
|
594167 |
- assert(p);
|
|
|
594167 |
-
|
|
|
594167 |
- json_variant_unref(*p);
|
|
|
594167 |
- *p = json_variant_ref(variant);
|
|
|
594167 |
|
|
|
594167 |
+ JSON_VARIANT_REPLACE(*p, json_variant_ref(variant));
|
|
|
594167 |
return 0;
|
|
|
594167 |
}
|
|
|
594167 |
|
|
|
594167 |
@@ -4628,8 +4614,7 @@ int json_variant_sort(JsonVariant **v) {
|
|
|
594167 |
if (!n->sorted) /* Check if this worked. This will fail if there are multiple identical keys used. */
|
|
|
594167 |
return -ENOTUNIQ;
|
|
|
594167 |
|
|
|
594167 |
- json_variant_unref(*v);
|
|
|
594167 |
- *v = n;
|
|
|
594167 |
+ JSON_VARIANT_REPLACE(*v, n);
|
|
|
594167 |
|
|
|
594167 |
return 1;
|
|
|
594167 |
}
|
|
|
594167 |
@@ -4684,8 +4669,7 @@ int json_variant_normalize(JsonVariant **v) {
|
|
|
594167 |
goto finish;
|
|
|
594167 |
}
|
|
|
594167 |
|
|
|
594167 |
- json_variant_unref(*v);
|
|
|
594167 |
- *v = n;
|
|
|
594167 |
+ JSON_VARIANT_REPLACE(*v, n);
|
|
|
594167 |
|
|
|
594167 |
r = 1;
|
|
|
594167 |
|
|
|
594167 |
diff --git a/src/shared/json.h b/src/shared/json.h
|
|
|
594167 |
index 8760354b66..dd73c1e497 100644
|
|
|
594167 |
--- a/src/shared/json.h
|
|
|
594167 |
+++ b/src/shared/json.h
|
|
|
594167 |
@@ -82,6 +82,14 @@ JsonVariant *json_variant_ref(JsonVariant *v);
|
|
|
594167 |
JsonVariant *json_variant_unref(JsonVariant *v);
|
|
|
594167 |
void json_variant_unref_many(JsonVariant **array, size_t n);
|
|
|
594167 |
|
|
|
594167 |
+#define JSON_VARIANT_REPLACE(v, q) \
|
|
|
594167 |
+ do { \
|
|
|
594167 |
+ typeof(v)* _v = &(v); \
|
|
|
594167 |
+ typeof(q) _q = (q); \
|
|
|
594167 |
+ json_variant_unref(*_v); \
|
|
|
594167 |
+ *_v = _q; \
|
|
|
594167 |
+ } while(0)
|
|
|
594167 |
+
|
|
|
594167 |
DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant *, json_variant_unref);
|
|
|
594167 |
|
|
|
594167 |
const char *json_variant_string(JsonVariant *v);
|