Zebediah Figura : setupapi: Avoid unnecessary buffer allocation in SetupDiGetDeviceInstanceIdA().

Alexandre Julliard julliard at winehq.org
Wed Nov 28 14:11:48 CST 2018


Module: wine
Branch: master
Commit: 81d2b419b87214ead8e0b2d8dd696536b45d82aa
URL:    https://source.winehq.org/git/wine.git/?a=commit;h=81d2b419b87214ead8e0b2d8dd696536b45d82aa

Author: Zebediah Figura <z.figura12 at gmail.com>
Date:   Tue Nov 27 19:55:36 2018 -0600

setupapi: Avoid unnecessary buffer allocation in SetupDiGetDeviceInstanceIdA().

Signed-off-by: Zebediah Figura <z.figura12 at gmail.com>
Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 dlls/setupapi/devinst.c | 64 +++++++++++++------------------------------------
 1 file changed, 16 insertions(+), 48 deletions(-)

diff --git a/dlls/setupapi/devinst.c b/dlls/setupapi/devinst.c
index a9145ad..be7ce64 100644
--- a/dlls/setupapi/devinst.c
+++ b/dlls/setupapi/devinst.c
@@ -1607,57 +1607,25 @@ BOOL WINAPI SetupDiEnumDeviceInfo(HDEVINFO devinfo, DWORD index, SP_DEVINFO_DATA
 /***********************************************************************
  *		SetupDiGetDeviceInstanceIdA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInstanceIdA(
-	HDEVINFO DeviceInfoSet,
-	PSP_DEVINFO_DATA DeviceInfoData,
-	PSTR DeviceInstanceId,
-	DWORD DeviceInstanceIdSize,
-	PDWORD RequiredSize)
+BOOL WINAPI SetupDiGetDeviceInstanceIdA(HDEVINFO devinfo, SP_DEVINFO_DATA *device_data,
+        char *id, DWORD size, DWORD *needed)
 {
-    BOOL ret = FALSE;
-    DWORD size;
-    PWSTR instanceId;
-
-    TRACE("%p %p %p %d %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstanceId,
-	    DeviceInstanceIdSize, RequiredSize);
-
-    SetupDiGetDeviceInstanceIdW(DeviceInfoSet,
-                                DeviceInfoData,
-                                NULL,
-                                0,
-                                &size);
-    if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
+    WCHAR idW[MAX_DEVICE_ID_LEN];
+
+    TRACE("devinfo %p, device_data %p, id %p, size %d, needed %p.\n",
+            devinfo, device_data, id, size, needed);
+
+    if (!SetupDiGetDeviceInstanceIdW(devinfo, device_data, idW, ARRAY_SIZE(idW), NULL))
         return FALSE;
-    instanceId = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
-    if (instanceId)
-    {
-        ret = SetupDiGetDeviceInstanceIdW(DeviceInfoSet,
-                                          DeviceInfoData,
-                                          instanceId,
-                                          size,
-                                          &size);
-        if (ret)
-        {
-            int len = WideCharToMultiByte(CP_ACP, 0, instanceId, -1,
-                                          DeviceInstanceId,
-                                          DeviceInstanceIdSize, NULL, NULL);
 
-            if (!len)
-                ret = FALSE;
-            else
-            {
-                if (len > DeviceInstanceIdSize)
-                {
-                    SetLastError(ERROR_INSUFFICIENT_BUFFER);
-                    ret = FALSE;
-                }
-                if (RequiredSize)
-                    *RequiredSize = len;
-            }
-        }
-        HeapFree(GetProcessHeap(), 0, instanceId);
-    }
-    return ret;
+    if (needed)
+        *needed = WideCharToMultiByte(CP_ACP, 0, idW, -1, NULL, 0, NULL, NULL);
+
+    if (size && WideCharToMultiByte(CP_ACP, 0, idW, -1, id, size, NULL, NULL))
+        return TRUE;
+
+    SetLastError(ERROR_INSUFFICIENT_BUFFER);
+    return FALSE;
 }
 
 /***********************************************************************




More information about the wine-cvs mailing list