Commit d7684186 authored by Daniel P. Berrangé's avatar Daniel P. Berrangé

Ensure nul termination of hostname

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: 's avatarDaniel P. Berrangé <berrange@redhat.com>
parent 7b2725ba
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
const static CMPIBroker *_BROKER; 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; struct hostent *he;
int i; int i;
...@@ -52,7 +52,8 @@ static int resolve_host(char *host, char *buf, int size) ...@@ -52,7 +52,8 @@ static int resolve_host(char *host, char *buf, int size)
for (i = 0; he->h_aliases[i] != NULL; i++) { for (i = 0; he->h_aliases[i] != NULL; i++) {
if ((strchr(he->h_aliases[i], '.') != NULL) && if ((strchr(he->h_aliases[i], '.') != NULL) &&
(strstr(he->h_aliases[i], "localhost") == 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; return 0;
} }
} }
...@@ -63,12 +64,13 @@ static int resolve_host(char *host, char *buf, int size) ...@@ -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 // but also be sure the value isn't empty and that it doesn't
// contain "localhost" // contain "localhost"
if ((he->h_name != NULL) && (!STREQC(he->h_name, "")) && if ((he->h_name != NULL) && (!STREQC(he->h_name, "")) &&
(strstr(he->h_name, "localhost") == NULL)) (strstr(he->h_name, "localhost") == NULL)) {
strncpy(buf, he->h_name, size); strncpy(host, he->h_name, size - 1);
else if ((host != NULL) && (!STREQC(host, "")) && host[size - 1] = '\0';
(strstr(host, "localhost") == NULL)) } else if ((host != NULL) && (!STREQC(host, "")) &&
strncpy(buf, host, size); (strstr(host, "localhost") == NULL)) {
else { return 0;
} else {
CU_DEBUG("Unable to find valid hostname value."); CU_DEBUG("Unable to find valid hostname value.");
return -1; return -1;
} }
...@@ -76,20 +78,18 @@ static int resolve_host(char *host, char *buf, int size) ...@@ -76,20 +78,18 @@ static int resolve_host(char *host, char *buf, int size)
return 0; return 0;
} }
static int get_fqdn(char *buf, int size) static int get_fqdn(char *host, int size)
{ {
char host[256];
int ret = 0; int ret = 0;
if (gethostname(host, sizeof(host)) != 0) { if (gethostname(host, size) != 0) {
CU_DEBUG("gethostname(): %m"); CU_DEBUG("gethostname(): %m");
return -1; return -1;
} }
if (strchr(host, '.') != NULL) if (strchr(host, '.') == NULL) {
strncpy(buf, host, size); ret = resolve_host(host, size);
else }
ret = resolve_host(host, buf, size);
return ret; return ret;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment