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

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