From: Luca Boccassi Date: Wed, 7 Apr 2021 09:38:56 +0000 (+0100) Subject: coredumpctl: fetch JSON object by key instead of iterating X-Git-Tag: v249-rc1~463^2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b7ddd44497cc08e3d437743ba137c7e6eb6f116f;p=thirdparty%2Fsystemd.git coredumpctl: fetch JSON object by key instead of iterating Follow-up for d1b5a0c691 --- diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index 219f201a555..dffb92981b1 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -735,18 +735,9 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) { const char *module_name; JsonVariant *module_json; - /* Cannot nest two JSON_VARIANT_OBJECT_FOREACH as they define the same - * iterator variable '_state' */ - for (struct json_variant_foreach_state _state2 = { (v), 0 }; \ - json_variant_is_object(_state2.variant) && \ - _state2.idx < json_variant_elements(_state2.variant) && \ - ({ module_name = json_variant_string(json_variant_by_index(_state2.variant, _state2.idx)); \ - module_json = json_variant_by_index(_state2.variant, _state2.idx + 1); \ - true; }); \ - _state2.idx += 2) { + JSON_VARIANT_OBJECT_FOREACH(module_name, module_json, v) { _cleanup_free_ char *module_basename = NULL, *exe_basename = NULL; - const char *key; - JsonVariant *w; + JsonVariant *build_id; /* The module name, most likely parsed from the ELF core file, * sometimes contains the full path and sometimes does not. */ @@ -765,17 +756,9 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) { if (!streq(module_basename, exe_basename)) continue; - JSON_VARIANT_OBJECT_FOREACH(key, w, module_json) { - if (!json_variant_is_string(w)) - continue; - - if (!streq(key, "buildId")) - continue; - - fprintf(file, " build-id: %s\n", json_variant_string(w)); - - break; - } + build_id = json_variant_by_key(module_json, "buildId"); + if (build_id) + fprintf(file, " build-id: %s\n", json_variant_string(build_id)); break; }