Blob Blame History Raw
From a956c1a2ab36a221c0a5dc7cd474ac86f4033983 Mon Sep 17 00:00:00 2001
From: Kostiantyn Kostiuk <konstantin@daynix.com>
Date: Tue, 20 Jul 2021 17:22:18 +0000
Subject: [PATCH 2/2] qga: get_pci_info leak fix

Signed-off-by: Kostiantyn Kostiuk <konstantin@daynix.com>
---
 qga/commands-win32.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/qga/commands-win32.c b/qga/commands-win32.c
index 300b87c859..cf6d46361a 100644
--- a/qga/commands-win32.c
+++ b/qga/commands-win32.c
@@ -514,7 +514,10 @@ DEFINE_GUID(GUID_DEVINTERFACE_STORAGEPORT,
 
 static GuestPCIAddress *get_pci_info(int number, Error **errp)
 {
-    HDEVINFO dev_info;
+    HDEVINFO dev_info = INVALID_HANDLE_VALUE;
+    HDEVINFO parent_dev_info = INVALID_HANDLE_VALUE;
+    char *parent_dev_id = NULL;
+
     SP_DEVINFO_DATA dev_info_data;
     SP_DEVICE_INTERFACE_DATA dev_iface_data;
     HANDLE dev_file;
@@ -541,8 +544,6 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
     for (i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) {
         PSP_DEVICE_INTERFACE_DETAIL_DATA pdev_iface_detail_data = NULL;
         STORAGE_DEVICE_NUMBER sdn;
-        char *parent_dev_id = NULL;
-        HDEVINFO parent_dev_info;
         SP_DEVINFO_DATA parent_dev_info_data;
         DWORD j;
         DWORD size = 0;
@@ -657,6 +658,7 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
             SetupDiGetClassDevs(&GUID_DEVINTERFACE_STORAGEPORT, parent_dev_id,
                                 NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
         g_free(parent_dev_id);
+        parent_dev_id = NULL;
 
         if (parent_dev_info == INVALID_HANDLE_VALUE) {
             error_setg_win32(errp, GetLastError(),
@@ -745,12 +747,27 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
             }
         }
         SetupDiDestroyDeviceInfoList(parent_dev_info);
+        parent_dev_info = INVALID_HANDLE_VALUE;
         break;
     }
 
 free_dev_info:
-    SetupDiDestroyDeviceInfoList(dev_info);
 out:
+    if (parent_dev_id != NULL) {
+        g_debug("get_pci_info: parent_dev_id pointer present");
+        g_free(parent_dev_id);
+    }
+
+    if (dev_info != INVALID_HANDLE_VALUE) {
+        g_debug("get_pci_info: dev_info handle present");
+        SetupDiDestroyDeviceInfoList(dev_info);
+    }
+
+    if (parent_dev_info != INVALID_HANDLE_VALUE) {
+        g_debug("get_pci_info: parent_dev_info handle present");
+        SetupDiDestroyDeviceInfoList(parent_dev_info);
+    }
+
     return pci;
 }
 
-- 
2.25.1