All of lore.kernel.org
 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 2/3] lib: fix warning from ll_addr_a2n
Date: Sun, 23 Aug 2026 16:51:32 -0700	[thread overview]
Message-ID: <20260823235220.79790-3-stephen@networkplumber.org> (raw)
In-Reply-To: <20260823235220.79790-1-stephen@networkplumber.org>

Recent versions of strchr() propogate const char *.
This triggers warnings about lost of const.
Resolve this by reworking the loop parsing hex values.
This also adds checking for garbage after the hex value.

Also:
 - arg is no longer modified
 - Trailing garbage ("00:11:zz", "00:11:") are now rejected
 - An address longer than len bytes now fails

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/ll_addr.c | 52 ++++++++++++++++++++++++++-------------------------
 1 file changed, 27 insertions(+), 25 deletions(-)

diff --git a/lib/ll_addr.c b/lib/ll_addr.c
index 7a9eb3a4..851a5be0 100644
--- a/lib/ll_addr.c
+++ b/lib/ll_addr.c
@@ -7,6 +7,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <ctype.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
@@ -45,8 +46,12 @@ const char *ll_addr_n2a(const unsigned char *addr, int alen, int type,
 /*NB: lladdr is char * (rather than u8 *) because sa_data is char * (1003.1g) */
 int ll_addr_a2n(char *lladdr, int len, const char *arg)
 {
+	const char *cp;
+	int i;
+
 	if (strchr(arg, '.')) {
 		inet_prefix pfx;
+
 		if (get_addr_1(&pfx, arg, AF_INET)) {
 			fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg);
 			return -1;
@@ -55,31 +60,28 @@ int ll_addr_a2n(char *lladdr, int len, const char *arg)
 			return -1;
 		memcpy(lladdr, pfx.data, 4);
 		return 4;
-	} else {
-		int i;
+	}
 
-		for (i = 0; i < len; i++) {
-			int temp;
-			char *cp = strchr(arg, ':');
-			if (cp) {
-				*cp = 0;
-				cp++;
-			}
-			if (sscanf(arg, "%x", &temp) != 1) {
-				fprintf(stderr, "\"%s\" is invalid lladdr.\n",
-					arg);
-				return -1;
-			}
-			if (temp < 0 || temp > 255) {
-				fprintf(stderr, "\"%s\" is invalid lladdr.\n",
-					arg);
-				return -1;
-			}
-			lladdr[i] = temp;
-			if (!cp)
-				break;
-			arg = cp;
-		}
-		return i + 1;
+	for (i = 0, cp = arg; i < len; i++) {
+		unsigned long val;
+		char *endp;
+
+		if (!isxdigit(*cp))
+			goto invalid;
+
+		val = strtoul(cp, &endp, 16);
+		if (val > 255)
+			goto invalid;
+
+		lladdr[i] = val;
+
+		if (*endp == '\0')
+			return i + 1;
+		if (*endp != ':')
+			goto invalid;
+		cp = endp + 1;
 	}
+invalid:
+	fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg);
+	return -1;
 }
-- 
2.53.0


  parent reply	other threads:[~2026-08-23 23:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-23 23:51 [PATCH iproute2 0/3] compiler warning fixes Stephen Hemminger
2026-08-23 23:51 ` [PATCH iproute2 1/3] json_print: fix Gcc-17 warnings Stephen Hemminger
2026-08-23 23:51 ` Stephen Hemminger [this message]
2026-08-23 23:51 ` [PATCH iproute2 3/3] tc/f_u32: fix const strchr() warning Stephen Hemminger
2026-08-25  4:10 ` [PATCH iproute2 0/3] compiler warning fixes patchwork-bot+netdevbpf

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=20260823235220.79790-3-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.