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