From: Lennart Poettering Date: Wed, 10 Sep 2025 20:46:40 +0000 (+0200) Subject: sd-json: make sure JSON_BUILD_STRING_UNDERSCORIFY() maps + to _, too X-Git-Tag: v259-rc1~521^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6be5b612389a9923d231f7be6465e67fc88c38a8;p=thirdparty%2Fsystemd.git 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. --- diff --git a/src/libsystemd/sd-json/sd-json.c b/src/libsystemd/sd-json/sd-json.c index 9add7be9a88..182a85ae7ff 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);