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

src/coredump/coredump.c

index 2e56184025abbc6613ba2c150919016afa0b2f70..d5b079a53bef5bc44cb0ca62c9b7c3b7a8346538 100644 (file)
@@ -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 */