]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-json: make sure JSON_BUILD_STRING_UNDERSCORIFY() maps + to _, too
authorLennart Poettering <lennart@poettering.net>
Wed, 10 Sep 2025 20:46:40 +0000 (22:46 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 18 Sep 2025 07:14:11 +0000 (09:14 +0200)
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

index 9add7be9a881e7d9454034bf9e9e9ed1f8e5ec95..182a85ae7ff93217c155f7e11c9186ce485d3a4e 100644 (file)
@@ -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);