Blob Blame History Raw
From ae1806eea8c688c6561b5f7dcbaa6f682233b73e Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Mon, 16 Jan 2023 14:16:14 +0100
Subject: [PATCH] busctl: simplify peeking the type

let's peek the type before we enter the variant, not after, so that we
can reuse it as-is, instead having to recombine it later.

Follow-up for: #26049

(cherry picked from commit ec56edf55c26ed2c65cf8e86b81ab0b516c94dd9)

Related: #2138081
---
 src/busctl/busctl.c | 30 ++++++++++--------------------
 1 file changed, 10 insertions(+), 20 deletions(-)

diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c
index cc2d0e3458..901b0e15f6 100644
--- a/src/busctl/busctl.c
+++ b/src/busctl/busctl.c
@@ -1021,17 +1021,16 @@ static int introspect(int argc, char **argv, void *userdata) {
                         return bus_log_parse_error(r);
 
                 for (;;) {
-                        Member *z;
-                        _cleanup_free_ char *buf = NULL, *signature = NULL;
                         _cleanup_fclose_ FILE *mf = NULL;
-                        size_t sz = 0;
+                        _cleanup_free_ char *buf = NULL;
                         const char *name, *contents;
+                        size_t sz = 0;
+                        Member *z;
                         char type;
 
                         r = sd_bus_message_enter_container(reply, 'e', "sv");
                         if (r < 0)
                                 return bus_log_parse_error(r);
-
                         if (r == 0)
                                 break;
 
@@ -1039,24 +1038,15 @@ static int introspect(int argc, char **argv, void *userdata) {
                         if (r < 0)
                                 return bus_log_parse_error(r);
 
-                        r = sd_bus_message_enter_container(reply, 'v', NULL);
+                        r = sd_bus_message_peek_type(reply, &type, &contents);
                         if (r < 0)
                                 return bus_log_parse_error(r);
+                        if (type != 'v')
+                                return bus_log_parse_error(EINVAL);
 
-                        r = sd_bus_message_peek_type(reply, &type, &contents);
-                        if (r <= 0)
-                                return bus_log_parse_error(r == 0 ? EINVAL : r);
-
-                        if (type == SD_BUS_TYPE_STRUCT_BEGIN)
-                                signature = strjoin(CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END));
-                        else if (type == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
-                                signature = strjoin(CHAR_TO_STR(SD_BUS_TYPE_DICT_ENTRY_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_DICT_ENTRY_END));
-                        else if (contents)
-                                signature = strjoin(CHAR_TO_STR(type), contents);
-                        else
-                                signature = strdup(CHAR_TO_STR(type));
-                        if (!signature)
-                                return log_oom();
+                        r = sd_bus_message_enter_container(reply, 'v', contents);
+                        if (r < 0)
+                                return bus_log_parse_error(r);
 
                         mf = open_memstream_unlocked(&buf, &sz);
                         if (!mf)
@@ -1071,7 +1061,7 @@ static int introspect(int argc, char **argv, void *userdata) {
                         z = set_get(members, &((Member) {
                                                 .type = "property",
                                                 .interface = m->interface,
-                                                .signature = signature,
+                                                .signature = (char*) contents,
                                                 .name = (char*) name }));
                         if (z)
                                 free_and_replace(z->value, buf);