naccyde / rpms / iproute

Forked from rpms/iproute 7 months ago
Clone

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

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