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)
This commit is contained in:
Adrian Vovk
2026-10-16 17:24:52 +01:00
committed by Luca Boccassi
parent 934e31fac0
commit adc93a4ad5
2 changed files with 3 additions and 1 deletions
+2 -1
View File
@@ -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;
}
+1
View File
@@ -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 "<sensitive data>" */
SD_JSON_FORMAT_EMPTY_OBJECT = 1 << 12, /* output "{}" for empty input */
_SD_ENUM_FORCE_S64(JSON_FORMAT_FLAGS)
} sd_json_format_flags_t;