From adc93a4ad5fb89f6d10eacf22c6492b0fa8ffb0c Mon Sep 17 00:00:00 2001 From: Adrian Vovk Date: Thu, 24 Sep 2026 15:32:37 -0400 Subject: [PATCH] sd-json: Introduce SD_JSON_FORMAT_EMPTY_OBJECT This mirrors SD_JSON_FORMAT_EMPTY_ARRAY We also removed the assertion that a non-null variant is passed into sd_json_variant_format. It _is_ actually valid to pass a null variant through to sd_json_variant_dump. If neither SD_JSON_FORMAT_EMPTY_* flag is set, then sd_json_variant_format will simply return an empty string. The removal of the assertion fixes a crash in 'varlinkctl --exec call' (cherry picked from commit 7e70352bf10c2fad0099f69e0c451ff1ce8dc362) --- src/libsystemd/sd-json/sd-json.c | 3 ++- src/systemd/sd-json.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libsystemd/sd-json/sd-json.c b/src/libsystemd/sd-json/sd-json.c index 531429b51f..dea16611a2 100644 --- a/src/libsystemd/sd-json/sd-json.c +++ b/src/libsystemd/sd-json/sd-json.c @@ -1868,7 +1868,6 @@ _public_ int sd_json_variant_format(sd_json_variant *v, sd_json_format_flags_t f /* Returns the length of the generated string (without the terminating NUL), * or negative on error. */ - assert_return(v, -EINVAL); assert_return(ret, -EINVAL); if (!sd_json_format_enabled(flags)) @@ -1893,6 +1892,8 @@ _public_ int sd_json_variant_dump(sd_json_variant *v, sd_json_format_flags_t fla if (!v) { if (flags & SD_JSON_FORMAT_EMPTY_ARRAY) v = JSON_VARIANT_MAGIC_EMPTY_ARRAY; + else if (flags & SD_JSON_FORMAT_EMPTY_OBJECT) + v = JSON_VARIANT_MAGIC_EMPTY_OBJECT; else return 0; } diff --git a/src/systemd/sd-json.h b/src/systemd/sd-json.h index 5ad825f3c1..94f08ac05d 100644 --- a/src/systemd/sd-json.h +++ b/src/systemd/sd-json.h @@ -144,6 +144,7 @@ __extension__ typedef enum _SD_ENUM_TYPE_S64(sd_json_format_flags_t) { SD_JSON_FORMAT_FLUSH = 1 << 9, /* call fflush() after dumping JSON */ SD_JSON_FORMAT_EMPTY_ARRAY = 1 << 10, /* output "[]" for empty input */ SD_JSON_FORMAT_CENSOR_SENSITIVE = 1 << 11, /* replace all sensitive elements with the string "" */ + SD_JSON_FORMAT_EMPTY_OBJECT = 1 << 12, /* output "{}" for empty input */ _SD_ENUM_FORCE_S64(JSON_FORMAT_FLAGS) } sd_json_format_flags_t;