Blame SOURCES/0107-lib-inet_proto-Review-inet_proto_-a2n-n2a.patch

36cfb7
From e47b57df11565c51b9d8a5307a63d93f8e9a061b Mon Sep 17 00:00:00 2001
36cfb7
From: Andrea Claudi <aclaudi@redhat.com>
36cfb7
Date: Mon, 29 Apr 2019 20:08:08 +0200
36cfb7
Subject: [PATCH] lib/inet_proto: Review inet_proto_{a2n,n2a}()
36cfb7
36cfb7
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1465646
36cfb7
Upstream Status: iproute2.git commit cfda500a7d808
36cfb7
36cfb7
commit cfda500a7d808a6e0f3eca47abd75c22cfe716e5
36cfb7
Author: Phil Sutter <phil@nwl.cc>
36cfb7
Date:   Thu Aug 24 11:51:47 2017 +0200
36cfb7
36cfb7
    lib/inet_proto: Review inet_proto_{a2n,n2a}()
36cfb7
36cfb7
    The original intent was to make sure strings written by those functions
36cfb7
    are NUL-terminated at all times, though it was suggested to get rid of
36cfb7
    the 15 char protocol name limit as well which this patch accomplishes.
36cfb7
36cfb7
    In addition to that, simplify inet_proto_a2n() a bit: Use the error
36cfb7
    checking in get_u8() to find out whether passed 'buf' contains a valid
36cfb7
    decimal number instead of checking the first character's value manually.
36cfb7
36cfb7
    Signed-off-by: Phil Sutter <phil@nwl.cc>
36cfb7
---
36cfb7
 lib/inet_proto.c | 24 +++++++++++++-----------
36cfb7
 1 file changed, 13 insertions(+), 11 deletions(-)
36cfb7
36cfb7
diff --git a/lib/inet_proto.c b/lib/inet_proto.c
36cfb7
index ceda082b12a2e..53c029039b6d5 100644
36cfb7
--- a/lib/inet_proto.c
36cfb7
+++ b/lib/inet_proto.c
36cfb7
@@ -25,7 +25,7 @@
36cfb7
 
36cfb7
 const char *inet_proto_n2a(int proto, char *buf, int len)
36cfb7
 {
36cfb7
-	static char ncache[16];
36cfb7
+	static char *ncache;
36cfb7
 	static int icache = -1;
36cfb7
 	struct protoent *pe;
36cfb7
 
36cfb7
@@ -34,9 +34,12 @@ const char *inet_proto_n2a(int proto, char *buf, int len)
36cfb7
 
36cfb7
 	pe = getprotobynumber(proto);
36cfb7
 	if (pe) {
36cfb7
+		if (icache != -1)
36cfb7
+			free(ncache);
36cfb7
 		icache = proto;
36cfb7
-		strncpy(ncache, pe->p_name, 16);
36cfb7
-		strncpy(buf, pe->p_name, len);
36cfb7
+		ncache = strdup(pe->p_name);
36cfb7
+		strncpy(buf, pe->p_name, len - 1);
36cfb7
+		buf[len - 1] = '\0';
36cfb7
 		return buf;
36cfb7
 	}
36cfb7
 	snprintf(buf, len, "ipproto-%d", proto);
36cfb7
@@ -45,24 +48,23 @@ const char *inet_proto_n2a(int proto, char *buf, int len)
36cfb7
 
36cfb7
 int inet_proto_a2n(const char *buf)
36cfb7
 {
36cfb7
-	static char ncache[16];
36cfb7
+	static char *ncache;
36cfb7
 	static int icache = -1;
36cfb7
 	struct protoent *pe;
36cfb7
+	__u8 ret;
36cfb7
 
36cfb7
-	if (icache>=0 && strcmp(ncache, buf) == 0)
36cfb7
+	if (icache != -1 && strcmp(ncache, buf) == 0)
36cfb7
 		return icache;
36cfb7
 
36cfb7
-	if (buf[0] >= '0' && buf[0] <= '9') {
36cfb7
-		__u8 ret;
36cfb7
-		if (get_u8(&ret, buf, 10))
36cfb7
-			return -1;
36cfb7
+	if (!get_u8(&ret, buf, 10))
36cfb7
 		return ret;
36cfb7
-	}
36cfb7
 
36cfb7
 	pe = getprotobyname(buf);
36cfb7
 	if (pe) {
36cfb7
+		if (icache != -1)
36cfb7
+			free(ncache);
36cfb7
 		icache = pe->p_proto;
36cfb7
-		strncpy(ncache, pe->p_name, 16);
36cfb7
+		ncache = strdup(pe->p_name);
36cfb7
 		return pe->p_proto;
36cfb7
 	}
36cfb7
 	return -1;
36cfb7
-- 
e138d9
2.21.0
36cfb7