ac3a84
From ae1806eea8c688c6561b5f7dcbaa6f682233b73e Mon Sep 17 00:00:00 2001
ac3a84
From: Lennart Poettering <lennart@poettering.net>
ac3a84
Date: Mon, 16 Jan 2023 14:16:14 +0100
ac3a84
Subject: [PATCH] busctl: simplify peeking the type
ac3a84
ac3a84
let's peek the type before we enter the variant, not after, so that we
ac3a84
can reuse it as-is, instead having to recombine it later.
ac3a84
ac3a84
Follow-up for: #26049
ac3a84
ac3a84
(cherry picked from commit ec56edf55c26ed2c65cf8e86b81ab0b516c94dd9)
ac3a84
ac3a84
Related: #2138081
ac3a84
---
ac3a84
 src/busctl/busctl.c | 30 ++++++++++--------------------
ac3a84
 1 file changed, 10 insertions(+), 20 deletions(-)
ac3a84
ac3a84
diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c
ac3a84
index cc2d0e3458..901b0e15f6 100644
ac3a84
--- a/src/busctl/busctl.c
ac3a84
+++ b/src/busctl/busctl.c
ac3a84
@@ -1021,17 +1021,16 @@ static int introspect(int argc, char **argv, void *userdata) {
ac3a84
                         return bus_log_parse_error(r);
ac3a84
 
ac3a84
                 for (;;) {
ac3a84
-                        Member *z;
ac3a84
-                        _cleanup_free_ char *buf = NULL, *signature = NULL;
ac3a84
                         _cleanup_fclose_ FILE *mf = NULL;
ac3a84
-                        size_t sz = 0;
ac3a84
+                        _cleanup_free_ char *buf = NULL;
ac3a84
                         const char *name, *contents;
ac3a84
+                        size_t sz = 0;
ac3a84
+                        Member *z;
ac3a84
                         char type;
ac3a84
 
ac3a84
                         r = sd_bus_message_enter_container(reply, 'e', "sv");
ac3a84
                         if (r < 0)
ac3a84
                                 return bus_log_parse_error(r);
ac3a84
-
ac3a84
                         if (r == 0)
ac3a84
                                 break;
ac3a84
 
ac3a84
@@ -1039,24 +1038,15 @@ static int introspect(int argc, char **argv, void *userdata) {
ac3a84
                         if (r < 0)
ac3a84
                                 return bus_log_parse_error(r);
ac3a84
 
ac3a84
-                        r = sd_bus_message_enter_container(reply, 'v', NULL);
ac3a84
+                        r = sd_bus_message_peek_type(reply, &type, &contents);
ac3a84
                         if (r < 0)
ac3a84
                                 return bus_log_parse_error(r);
ac3a84
+                        if (type != 'v')
ac3a84
+                                return bus_log_parse_error(EINVAL);
ac3a84
 
ac3a84
-                        r = sd_bus_message_peek_type(reply, &type, &contents);
ac3a84
-                        if (r <= 0)
ac3a84
-                                return bus_log_parse_error(r == 0 ? EINVAL : r);
ac3a84
-
ac3a84
-                        if (type == SD_BUS_TYPE_STRUCT_BEGIN)
ac3a84
-                                signature = strjoin(CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END));
ac3a84
-                        else if (type == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
ac3a84
-                                signature = strjoin(CHAR_TO_STR(SD_BUS_TYPE_DICT_ENTRY_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_DICT_ENTRY_END));
ac3a84
-                        else if (contents)
ac3a84
-                                signature = strjoin(CHAR_TO_STR(type), contents);
ac3a84
-                        else
ac3a84
-                                signature = strdup(CHAR_TO_STR(type));
ac3a84
-                        if (!signature)
ac3a84
-                                return log_oom();
ac3a84
+                        r = sd_bus_message_enter_container(reply, 'v', contents);
ac3a84
+                        if (r < 0)
ac3a84
+                                return bus_log_parse_error(r);
ac3a84
 
ac3a84
                         mf = open_memstream_unlocked(&buf, &sz);
ac3a84
                         if (!mf)
ac3a84
@@ -1071,7 +1061,7 @@ static int introspect(int argc, char **argv, void *userdata) {
ac3a84
                         z = set_get(members, &((Member) {
ac3a84
                                                 .type = "property",
ac3a84
                                                 .interface = m->interface,
ac3a84
-                                                .signature = signature,
ac3a84
+                                                .signature = (char*) contents,
ac3a84
                                                 .name = (char*) name }));
ac3a84
                         if (z)
ac3a84
                                 free_and_replace(z->value, buf);