From: Lennart Poettering Date: Tue, 7 Jan 2025 13:41:22 +0000 (+0100) Subject: sd-json: make it safe to call sd_json_dispatch_full() with a NULL table X-Git-Tag: v258-rc1~1641^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=14533598bd0eb9f1f5f563e80fbd9645638831f6;p=thirdparty%2Fsystemd.git sd-json: make it safe to call sd_json_dispatch_full() with a NULL table 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. --- diff --git a/src/libsystemd/sd-json/sd-json.c b/src/libsystemd/sd-json/sd-json.c index b8633548c80..99d941e1efd 100644 --- a/src/libsystemd/sd-json/sd-json.c +++ b/src/libsystemd/sd-json/sd-json.c @@ -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]) { diff --git a/src/libsystemd/sd-varlink/sd-varlink.c b/src/libsystemd/sd-varlink/sd-varlink.c index 9e8f2a66f8b..962adcb8697 100644 --- a/src/libsystemd/sd-varlink/sd-varlink.c +++ b/src/libsystemd/sd-varlink/sd-varlink.c @@ -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. */