From 6be5b612389a9923d231f7be6465e67fc88c38a8 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 10 Sep 2025 22:46:40 +0200 Subject: [PATCH 1/2] sd-json: make sure JSON_BUILD_STRING_UNDERSCORIFY() maps + to _, too This is ultimately preparation for making systemd-creds's --with-key= switch also accessible via Varlink, because it uses "+" inside an the enum name. It makes sense to to allow this generally however. --- src/libsystemd/sd-json/sd-json.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/libsystemd/sd-json/sd-json.c b/src/libsystemd/sd-json/sd-json.c index 9add7be9a8..182a85ae7f 100644 --- a/src/libsystemd/sd-json/sd-json.c +++ b/src/libsystemd/sd-json/sd-json.c @@ -3474,6 +3474,17 @@ _public_ int sd_json_parse_file( return sd_json_parse_file_at(f, AT_FDCWD, path, flags, ret, reterr_line, reterr_column); } +static char *underscorify(char *p) { + assert(p); + + /* Replaces "-", "+" by "_", to deal with the usual enum naming rules we have. */ + + for (char *q = p; *q; q++) + *q = IN_SET(*q, '_', '-', '+') ? '_' : *q; + + return p; +} + _public_ int sd_json_buildv(sd_json_variant **ret, va_list ap) { JsonStack *stack = NULL; size_t n_stack = 1; @@ -3521,13 +3532,13 @@ _public_ int sd_json_buildv(sd_json_variant **ret, va_list ap) { _cleanup_free_ char *c = NULL; if (command == _JSON_BUILD_STRING_UNDERSCORIFY) { - c = strreplace(p, "-", "_"); + c = strdup(p); if (!c) { r = -ENOMEM; goto finish; } - p = c; + p = underscorify(c); } r = sd_json_variant_new_string(&add, p); From 8c7c808f84a93fdddd04798a94100535d17e3096 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 29 Aug 2025 15:17:56 +0200 Subject: [PATCH 2/2] sd-json: allow sd_json_dispatch() accept NULL pointers and treat them like empty objects --- src/libsystemd/sd-json/sd-json.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libsystemd/sd-json/sd-json.c b/src/libsystemd/sd-json/sd-json.c index 182a85ae7f..77f7524d2a 100644 --- a/src/libsystemd/sd-json/sd-json.c +++ b/src/libsystemd/sd-json/sd-json.c @@ -5096,6 +5096,10 @@ _public_ int sd_json_dispatch_full( int r, done = 0; bool *found; + /* Consider a NULL pointer equivalent to an empty object */ + if (!v) + v = JSON_VARIANT_MAGIC_EMPTY_OBJECT; + if (!sd_json_variant_is_object(v)) { json_log(v, flags, 0, "JSON variant is not an object.");