From 0b8de3d5df66699cd70f87aae52912a02d92bbcc Mon Sep 17 00:00:00 2001 From: Denis Kenzior Date: Thu, 17 Oct 2019 11:49:11 -0500 Subject: [PATCH] monitor: Fix p2p channel list dumper This dumper probably intended to update pos after invoking strncpy. However, strncpy returns the number of bytes that *would* have been copied and so the logic gets a bit complex to get completely right. Instead, switch to using l_string since this is inside the monitor and not particularly performance critical. --- monitor/nlmon.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/monitor/nlmon.c b/monitor/nlmon.c index 17ba7e24..5b8986bb 100644 --- a/monitor/nlmon.c +++ b/monitor/nlmon.c @@ -3013,8 +3013,9 @@ static void print_p2p_channel_list(unsigned int level, const char *label, while (size) { uint8_t channels; - char str[128]; - int pos = 0; + struct l_string *string; + char *str; + bool first = true; if (size < 2 || size < 2 + bytes[1]) { printf("malformed P2P %s\n", label); @@ -3025,11 +3026,18 @@ static void print_p2p_channel_list(unsigned int level, const char *label, channels = *bytes++; size -= 2 + channels; - while (channels--) - snprintf(str + pos, sizeof(str) - pos, "%s%u", - pos ? ", " : "", (int) *bytes++); + string = l_string_new(128); + while (channels--) { + l_string_append_printf(string, "%s%u", + first ? "" : ", ", + (int ) *bytes++); + first = false; + } + + str = l_string_unwrap(string); print_attr(level + 2, "%s", str); + l_free(str); } }