1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-11 09:18:07 +03:00

json: don't assert() if we add a NULL element via json_variant_set_field()

The rest of our JSON code tries hard to magically convert NULL inputs
into "null" JSON objects, let's make sure this also works with
json_variant_set_field().
This commit is contained in:
Lennart Poettering 2021-11-25 10:30:45 +01:00
parent fb0321029c
commit e2c7efd329
2 changed files with 24 additions and 1 deletions

View File

@ -515,7 +515,6 @@ static void json_variant_set(JsonVariant *a, JsonVariant *b) {
static void json_variant_copy_source(JsonVariant *v, JsonVariant *from) {
assert(v);
assert(from);
if (!json_variant_is_regular(from))
return;

View File

@ -569,6 +569,29 @@ static void test_float(void) {
test_float_match(w);
}
static void test_equal_text(JsonVariant *v, const char *text) {
_cleanup_(json_variant_unrefp) JsonVariant *w = NULL;
assert_se(json_parse(text, 0, &w, NULL, NULL) >= 0);
assert_se(json_variant_equal(v, w) || (!v && json_variant_is_null(w)));
}
static void test_set_field(void) {
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
log_info("/* %s */", __func__);
test_equal_text(v, "null");
assert_se(json_variant_set_field(&v, "foo", NULL) >= 0);
test_equal_text(v, "{\"foo\" : null}");
assert_se(json_variant_set_field(&v, "bar", JSON_VARIANT_STRING_CONST("quux")) >= 0);
test_equal_text(v, "{\"foo\" : null, \"bar\" : \"quux\"}");
assert_se(json_variant_set_field(&v, "foo", JSON_VARIANT_STRING_CONST("quux2")) >= 0);
test_equal_text(v, "{\"foo\" : \"quux2\", \"bar\" : \"quux\"}");
assert_se(json_variant_set_field(&v, "bar", NULL) >= 0);
test_equal_text(v, "{\"foo\" : \"quux2\", \"bar\" : null}");
}
int main(int argc, char *argv[]) {
test_setup_logging(LOG_DEBUG);
@ -622,6 +645,7 @@ int main(int argc, char *argv[]) {
test_normalize();
test_bisect();
test_float();
test_set_field();
return 0;
}