From: Phil Sutter <phil@nwl.cc>
To: Stephen Hemminger <shemming@brocade.com>
Cc: netdev@vger.kernel.org
Subject: [iproute PATCH 7/7] lib/ll_addr: improve ll_addr_n2a() a bit
Date: Tue, 22 Mar 2016 19:35:19 +0100 [thread overview]
Message-ID: <1458671719-14361-8-git-send-email-phil@nwl.cc> (raw)
In-Reply-To: <1458671719-14361-1-git-send-email-phil@nwl.cc>
Apart from making the code a bit more compact and efficient, this also
prevents a potential buffer overflow if the passed buffer is really too
small: Although correctly decrementing the size parameter passed to
snprintf, it could become negative which would then wrap since snprintf
uses (unsigned) size_t for the parameter.
Signed-off-by: Phil Sutter <phil@nwl.cc>
---
lib/ll_addr.c | 15 +++------------
1 file changed, 3 insertions(+), 12 deletions(-)
diff --git a/lib/ll_addr.c b/lib/ll_addr.c
index 2ce9abfbb8c69..465ed6fa4d9a2 100644
--- a/lib/ll_addr.c
+++ b/lib/ll_addr.c
@@ -41,18 +41,9 @@ const char *ll_addr_n2a(const unsigned char *addr, int alen, int type, char *buf
if (alen == 16 && type == ARPHRD_TUNNEL6) {
return inet_ntop(AF_INET6, addr, buf, blen);
}
- l = 0;
- for (i=0; i<alen; i++) {
- if (i==0) {
- snprintf(buf+l, blen, "%02x", addr[i]);
- blen -= 2;
- l += 2;
- } else {
- snprintf(buf+l, blen, ":%02x", addr[i]);
- blen -= 3;
- l += 3;
- }
- }
+ snprintf(buf, blen, "%02x", addr[0]);
+ for (i = 1, l = 2; i < alen && l < blen; i++, l += 3)
+ snprintf(buf + l, blen - l, ":%02x", addr[i]);
return buf;
}
--
2.7.2
next prev parent reply other threads:[~2016-03-22 18:35 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-22 18:35 [iproute PATCH 0/7] Refactor some internal library functions Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 1/7] color: introduce color helpers and COLOR_CLEAR Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 2/7] ipaddress: colorize peer, broadcast and anycast addresses as well Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 3/7] make format_host non-reentrant by default Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 4/7] utils: make rt_addr_n2a() " Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 5/7] lib/utils: introduce format_host_rta() Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 6/7] lib/utils: introduce rt_addr_n2a_rta() Phil Sutter
2016-03-22 18:35 ` Phil Sutter [this message]
2016-03-27 17:38 ` [iproute PATCH 0/7] Refactor some internal library functions Stephen Hemminger
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=1458671719-14361-8-git-send-email-phil@nwl.cc \
--to=phil@nwl.cc \
--cc=netdev@vger.kernel.org \
--cc=shemming@brocade.com \
/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;
as well as URLs for NNTP newsgroup(s).