Blame SOURCES/0123-utils-strlcpy-and-strlcat-don-t-clobber-dst.patch

99be8f
From 866b995355894ab8f20d22a554d47322dcf1029a Mon Sep 17 00:00:00 2001
99be8f
From: Andrea Claudi <aclaudi@redhat.com>
99be8f
Date: Mon, 29 Apr 2019 20:09:13 +0200
99be8f
Subject: [PATCH] utils: strlcpy() and strlcat() don't clobber dst
99be8f
99be8f
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1465646
99be8f
Upstream Status: iproute2.git commit 50ea3c64384b1
99be8f
99be8f
commit 50ea3c64384b1d1bfa9c96de86c21ac8e9fef183
99be8f
Author: Phil Sutter <phil@nwl.cc>
99be8f
Date:   Wed Sep 6 18:51:42 2017 +0200
99be8f
99be8f
    utils: strlcpy() and strlcat() don't clobber dst
99be8f
99be8f
    As David Laight correctly pointed out, the first version of strlcpy()
99be8f
    modified dst buffer behind the string copied into it. Fix this by
99be8f
    writing NUL to the byte immediately following src string instead of to
99be8f
    the last byte in dst. Doing so also allows to reduce overhead by using
99be8f
    memcpy().
99be8f
99be8f
    Improve strlcat() by avoiding the call to strlcpy() if dst string is
99be8f
    already full, not just as sanity check.
99be8f
99be8f
    Signed-off-by: Phil Sutter <phil@nwl.cc>
99be8f
---
99be8f
 lib/utils.c | 12 ++++++++----
99be8f
 1 file changed, 8 insertions(+), 4 deletions(-)
99be8f
99be8f
diff --git a/lib/utils.c b/lib/utils.c
99be8f
index c9ba2f332c2a7..228d97bfe5e9b 100644
99be8f
--- a/lib/utils.c
99be8f
+++ b/lib/utils.c
99be8f
@@ -1231,18 +1231,22 @@ int get_real_family(int rtm_type, int rtm_family)
99be8f
 
99be8f
 size_t strlcpy(char *dst, const char *src, size_t size)
99be8f
 {
99be8f
+	size_t srclen = strlen(src);
99be8f
+
99be8f
 	if (size) {
99be8f
-		strncpy(dst, src, size - 1);
99be8f
-		dst[size - 1] = '\0';
99be8f
+		size_t minlen = min(srclen, size - 1);
99be8f
+
99be8f
+		memcpy(dst, src, minlen);
99be8f
+		dst[minlen] = '\0';
99be8f
 	}
99be8f
-	return strlen(src);
99be8f
+	return srclen;
99be8f
 }
99be8f
 
99be8f
 size_t strlcat(char *dst, const char *src, size_t size)
99be8f
 {
99be8f
 	size_t dlen = strlen(dst);
99be8f
 
99be8f
-	if (dlen > size)
99be8f
+	if (dlen >= size)
99be8f
 		return dlen + strlen(src);
99be8f
 
99be8f
 	return dlen + strlcpy(dst + dlen, src, size - dlen);
99be8f
-- 
99be8f
2.20.1
99be8f