]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
nss: Fix memory leak in findLease()
authorAlexander Kuznetsov <kuznetsovam@altlinux.org>
Tue, 15 Apr 2025 11:48:38 +0000 (14:48 +0300)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 15 Apr 2025 12:26:59 +0000 (14:26 +0200)
path is allocated by asprintf() and must be freed later if realloc() fails.

Restructure the code to allocate path only after realloc() succeeds,
avoiding the need for an extra free().

Found by Linux Verification Center (linuxtesting.org) with Svace.

Reported-by: Dmitry Fedin <d.fedin@fobos-nt.ru>
Signed-off-by: Alexander Kuznetsov <kuznetsovam@altlinux.org>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
tools/nss/libvirt_nss.c

index d79a00a1b0b0e1735dc0248497c499388efeab1f..3d634b5ae8347e7b5b09da23c29919f1c313111c 100644 (file)
@@ -137,13 +137,15 @@ findLease(const char *name,
 
         if (dlen >= 7 && !strcmp(entry->d_name + dlen - 7, ".status")) {
             char **tmpLease;
-            if (asprintf(&path, "%s/%s", leaseDir, entry->d_name) < 0)
-                goto cleanup;
 
             tmpLease = realloc(leaseFiles, sizeof(char *) * (nleaseFiles + 1));
             if (!tmpLease)
                 goto cleanup;
             leaseFiles = tmpLease;
+
+            if (asprintf(&path, "%s/%s", leaseDir, entry->d_name) < 0)
+                goto cleanup;
+
             leaseFiles[nleaseFiles++] = path;
 #if defined(LIBVIRT_NSS_GUEST)
         } else if (dlen >= 5 && !strcmp(entry->d_name + dlen - 5, ".macs")) {