[PATCH] driver: Don't leak saved error in virGetConnectGeneric()

Michal Privoznik posted 1 patch 2 years, 11 months ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/0f00a93459269b13f82d977d979f584fc4b4a79d.1621255953.git.mprivozn@redhat.com
src/driver.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
[PATCH] driver: Don't leak saved error in virGetConnectGeneric()
Posted by Michal Privoznik 2 years, 11 months ago
Recently, a new code was added to virGetConnectGeneric() that
saves the original error into a variable so that it's not lost in
virConnectClose() called under the 'error' label.

However, the error saving code uses virSaveLastError() +
virSetError() combo which leaks the memory allocated for the
error copy. Using virErrorPreserveLast() + virErrorRestore() does
the same job without the memleak.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/driver.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/driver.c b/src/driver.c
index 227bb56e48..329d493a50 100644
--- a/src/driver.c
+++ b/src/driver.c
@@ -138,7 +138,7 @@ static virConnectPtr
 virGetConnectGeneric(virThreadLocal *threadPtr, const char *name)
 {
     virConnectPtr conn;
-    virErrorPtr saved;
+    virErrorPtr orig_err;
 
     if (virConnectCacheInitialize() < 0)
         return NULL;
@@ -178,9 +178,9 @@ virGetConnectGeneric(virThreadLocal *threadPtr, const char *name)
     return conn;
 
  error:
-    saved = virSaveLastError();
+    virErrorPreserveLast(&orig_err);
     virConnectClose(conn);
-    virSetError(saved);
+    virErrorRestore(&orig_err);
     return NULL;
 }
 
-- 
2.26.3