lib/xalloc: fix mamory leak in xgethostname() [coverity scan]

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2013-03-27 16:08:47 +01:00
parent 6acde8a3b1
commit 8362545b4a
2 changed files with 10 additions and 6 deletions

View File

@ -83,9 +83,11 @@ static inline char *xgethostname(void)
size_t sz = get_hostname_max() + 1;
name = xmalloc(sizeof(char) * sz);
if (gethostname(name, sz) != 0)
return NULL;
if (gethostname(name, sz) != 0) {
free(name);
return NULL;
}
name[sz - 1] = '\0';
return name;
}

View File

@ -1209,9 +1209,10 @@ static char *xgethostname(void)
if (!name)
log_err(_("failed to allocate memory: %m"));
if (gethostname(name, sz) != 0)
if (gethostname(name, sz) != 0) {
free(name);
return NULL;
}
name[sz - 1] = '\0';
return name;
}
@ -1226,9 +1227,10 @@ static char *xgetdomainname(void)
if (!name)
log_err(_("failed to allocate memory: %m"));
if (getdomainname(name, sz) != 0)
if (getdomainname(name, sz) != 0) {
free(name);
return NULL;
}
name[sz - 1] = '\0';
return name;
#endif