From: Phil Sutter <phil@nwl.cc>
To: Stephen Hemminger <stephen@networkplumber.org>
Cc: netdev@vger.kernel.org
Subject: [iproute PATCH v4 3/6] lib/inet_proto: Review inet_proto_{a2n,n2a}()
Date: Thu, 24 Aug 2017 11:51:47 +0200 [thread overview]
Message-ID: <20170824095150.5469-4-phil@nwl.cc> (raw)
In-Reply-To: <20170824095150.5469-1-phil@nwl.cc>
The original intent was to make sure strings written by those functions
are NUL-terminated at all times, though it was suggested to get rid of
the 15 char protocol name limit as well which this patch accomplishes.
In addition to that, simplify inet_proto_a2n() a bit: Use the error
checking in get_u8() to find out whether passed 'buf' contains a valid
decimal number instead of checking the first character's value manually.
Signed-off-by: Phil Sutter <phil@nwl.cc>
---
lib/inet_proto.c | 24 +++++++++++++-----------
1 file changed, 13 insertions(+), 11 deletions(-)
diff --git a/lib/inet_proto.c b/lib/inet_proto.c
index ceda082b12a2e..53c029039b6d5 100644
--- a/lib/inet_proto.c
+++ b/lib/inet_proto.c
@@ -25,7 +25,7 @@
const char *inet_proto_n2a(int proto, char *buf, int len)
{
- static char ncache[16];
+ static char *ncache;
static int icache = -1;
struct protoent *pe;
@@ -34,9 +34,12 @@ const char *inet_proto_n2a(int proto, char *buf, int len)
pe = getprotobynumber(proto);
if (pe) {
+ if (icache != -1)
+ free(ncache);
icache = proto;
- strncpy(ncache, pe->p_name, 16);
- strncpy(buf, pe->p_name, len);
+ ncache = strdup(pe->p_name);
+ strncpy(buf, pe->p_name, len - 1);
+ buf[len - 1] = '\0';
return buf;
}
snprintf(buf, len, "ipproto-%d", proto);
@@ -45,24 +48,23 @@ const char *inet_proto_n2a(int proto, char *buf, int len)
int inet_proto_a2n(const char *buf)
{
- static char ncache[16];
+ static char *ncache;
static int icache = -1;
struct protoent *pe;
+ __u8 ret;
- if (icache>=0 && strcmp(ncache, buf) == 0)
+ if (icache != -1 && strcmp(ncache, buf) == 0)
return icache;
- if (buf[0] >= '0' && buf[0] <= '9') {
- __u8 ret;
- if (get_u8(&ret, buf, 10))
- return -1;
+ if (!get_u8(&ret, buf, 10))
return ret;
- }
pe = getprotobyname(buf);
if (pe) {
+ if (icache != -1)
+ free(ncache);
icache = pe->p_proto;
- strncpy(ncache, pe->p_name, 16);
+ ncache = strdup(pe->p_name);
return pe->p_proto;
}
return -1;
--
2.13.1
next prev parent reply other threads:[~2017-08-24 9:52 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-24 9:51 [iproute PATCH v4 0/6] Covscan: Fixes for string termination Phil Sutter
2017-08-24 9:51 ` [iproute PATCH v4 1/6] ipntable: Avoid memory allocation for filter.name Phil Sutter
2017-08-24 9:51 ` [iproute PATCH v4 2/6] lib/fs: Fix format string in find_fs_mount() Phil Sutter
2017-08-24 9:51 ` Phil Sutter [this message]
2017-08-24 9:51 ` [iproute PATCH v4 4/6] lnstat_util: Simplify alloc_and_open() a bit Phil Sutter
2017-08-24 9:51 ` [iproute PATCH v4 5/6] tc/m_xt: Fix for potential string buffer overflows Phil Sutter
2017-08-24 9:51 ` [iproute PATCH v4 6/6] lib/ll_map: Choose size of new cache items at run-time Phil Sutter
2017-08-24 22:16 ` [iproute PATCH v4 0/6] Covscan: Fixes for string termination 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=20170824095150.5469-4-phil@nwl.cc \
--to=phil@nwl.cc \
--cc=netdev@vger.kernel.org \
--cc=stephen@networkplumber.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;
as well as URLs for NNTP newsgroup(s).