Netdev List
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: netdev@vger.kernel.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH iproute2 3/3] tc: build ct label strings with hexstring_alloc
Date: Thu, 23 Jul 2026 12:11:40 -0700	[thread overview]
Message-ID: <20260723191511.216182-4-stephen@networkplumber.org> (raw)
In-Reply-To: <20260723191511.216182-1-stephen@networkplumber.org>

flower_print_ct_label() and ct_print_labels() formatted the key and
optional mask into a fixed on-stack buffer with manual pointer
arithmetic, relying on hexstring_n2a() not to overrun it. Build the
"key/mask" string with hexstring_alloc() and asprintf() instead, which
sizes to the data and drops the open-coded offset handling.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 tc/f_flower.c | 25 ++++++++++++++-----------
 tc/m_ct.c     | 24 ++++++++++++++----------
 2 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/tc/f_flower.c b/tc/f_flower.c
index 6fc2c6a1..d6844282 100644
--- a/tc/f_flower.c
+++ b/tc/f_flower.c
@@ -2619,18 +2619,15 @@ static void flower_print_ct_label(struct rtattr *attr,
 {
 	const unsigned char *str;
 	bool print_mask = false;
+	char *key, *out;
 	int data_len, i;
-	char out[128];
-	char *p;
 
 	if (!attr)
 		return;
 
 	data_len = RTA_PAYLOAD(attr);
-	hexstring_n2a(RTA_DATA(attr), data_len, out, sizeof(out));
-	p = out + data_len*2;
+	key = hexstring_alloc(RTA_DATA(attr), data_len);
 
-	data_len = RTA_PAYLOAD(attr);
 	str = RTA_DATA(mask_attr);
 	if (data_len != 16)
 		print_mask = true;
@@ -2638,16 +2635,22 @@ static void flower_print_ct_label(struct rtattr *attr,
 		if (str[i] != 0xff)
 			print_mask = true;
 	}
+
 	if (print_mask) {
-		*p++ = '/';
-		hexstring_n2a(RTA_DATA(mask_attr), data_len, p,
-			      sizeof(out)-(p-out));
-		p += data_len*2;
+		char *mask = hexstring_alloc(RTA_DATA(mask_attr), data_len);
+
+		if (asprintf(&out, "%s/%s", key, mask) < 0)
+			out = NULL;
+		free(mask);
+		free(key);
+	} else {
+		out = key;
 	}
-	*p = '\0';
 
 	print_nl();
-	print_string(PRINT_ANY, "ct_label", "  ct_label %s", out);
+	print_string(PRINT_ANY, "ct_label", "  ct_label %s", out ? : "");
+
+	free(out);
 }
 
 static void flower_print_ct_zone(struct rtattr *attr,
diff --git a/tc/m_ct.c b/tc/m_ct.c
index e549cb9c..e5eebbda 100644
--- a/tc/m_ct.c
+++ b/tc/m_ct.c
@@ -443,17 +443,15 @@ static void ct_print_labels(struct rtattr *attr,
 {
 	const unsigned char *str;
 	bool print_mask = false;
-	char out[256], *p;
+	char *key, *out;
 	int data_len, i;
 
 	if (!attr)
 		return;
 
 	data_len = RTA_PAYLOAD(attr);
-	hexstring_n2a(RTA_DATA(attr), data_len, out, sizeof(out));
-	p = out + data_len*2;
+	key = hexstring_alloc(RTA_DATA(attr), data_len);
 
-	data_len = RTA_PAYLOAD(attr);
 	str = RTA_DATA(mask_attr);
 	if (data_len != 16)
 		print_mask = true;
@@ -461,15 +459,21 @@ static void ct_print_labels(struct rtattr *attr,
 		if (str[i] != 0xff)
 			print_mask = true;
 	}
+
 	if (print_mask) {
-		*p++ = '/';
-		hexstring_n2a(RTA_DATA(mask_attr), data_len, p,
-			      sizeof(out)-(p-out));
-		p += data_len*2;
+		char *mask = hexstring_alloc(RTA_DATA(mask_attr), data_len);
+
+		if (asprintf(&out, "%s/%s", key, mask) < 0)
+			out = NULL;
+		free(mask);
+		free(key);
+	} else {
+		out = key;
 	}
-	*p = '\0';
 
-	print_string(PRINT_ANY, "label", " label %s", out);
+	print_string(PRINT_ANY, "label", " label %s", out ? : "");
+
+	free(out);
 }
 
 static void ct_print_helper(struct rtattr *family, struct rtattr *proto, struct rtattr *name)
-- 
2.53.0


      parent reply	other threads:[~2026-07-23 19:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-23 19:11 [PATCH iproute2 0/3] hexstring truncation fixes Stephen Hemminger
2026-07-23 19:11 ` [PATCH iproute2 1/3] utils: add hexstring_alloc and print_hexstring helpers Stephen Hemminger
2026-07-23 19:11 ` [PATCH iproute2 2/3] ip, tc: print hex attributes with print_hexstring Stephen Hemminger
2026-07-23 19:11 ` Stephen Hemminger [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260723191511.216182-4-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox