]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-json: make it safe to call sd_json_dispatch_full() with a NULL table
authorLennart Poettering <lennart@poettering.net>
Tue, 7 Jan 2025 13:41:22 +0000 (14:41 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 10 Jan 2025 13:03:21 +0000 (14:03 +0100)
This is useful for generating good errors when dispatching varlink
methods that take no parameters, as we'll still generate precise errors
in that case, taking a NULL table as equivalent as one with no
entries.

src/libsystemd/sd-json/sd-json.c
src/libsystemd/sd-varlink/sd-varlink.c

index b8633548c80ed3cd89cddadc233a88fff0f2ffa5..99d941e1efdc064e117fa8f111e601292572c56d 100644 (file)
@@ -5104,7 +5104,7 @@ _public_ int sd_json_dispatch_full(
         }
 
         m = 0;
-        for (const sd_json_dispatch_field *p = table; p->name; p++)
+        for (const sd_json_dispatch_field *p = table; p && p->name; p++)
                 m++;
 
         found = newa0(bool, m);
@@ -5117,12 +5117,12 @@ _public_ int sd_json_dispatch_full(
                 assert_se(key = sd_json_variant_by_index(v, i));
                 assert_se(value = sd_json_variant_by_index(v, i+1));
 
-                for (p = table; p->name; p++)
+                for (p = table; p && p->name; p++)
                         if (p->name == POINTER_MAX ||
                             streq_ptr(sd_json_variant_string(key), p->name))
                                 break;
 
-                if (p->name) { /* Found a matching entry! ðŸ™‚ */
+                if (p && p->name) { /* Found a matching entry! ðŸ™‚ */
                         sd_json_dispatch_flags_t merged_flags;
 
                         merged_flags = flags | p->flags;
@@ -5223,7 +5223,7 @@ _public_ int sd_json_dispatch_full(
                 }
         }
 
-        for (const sd_json_dispatch_field *p = table; p->name; p++) {
+        for (const sd_json_dispatch_field *p = table; p && p->name; p++) {
                 sd_json_dispatch_flags_t merged_flags = p->flags | flags;
 
                 if ((merged_flags & SD_JSON_MANDATORY) && !found[p-table]) {
index 9e8f2a66f8b0fae68e0ef7201145b94ae67db857..962adcb8697c33040df7e76cce18e9681197bbb6 100644 (file)
@@ -2755,7 +2755,6 @@ _public_ int sd_varlink_dispatch(sd_varlink *v, sd_json_variant *parameters, con
         int r;
 
         assert_return(v, -EINVAL);
-        assert_return(table, -EINVAL);
 
         /* A wrapper around json_dispatch_full() that returns a nice InvalidParameter error if we hit a problem with some field. */