summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/import/pull-dkr.c5
-rw-r--r--src/libsystemd-network/sd-dhcp-lease.c2
-rw-r--r--src/shared/json.c33
-rw-r--r--src/shared/json.h22
-rw-r--r--src/test/test-json.c5
5 files changed, 39 insertions, 28 deletions
diff --git a/src/import/pull-dkr.c b/src/import/pull-dkr.c
index dd2097bb1d..40aca786a5 100644
--- a/src/import/pull-dkr.c
+++ b/src/import/pull-dkr.c
@@ -794,7 +794,7 @@ static void dkr_pull_job_on_finished_v2(PullJob *j) {
} else if (i->tags_job == j) {
const char *url;
_cleanup_free_ const char *buf;
- _cleanup_jsonunref_ JsonVariant *doc = NULL;
+ _cleanup_json_variant_unref_ JsonVariant *doc = NULL;
JsonVariant *e = NULL;
assert(!i->ancestry_job);
@@ -851,8 +851,7 @@ static void dkr_pull_job_on_finished_v2(PullJob *j) {
} else if (i->ancestry_job == j) {
- _cleanup_jsonunref_ JsonVariant *doc = NULL;
- _cleanup_jsonunref_ JsonVariant *compat = NULL;
+ _cleanup_json_variant_unref_ JsonVariant *doc = NULL, *compat = NULL;
JsonVariant *e = NULL;
_cleanup_strv_free_ char **ancestry = NULL;
size_t allocated = 0, size = 0;
diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c
index 4b8763aab0..8a4220621b 100644
--- a/src/libsystemd-network/sd-dhcp-lease.c
+++ b/src/libsystemd-network/sd-dhcp-lease.c
@@ -672,7 +672,7 @@ int sd_dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
if (r >= 0) {
_cleanup_free_ char *client_id_hex;
- client_id_hex = hexmem (client_id, client_id_len);
+ client_id_hex = hexmem(client_id, client_id_len);
if (!client_id_hex) {
r = -ENOMEM;
goto finish;
diff --git a/src/shared/json.c b/src/shared/json.c
index 69514160c2..be40a0d203 100644
--- a/src/shared/json.c
+++ b/src/shared/json.c
@@ -27,6 +27,7 @@
int json_variant_new(JsonVariant **ret, JsonVariantType type) {
JsonVariant *v;
+
v = new0(JsonVariant, 1);
if (!v)
return -ENOMEM;
@@ -36,6 +37,8 @@ int json_variant_new(JsonVariant **ret, JsonVariantType type) {
}
static int json_variant_deep_copy(JsonVariant *ret, JsonVariant *variant) {
+ int r;
+
assert(ret);
assert(variant);
@@ -47,18 +50,18 @@ static int json_variant_deep_copy(JsonVariant *ret, JsonVariant *variant) {
if (!ret->string)
return -ENOMEM;
} else if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT) {
+ size_t i;
+
ret->objects = new0(JsonVariant, variant->size);
if (!ret->objects)
return -ENOMEM;
- for (unsigned i = 0; i < variant->size; ++i) {
- int r;
+ for (i = 0; i < variant->size; ++i) {
r = json_variant_deep_copy(&ret->objects[i], &variant->objects[i]);
if (r < 0)
return r;
}
- }
- else
+ } else
ret->value = variant->value;
return 0;
@@ -72,7 +75,6 @@ static JsonVariant *json_variant_unref_inner(JsonVariant *variant) {
if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT)
return json_object_unref(variant);
-
else if (variant->type == JSON_VARIANT_STRING)
free(variant->string);
@@ -91,11 +93,14 @@ static JsonVariant *json_raw_unref(JsonVariant *variant, size_t size) {
}
static JsonVariant *json_object_unref(JsonVariant *variant) {
+ size_t i;
+
assert(variant);
+
if (!variant->objects)
return NULL;
- for (unsigned i = 0; i < variant->size; ++i)
+ for (i = 0; i < variant->size; ++i)
json_variant_unref_inner(&variant->objects[i]);
free(variant->objects);
@@ -119,6 +124,7 @@ static JsonVariant **json_variant_array_unref(JsonVariant **variant) {
return NULL;
}
+
DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant **, json_variant_array_unref);
JsonVariant *json_variant_unref(JsonVariant *variant) {
@@ -127,7 +133,6 @@ JsonVariant *json_variant_unref(JsonVariant *variant) {
if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT)
json_object_unref(variant);
-
else if (variant->type == JSON_VARIANT_STRING)
free(variant->string);
@@ -174,11 +179,13 @@ JsonVariant *json_variant_element(JsonVariant *variant, unsigned index) {
}
JsonVariant *json_variant_value(JsonVariant *variant, const char *key) {
+ size_t i;
+
assert(variant);
assert(variant->type == JSON_VARIANT_OBJECT);
assert(variant->objects);
- for (unsigned i = 0; i < variant->size; i += 2) {
+ for (i = 0; i < variant->size; i += 2) {
JsonVariant *p = &variant->objects[i];
if (p->type == JSON_VARIANT_STRING && streq(key, p->string))
return &variant->objects[i + 1];
@@ -624,9 +631,11 @@ static int json_scoped_parse(JsonVariant **tokens, size_t *i, size_t n, JsonVari
assert(scope);
while((var = *i < n ? tokens[(*i)++] : NULL) != NULL) {
- bool stopper = !json_is_value(var) && var->value.integer == terminator;
+ bool stopper;
int r;
+ stopper = !json_is_value(var) && var->value.integer == terminator;
+
if (stopper) {
if (state != STATE_COMMA && size > 0)
goto error;
@@ -655,7 +664,7 @@ static int json_scoped_parse(JsonVariant **tokens, size_t *i, size_t n, JsonVari
state = STATE_VALUE;
}
else if (state == STATE_VALUE) {
- _cleanup_jsonunref_ JsonVariant *v = NULL;
+ _cleanup_json_variant_unref_ JsonVariant *v = NULL;
size_t toadd = arr ? 1 : 2;
if (!json_is_value(var)) {
@@ -723,7 +732,7 @@ static int json_parse_tokens(JsonVariant **tokens, size_t ntokens, JsonVariant *
size_t it = 0;
int r;
JsonVariant *e;
- _cleanup_jsonunref_ JsonVariant *p = NULL;
+ _cleanup_json_variant_unref_ JsonVariant *p = NULL;
assert(tokens);
assert(ntokens);
@@ -767,8 +776,8 @@ static int json_tokens(const char *string, size_t size, JsonVariant ***tokens, s
p = buf;
for (;;) {
+ _cleanup_json_variant_unref_ JsonVariant *var = NULL;
_cleanup_free_ char *rstr = NULL;
- _cleanup_jsonunref_ JsonVariant *var = NULL;
t = json_tokenize(&p, &rstr, &v, &json_state, NULL);
diff --git a/src/shared/json.h b/src/shared/json.h
index 2ce56420a3..e0b4d810b5 100644
--- a/src/shared/json.h
+++ b/src/shared/json.h
@@ -57,30 +57,32 @@ union json_value {
};
typedef struct JsonVariant {
+ JsonVariantType type;
+ size_t size;
union {
char *string;
struct JsonVariant *objects;
union json_value value;
};
- JsonVariantType type;
- unsigned size;
} JsonVariant;
int json_variant_new(JsonVariant **ret, JsonVariantType type);
-JsonVariant *json_variant_unref(JsonVariant *);
+JsonVariant *json_variant_unref(JsonVariant *v);
+
DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant *, json_variant_unref);
-#define _cleanup_jsonunref_ _cleanup_(json_variant_unrefp)
+#define _cleanup_json_variant_unref_ _cleanup_(json_variant_unrefp)
-char *json_variant_string(JsonVariant *);
-bool json_variant_bool(JsonVariant *);
-intmax_t json_variant_integer(JsonVariant *);
-double json_variant_real(JsonVariant *);
+char *json_variant_string(JsonVariant *v);
+bool json_variant_bool(JsonVariant *v);
+intmax_t json_variant_integer(JsonVariant *v);
+double json_variant_real(JsonVariant *v);
-JsonVariant *json_variant_element(JsonVariant *, unsigned index);
-JsonVariant *json_variant_value(JsonVariant *, const char *key);
+JsonVariant *json_variant_element(JsonVariant *v, unsigned index);
+JsonVariant *json_variant_value(JsonVariant *v, const char *key);
#define JSON_VALUE_NULL ((union json_value) {})
int json_tokenize(const char **p, char **ret_string, union json_value *ret_value, void **state, unsigned *line);
+
int json_parse(const char *string, JsonVariant **rv);
int json_parse_measure(const char *string, size_t *size);
diff --git a/src/test/test-json.c b/src/test/test-json.c
index 1e3a72729f..1058c583c3 100644
--- a/src/test/test-json.c
+++ b/src/test/test-json.c
@@ -75,9 +75,10 @@ static void test_one(const char *data, ...) {
typedef void (*Test)(JsonVariant *);
static void test_file(const char *data, Test test) {
- _cleanup_jsonunref_ JsonVariant *v = NULL;
- int r = json_parse(data, &v);
+ _cleanup_json_variant_unref_ JsonVariant *v = NULL;
+ int r;
+ r = json_parse(data, &v);
assert_se(r == 0);
assert_se(v != NULL);
assert_se(v->type == JSON_VARIANT_OBJECT);