Skip to contentSkip to conversation

gqa-win: get_pci_info: Clean dev_info if handle is valid

RFC In review Kostiantyn Kostiuk 2 participants 6 messages Aug 9, 2021 – Aug 10, 2021

lore
Share & tools
lore
#0 lore raw

RFC 1/4 gqa-win: get_pci_info: Clean dev_info if handle is valid

---
 qga/commands-win32.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
qga/commands-win32.c +4 -2
index 27baf17d6c..2ad8593b82 100644
@@ -514,7 +514,7 @@ DEFINE_GUID(GUID_DEVINTERFACE_STORAGEPORT,
 
 static GuestPCIAddress *get_pci_info(int number, Error **errp)
 {
-    HDEVINFO dev_info;
+    HDEVINFO dev_info = INVALID_HANDLE_VALUE;
     SP_DEVINFO_DATA dev_info_data;
     SP_DEVICE_INTERFACE_DATA dev_iface_data;
     HANDLE dev_file;
@@ -749,7 +749,9 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
     }
 
 free_dev_info:
-    SetupDiDestroyDeviceInfoList(dev_info);
+    if (dev_info != INVALID_HANDLE_VALUE) {
+        SetupDiDestroyDeviceInfoList(dev_info);
+    }
 out:
     return pci;
 }
Signature · 1 line
2.25.1
#1 lore raw

RFC 2/4 gqa-win: get_pci_info: Use common 'cleanup' label

To prevent memory leaks, always try to free initialized variables.

---
 qga/commands-win32.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)
qga/commands-win32.c +12 -13
index 2ad8593b82..724ce76a0e 100644
@@ -532,7 +532,7 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
                                    DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
     if (dev_info == INVALID_HANDLE_VALUE) {
         error_setg_win32(errp, GetLastError(), "failed to get devices tree");
-        goto out;
+        goto cleanup;
     }
 
     g_debug("enumerating devices");
@@ -562,7 +562,7 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
                 } else {
                     error_setg_win32(errp, GetLastError(),
                                      "failed to get device interfaces");
-                    goto free_dev_info;
+                    goto cleanup;
                 }
             }
 
@@ -576,7 +576,7 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
                 CloseHandle(dev_file);
                 error_setg_win32(errp, GetLastError(),
                                  "failed to get device slot number");
-                goto free_dev_info;
+                goto cleanup;
             }
 
             CloseHandle(dev_file);
@@ -586,7 +586,7 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
         } else {
             error_setg_win32(errp, GetLastError(),
                              "failed to get device interfaces");
-            goto free_dev_info;
+            goto cleanup;
         }
 
         g_debug("found device slot %d. Getting storage controller", number);
@@ -603,7 +603,7 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
                 } else {
                     error_setg_win32(errp, GetLastError(),
                                      "failed to get device instance ID");
-                    goto out;
+                    goto cleanup;
                 }
             }
 
@@ -617,14 +617,14 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
                 g_error("CM_Locate_DevInst failed with code %lx", cr);
                 error_setg_win32(errp, GetLastError(),
                                  "failed to get device instance");
-                goto out;
+                goto cleanup;
             }
             cr = CM_Get_Parent(&parent_dev_inst, dev_inst, 0);
             if (cr != CR_SUCCESS) {
                 g_error("CM_Get_Parent failed with code %lx", cr);
                 error_setg_win32(errp, GetLastError(),
                                  "failed to get parent device instance");
-                goto out;
+                goto cleanup;
             }
 
             cr = CM_Get_Device_ID_Size(&dev_id_size, parent_dev_inst, 0);
@@ -632,7 +632,7 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
                 g_error("CM_Get_Device_ID_Size failed with code %lx", cr);
                 error_setg_win32(errp, GetLastError(),
                                  "failed to get parent device ID length");
-                goto out;
+                goto cleanup;
             }
 
             ++dev_id_size;
@@ -647,7 +647,7 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
                 g_error("CM_Get_Device_ID failed with code %lx", cr);
                 error_setg_win32(errp, GetLastError(),
                                  "failed to get parent device ID");
-                goto out;
+                goto cleanup;
             }
         }
 
@@ -661,14 +661,14 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
         if (parent_dev_info == INVALID_HANDLE_VALUE) {
             error_setg_win32(errp, GetLastError(),
                              "failed to get parent device");
-            goto out;
+            goto cleanup;
         }
 
         parent_dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
         if (!SetupDiEnumDeviceInfo(parent_dev_info, 0, &parent_dev_info_data)) {
             error_setg_win32(errp, GetLastError(),
                            "failed to get parent device data");
-            goto out;
+            goto cleanup;
         }
 
         for (j = 0;
@@ -748,11 +748,10 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
         break;
     }
 
-free_dev_info:
+cleanup:
     if (dev_info != INVALID_HANDLE_VALUE) {
         SetupDiDestroyDeviceInfoList(dev_info);
     }
-out:
     return pci;
 }
 
Signature · 1 line
2.25.1
#2 lore raw

RFC 3/4 gqa-win: get_pci_info: Add g_autofree for few variables

In reply to Kostiantyn Kostiuk
---
 qga/commands-win32.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
qga/commands-win32.c +2 -2
index 724ce76a0e..a8a601776d 100644
@@ -539,9 +539,9 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
     dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
     dev_iface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
     for (i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) {
-        PSP_DEVICE_INTERFACE_DETAIL_DATA pdev_iface_detail_data = NULL;
+        g_autofree PSP_DEVICE_INTERFACE_DETAIL_DATA pdev_iface_detail_data = NULL;
         STORAGE_DEVICE_NUMBER sdn;
-        char *parent_dev_id = NULL;
+        g_autofree char *parent_dev_id = NULL;
         HDEVINFO parent_dev_info;
         SP_DEVINFO_DATA parent_dev_info_data;
         DWORD j;
Signature · 1 line
2.25.1
#3 lore raw

Re: [RFC 3/4] gqa-win: get_pci_info: Add g_autofree for few variables

Hi Kostiantyn,

Quoted 1 line · Kostiantyn Kostiuk

I'm not sure what you are trying to do here, fix a memory leak?

Quoted 11 lines
---
 qga/commands-win32.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
qga/commands-win32.c +0 -0
index 724ce76a0e..a8a601776d 100644
@@ -539,9 +539,9 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
     dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);

qga/commands-win32.c static GuestPCIAddress *get_pci_info(int number, Error **errp) · line 547

Quoted 10 lines
qga/commands-win32.c +2 -2
     dev_iface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
     for (i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) {
-        PSP_DEVICE_INTERFACE_DETAIL_DATA pdev_iface_detail_data = NULL;
+        g_autofree PSP_DEVICE_INTERFACE_DETAIL_DATA pdev_iface_detail_data = NULL;
         STORAGE_DEVICE_NUMBER sdn;
-        char *parent_dev_id = NULL;
+        g_autofree char *parent_dev_id = NULL;
         HDEVINFO parent_dev_info;
         SP_DEVINFO_DATA parent_dev_info_data;
         DWORD j;

Anyhow this function is confuse.

I think it would be easier to review by replacing the while() by 2 calls, as suggested in the documentation:
https://docs.microsoft.com/en-us/windows/win32/api/setupapi/nf-setupapi-setupdigetdeviceinterfacedetaila

-- >8 --

qga/commands-win32.c +0 -0
index 7bac0c5d422..2188c5dd80d 100644
@@ -539,7 +539,6 @@ static GuestPCIAddress *get_pci_info(int number,

Error **errp)

     dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
     dev_iface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
     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;

@@ -551,25 +550,36 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
if (SetupDiEnumDeviceInterfaces(dev_info, &dev_info_data,
&GUID_DEVINTERFACE_DISK, 0, &dev_iface_data)) {
- while (!SetupDiGetDeviceInterfaceDetail(dev_info,
&dev_iface_data,

-                                                    pdev_iface_detail_data,
-                                                    size, &size,
-                                                    &dev_info_data)) {

- if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
- pdev_iface_detail_data = g_malloc(size);
- pdev_iface_detail_data->cbSize =
- sizeof(*pdev_iface_detail_data);
- } else {
- error_setg_win32(errp, GetLastError(),
- "failed to get device interfaces");
- goto free_dev_info;
- }
+ g_autofree PSP_DEVICE_INTERFACE_DETAIL_DATA
+ pdev_iface_detail_data = NULL;
+
+ /* Get the required buffer size. */
+ if (!SetupDiGetDeviceInterfaceDetail(dev_info, &dev_iface_data,
+ NULL, 0, &size,
+ &dev_info_data)
+ && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
+ error_setg_win32(errp, GetLastError(),
+ "failed to get device interfaces
buffer size");
+ goto free_dev_info;
+ }
+

+            /* Allocate an appropriately sized buffer. */
+            pdev_iface_detail_data = g_malloc(size);
+            pdev_iface_detail_data->cbSize =

sizeof(*pdev_iface_detail_data);
+
+ /* Get the interface details. */
+ if (!SetupDiGetDeviceInterfaceDetail(dev_info, &dev_iface_data,

+                                                 pdev_iface_detail_data,
+                                                 size, &size,
+                                                 &dev_info_data)) {
+                error_setg_win32(errp, GetLastError(),
+                                 "failed to get device interfaces");
+                goto free_dev_info;
             }

             dev_file = CreateFile(pdev_iface_detail_data->DevicePath, 0,
                                   FILE_SHARE_READ, NULL, OPEN_EXISTING, 0,
                                   NULL);

- g_free(pdev_iface_detail_data);

if (!DeviceIoControl(dev_file, IOCTL_STORAGE_GET_DEVICE_NUMBER,
NULL, 0, &sdn, sizeof(sdn), &size,
NULL)) {
---

#4 lore raw

Re: [RFC 3/4] gqa-win: get_pci_info: Add g_autofree for few variables

Hi Philippe,

Quoted 7 lines · Philippe Mathieu-Daudé

Hi Kostiantyn,

Quoted 1 line · Kostiantyn Kostiuk

I'm not sure what you are trying to do here, fix a memory leak?

Yes. This set of patches fix a memory leak. The leak occurs in the case when a storage device does not have a PCI parent.

Quoted 26 lines

qga/commands-win32.c static GuestPCIAddress *get_pci_info(int number,

Quoted 10 lines
---
 qga/commands-win32.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
qga/commands-win32.c +0 -0
index 724ce76a0e..a8a601776d 100644
@@ -539,9 +539,9 @@ static GuestPCIAddress *get_pci_info(int number,

Error **errp)

qga/commands-win32.c static GuestPCIAddress *get_pci_info(int number, · line 541

Quoted 3 lines
qga/commands-win32.c +0 -0
     dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
     dev_iface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
     for (i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data);

i++) {

qga/commands-win32.c static GuestPCIAddress *get_pci_info(int number, · line 542

Quoted 2 lines
qga/commands-win32.c +1 -1
-        PSP_DEVICE_INTERFACE_DETAIL_DATA pdev_iface_detail_data = NULL;
+        g_autofree PSP_DEVICE_INTERFACE_DETAIL_DATA

pdev_iface_detail_data = NULL;

qga/commands-win32.c static GuestPCIAddress *get_pci_info(int number, · line 547

Quoted 7 lines
qga/commands-win32.c +1 -1
         STORAGE_DEVICE_NUMBER sdn;
-        char *parent_dev_id = NULL;
+        g_autofree char *parent_dev_id = NULL;
         HDEVINFO parent_dev_info;
         SP_DEVINFO_DATA parent_dev_info_data;
         DWORD j;
Quoted 8 lines

Anyhow this function is confuse.

I think it would be easier to review by replacing the while() by 2 calls, as suggested in the documentation:

https://docs.microsoft.com/en-us/windows/win32/api/setupapi/nf-setupapi-setupdigetdeviceinterfacedetaila

Ok. I will refactor these patches.

Quoted 77 lines

-- >8 --

qga/commands-win32.c +0 -1
index 7bac0c5d422..2188c5dd80d 100644
@@ -539,7 +539,6 @@ static GuestPCIAddress *get_pci_info(int number,
 Error **errp)
     dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
     dev_iface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
     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;
qga/commands-win32.c +25 -14
@@ -551,25 +550,36 @@ static GuestPCIAddress *get_pci_info(int number,
 Error **errp)
         if (SetupDiEnumDeviceInterfaces(dev_info, &dev_info_data,
                                         &GUID_DEVINTERFACE_DISK, 0,
                                         &dev_iface_data)) {
-            while (!SetupDiGetDeviceInterfaceDetail(dev_info,
 &dev_iface_data,
-
 pdev_iface_detail_data,
-                                                    size, &size,
-                                                    &dev_info_data)) {
-                if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
-                    pdev_iface_detail_data = g_malloc(size);
-                    pdev_iface_detail_data->cbSize =
-                        sizeof(*pdev_iface_detail_data);
-                } else {
-                    error_setg_win32(errp, GetLastError(),
-                                     "failed to get device interfaces");
-                    goto free_dev_info;
-                }
+            g_autofree PSP_DEVICE_INTERFACE_DETAIL_DATA
+                       pdev_iface_detail_data = NULL;
+
+            /* Get the required buffer size. */
+            if (!SetupDiGetDeviceInterfaceDetail(dev_info,
 &dev_iface_data,
+                                                 NULL, 0, &size,
+                                                 &dev_info_data)
+                    && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
+                error_setg_win32(errp, GetLastError(),
+                                 "failed to get device interfaces
 buffer size");
+                goto free_dev_info;
+            }
+
+            /* Allocate an appropriately sized buffer. */
+            pdev_iface_detail_data = g_malloc(size);
+            pdev_iface_detail_data->cbSize =
 sizeof(*pdev_iface_detail_data);
+
+            /* Get the interface details. */
+            if (!SetupDiGetDeviceInterfaceDetail(dev_info,
 &dev_iface_data,
+                                                 pdev_iface_detail_data,
+                                                 size, &size,
+                                                 &dev_info_data)) {
+                error_setg_win32(errp, GetLastError(),
+                                 "failed to get device interfaces");
+                goto free_dev_info;
             }
 
             dev_file = CreateFile(pdev_iface_detail_data->DevicePath, 0,
                                   FILE_SHARE_READ, NULL, OPEN_EXISTING, 0,
                                   NULL);
-            g_free(pdev_iface_detail_data);
 
             if (!DeviceIoControl(dev_file,

IOCTL_STORAGE_GET_DEVICE_NUMBER,

                                  NULL, 0, &sdn, sizeof(sdn), &size,

NULL)) {
---

#5 lore raw

RFC 4/4 gqa-win: get_pci_info: Free parent_dev_info properly

In reply to Kostiantyn Kostiuk

In case when the function fails to get parent device data, the parent_dev_info variable will be initialized, but not freed.

---
 qga/commands-win32.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
qga/commands-win32.c +6 -1
index a8a601776d..520c520cb8 100644
@@ -515,6 +515,8 @@ DEFINE_GUID(GUID_DEVINTERFACE_STORAGEPORT,
 static GuestPCIAddress *get_pci_info(int number, Error **errp)
 {
     HDEVINFO dev_info = INVALID_HANDLE_VALUE;
+    HDEVINFO parent_dev_info = INVALID_HANDLE_VALUE;
+
     SP_DEVINFO_DATA dev_info_data;
     SP_DEVICE_INTERFACE_DATA dev_iface_data;
     HANDLE dev_file;
@@ -542,7 +544,6 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
         g_autofree PSP_DEVICE_INTERFACE_DETAIL_DATA pdev_iface_detail_data = NULL;
         STORAGE_DEVICE_NUMBER sdn;
         g_autofree char *parent_dev_id = NULL;
-        HDEVINFO parent_dev_info;
         SP_DEVINFO_DATA parent_dev_info_data;
         DWORD j;
         DWORD size = 0;
@@ -745,6 +746,7 @@ static GuestPCIAddress *get_pci_info(int number, Error **errp)
             }
         }
         SetupDiDestroyDeviceInfoList(parent_dev_info);
+        parent_dev_info = INVALID_HANDLE_VALUE;
         break;
     }
 
@@ -752,6 +754,9 @@ cleanup:
     if (dev_info != INVALID_HANDLE_VALUE) {
         SetupDiDestroyDeviceInfoList(dev_info);
     }
+    if (parent_dev_info != INVALID_HANDLE_VALUE) {
+        SetupDiDestroyDeviceInfoList(parent_dev_info);
+    }
     return pci;
 }
 
Signature · 1 line
2.25.1