From: Luca Boccassi Date: Wed, 7 Apr 2021 09:35:34 +0000 (+0100) Subject: coredump: fetch JSON object by key instead of iterating X-Git-Tag: v249-rc1~463^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1f2abb791e3b963e614a8bac0313669484376e49;p=thirdparty%2Fsystemd.git coredump: fetch JSON object by key instead of iterating Follow-up for c546154a4448ddf9 --- diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index 2e56184025a..d5b079a53be 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -797,8 +797,7 @@ log: JSON_VARIANT_OBJECT_FOREACH(module_name, module_json, json_metadata) { _cleanup_free_ char *module_basename = NULL, *exe_basename = NULL; - const char *key; - JsonVariant *w; + JsonVariant *package_name, *package_version; /* The module name, most likely parsed from the ELF core file, * sometimes contains the full path and sometimes does not. */ @@ -813,26 +812,13 @@ log: if (!streq(module_basename, exe_basename)) continue; - /* Cannot nest two JSON_VARIANT_OBJECT_FOREACH as they define the same - * iterator variable '_state' */ - for (struct json_variant_foreach_state _state2 = { (module_json), 0 }; \ - json_variant_is_object(_state2.variant) && \ - _state2.idx < json_variant_elements(_state2.variant) && \ - ({ key = json_variant_string(json_variant_by_index(_state2.variant, _state2.idx)); \ - w = json_variant_by_index(_state2.variant, _state2.idx + 1); \ - true; }); \ - _state2.idx += 2) { - - if (!json_variant_is_string(w)) - continue; - - if (!STR_IN_SET(key, "package", "packageVersion")) - continue; + package_name = json_variant_by_key(module_json, "package"); + if (package_name) + (void) iovw_put_string_field(iovw, "COREDUMP_PACKAGE_NAME=", json_variant_string(package_name)); - (void) iovw_put_string_field(iovw, - streq(key, "package") ? "COREDUMP_PACKAGE_NAME=" : "COREDUMP_PACKAGE_VERSION=", - json_variant_string(w)); - } + package_version = json_variant_by_key(module_json, "packageVersion"); + if (package_version) + (void) iovw_put_string_field(iovw, "COREDUMP_PACKAGE_VERSION=", json_variant_string(package_version)); } /* Optionally store the entire coredump in the journal */