Newest GCC warns that the string copying is potentially truncated and thus not nul terminated. In file included from /usr/include/string.h:494, from ../../src/Virt_HostSystem.c:23: In function ‘strncpy’, inlined from ‘resolve_host’ at ../../src/Virt_HostSystem.c:55:28, inlined from ‘get_fqdn’ at ../../src/Virt_HostSystem.c:92:23, inlined from ‘set_host_system_properties’ at ../../src/Virt_HostSystem.c:109:13: /usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ specified bound 256 equals destination size [-Werror=stringop-truncation] 106 | return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In function ‘strncpy’, inlined from ‘resolve_host’ at ../../src/Virt_HostSystem.c:67:17, inlined from ‘get_fqdn’ at ../../src/Virt_HostSystem.c:92:23, inlined from ‘set_host_system_properties’ at ../../src/Virt_HostSystem.c:109:13: /usr/include/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ specified bound 256 equals destination size [-Werror=stringop-truncation] 106 | return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cc1: all warnings being treated as errors Signed-off-by: Daniel P. Berrangé <berrange@xxxxxxxxxx> --- src/Virt_HostSystem.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Virt_HostSystem.c b/src/Virt_HostSystem.c index ebe8184..5bc52ca 100644 --- a/src/Virt_HostSystem.c +++ b/src/Virt_HostSystem.c @@ -38,7 +38,7 @@ const static CMPIBroker *_BROKER; -static int resolve_host(char *host, char *buf, int size) +static int resolve_host(char *host, int size) { struct hostent *he; int i; @@ -52,7 +52,8 @@ static int resolve_host(char *host, char *buf, int size) for (i = 0; he->h_aliases[i] != NULL; i++) { if ((strchr(he->h_aliases[i], '.') != NULL) && (strstr(he->h_aliases[i], "localhost") == NULL)) { - strncpy(buf, he->h_aliases[i], size); + strncpy(host, he->h_aliases[i], size - 1); + host[size - 1] = '\0'; return 0; } } @@ -63,12 +64,13 @@ static int resolve_host(char *host, char *buf, int size) // but also be sure the value isn't empty and that it doesn't // contain "localhost" if ((he->h_name != NULL) && (!STREQC(he->h_name, "")) && - (strstr(he->h_name, "localhost") == NULL)) - strncpy(buf, he->h_name, size); - else if ((host != NULL) && (!STREQC(host, "")) && - (strstr(host, "localhost") == NULL)) - strncpy(buf, host, size); - else { + (strstr(he->h_name, "localhost") == NULL)) { + strncpy(host, he->h_name, size - 1); + host[size - 1] = '\0'; + } else if ((host != NULL) && (!STREQC(host, "")) && + (strstr(host, "localhost") == NULL)) { + return 0; + } else { CU_DEBUG("Unable to find valid hostname value."); return -1; } @@ -76,20 +78,18 @@ static int resolve_host(char *host, char *buf, int size) return 0; } -static int get_fqdn(char *buf, int size) +static int get_fqdn(char *host, int size) { - char host[256]; int ret = 0; - if (gethostname(host, sizeof(host)) != 0) { + if (gethostname(host, size) != 0) { CU_DEBUG("gethostname(): %m"); return -1; } - if (strchr(host, '.') != NULL) - strncpy(buf, host, size); - else - ret = resolve_host(host, buf, size); + if (strchr(host, '.') == NULL) { + ret = resolve_host(host, size); + } return ret; } -- 2.20.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list