Blame SOURCES/0008-Refactor-fix-strncpy-may-miss-trailing-null-byte-war.patch

afe0d5
From d3bf9f5ced41ad0f4e8ae87e80c7e44df4157b61 Mon Sep 17 00:00:00 2001
afe0d5
From: =?UTF-8?q?Jan=20Pokorn=C3=BD?= <jpokorny@redhat.com>
afe0d5
Date: Fri, 13 Jul 2018 14:40:07 +0200
afe0d5
Subject: [PATCH] Refactor: fix "strncpy may miss trailing null byte" warnings
afe0d5
 of GCC 8.1
afe0d5
MIME-Version: 1.0
afe0d5
Content-Type: text/plain; charset=UTF-8
afe0d5
Content-Transfer-Encoding: 8bit
afe0d5
afe0d5
Verbatim warning:
afe0d5
> ‘strncpy’ specified bound 64 equals destination size [-Wstringop-truncation]
afe0d5
afe0d5
Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
afe0d5
---
afe0d5
 src/config.c | 12 ++++++++++--
afe0d5
 1 file changed, 10 insertions(+), 2 deletions(-)
afe0d5
afe0d5
diff --git a/src/config.c b/src/config.c
afe0d5
index 9df5767..e4d36ab 100644
afe0d5
--- a/src/config.c
afe0d5
+++ b/src/config.c
afe0d5
@@ -75,7 +75,10 @@ static void hostname_to_ip(char * hostname)
afe0d5
 
afe0d5
 	/* Return the first found address */
afe0d5
 	if (addr_list[0] != NULL) {
afe0d5
-		strncpy(hostname, inet_ntoa(*addr_list[0]), BOOTH_NAME_LEN);
afe0d5
+		strncpy(hostname, inet_ntoa(*addr_list[0]), BOOTH_NAME_LEN - 1);
afe0d5
+		/* buffer overflow will not happen (IPv6 notation < 63 chars),
afe0d5
+		   but suppress the warnings */
afe0d5
+		hostname[BOOTH_NAME_LEN - 1] = '\0';
afe0d5
 	}
afe0d5
 	else {
afe0d5
 		log_error("no IP addresses found for the host \"%s\"", hostname);
afe0d5
@@ -106,7 +109,12 @@ static int add_site(char *addr_string, int type)
afe0d5
 	site->family = AF_INET;
afe0d5
 	site->type = type;
afe0d5
 
afe0d5
-	strncpy(site->addr_string, addr_string, sizeof(site->addr_string));
afe0d5
+	/* buffer overflow will not hapen (we've already checked that
afe0d5
+	   addr_string will fit incl. terminating '\0' above), but
afe0d5
+	   suppress the warnings with copying everything but the boundary
afe0d5
+	   byte, which is valid as-is, since this last byte will be safely
afe0d5
+	   pre-zeroed from the struct booth_config initialization */
afe0d5
+	strncpy(site->addr_string, addr_string, sizeof(site->addr_string) - 1);
afe0d5
 
afe0d5
 	if (!(inet_pton(AF_INET, site->addr_string, &site->sa4.sin_addr) > 0) &&
afe0d5
         !(inet_pton(AF_INET6, site->addr_string, &site->sa6.sin6_addr) > 0)) {
afe0d5
-- 
afe0d5
2.18.0.rc2
afe0d5