]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/xalloc: fix mamory leak in xgethostname() [coverity scan]
authorKarel Zak <kzak@redhat.com>
Wed, 27 Mar 2013 15:08:47 +0000 (16:08 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 27 Mar 2013 15:08:47 +0000 (16:08 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/xalloc.h
term-utils/agetty.c

index 7b685e71843172d221ef94a0484de1b7cd1fee58..1f29621ef709b71d89a87177c88a54af96036453 100644 (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;
 }
index 7ac12f20148c9cc7571efa505ec10a0727fd2272..f745e680006b2be9914a69efd1c55890b4dbdb4d 100644 (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