From: Alexander Kuznetsov Date: Tue, 15 Apr 2025 11:48:38 +0000 (+0300) Subject: nss: Fix memory leak in findLease() X-Git-Tag: v11.3.0-rc1~18 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=faa98ca6d343586fefac9d6f011efc6a8032dcad;p=thirdparty%2Flibvirt.git nss: Fix memory leak in findLease() 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 Signed-off-by: Alexander Kuznetsov Signed-off-by: Michal Privoznik Reviewed-by: Michal Privoznik --- diff --git a/tools/nss/libvirt_nss.c b/tools/nss/libvirt_nss.c index d79a00a1b0..3d634b5ae8 100644 --- a/tools/nss/libvirt_nss.c +++ b/tools/nss/libvirt_nss.c @@ -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")) {