]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
coredumpctl: fetch JSON object by key instead of iterating
authorLuca Boccassi <luca.boccassi@microsoft.com>
Wed, 7 Apr 2021 09:38:56 +0000 (10:38 +0100)
committerLuca Boccassi <luca.boccassi@microsoft.com>
Wed, 7 Apr 2021 13:03:49 +0000 (14:03 +0100)
Follow-up for d1b5a0c691

src/coredump/coredumpctl.c

index 219f201a55519f11bc01ebdee5653f920bc6daa9..dffb92981b1fa12396a9477d6cee6bed14ad7fb4 100644 (file)
@@ -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;
                         }