Signed-off-by: Zebediah Figura z.figura12@gmail.com --- dlls/setupapi/devinst.c | 14 +++++++++----- dlls/setupapi/tests/devinst.c | 20 +++++++++++++++++++- 2 files changed, 28 insertions(+), 6 deletions(-)
diff --git a/dlls/setupapi/devinst.c b/dlls/setupapi/devinst.c index 64c04131b0..8ce1344cda 100644 --- a/dlls/setupapi/devinst.c +++ b/dlls/setupapi/devinst.c @@ -1337,7 +1337,7 @@ HKEY WINAPI SetupDiCreateDevRegKeyW(HDEVINFO devinfo, SP_DEVINFO_DATA *device_da /*********************************************************************** * SetupDiCreateDeviceInfoA (SETUPAPI.@) */ -BOOL WINAPI SetupDiCreateDeviceInfoA(HDEVINFO DeviceInfoSet, PCSTR DeviceName, +BOOL WINAPI SetupDiCreateDeviceInfoA(HDEVINFO DeviceInfoSet, const char *name, const GUID *ClassGuid, PCSTR DeviceDescription, HWND hwndParent, DWORD CreationFlags, PSP_DEVINFO_DATA DeviceInfoData) { @@ -1345,11 +1345,15 @@ BOOL WINAPI SetupDiCreateDeviceInfoA(HDEVINFO DeviceInfoSet, PCSTR DeviceName, LPWSTR DeviceNameW = NULL; LPWSTR DeviceDescriptionW = NULL;
- if (DeviceName) + if (!name || strlen(name) >= MAX_DEVICE_ID_LEN) { - DeviceNameW = MultiByteToUnicode(DeviceName, CP_ACP); - if (DeviceNameW == NULL) return FALSE; + SetLastError(ERROR_INVALID_DEVINST_NAME); + return FALSE; } + + DeviceNameW = MultiByteToUnicode(name, CP_ACP); + if (DeviceNameW == NULL) return FALSE; + if (DeviceDescription) { DeviceDescriptionW = MultiByteToUnicode(DeviceDescription, CP_ACP); @@ -1407,7 +1411,7 @@ BOOL WINAPI SetupDiCreateDeviceInfoW(HDEVINFO devinfo, PCWSTR DeviceName, devinfo, debugstr_w(DeviceName), debugstr_guid(ClassGuid), debugstr_w(DeviceDescription), hwndParent, CreationFlags, device_data);
- if (!DeviceName) + if (!DeviceName || strlenW(DeviceName) >= MAX_DEVICE_ID_LEN) { SetLastError(ERROR_INVALID_DEVINST_NAME); return FALSE; diff --git a/dlls/setupapi/tests/devinst.c b/dlls/setupapi/tests/devinst.c index 0e6a1c0a4e..338393e5f2 100644 --- a/dlls/setupapi/tests/devinst.c +++ b/dlls/setupapi/tests/devinst.c @@ -271,8 +271,8 @@ static void test_device_info(void) { static const GUID deadbeef = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}}; SP_DEVINFO_DATA device = {0}, ret_device = {sizeof(ret_device)}; + char id[MAX_DEVICE_ID_LEN + 2]; HDEVINFO set; - char id[50]; BOOL ret;
SetLastError(0xdeadbeef); @@ -382,6 +382,24 @@ todo_wine { check_device_info(set, 2, &guid, "ROOT\LEGACY_BOGUS\testguid"); check_device_info(set, 3, NULL, NULL);
+ memset(id, 'x', sizeof(id)); + memcpy(id, "Root\LEGACY_BOGUS\", strlen("Root\LEGACY_BOGUS\")); + id[MAX_DEVICE_ID_LEN + 1] = 0; + SetLastError(0xdeadbeef); + ret = SetupDiCreateDeviceInfoA(set, id, &guid, NULL, NULL, 0, NULL); + ok(!ret, "Expected failure.\n"); + ok(GetLastError() == ERROR_INVALID_DEVINST_NAME, "Got unexpected error %#x.\n", GetLastError()); + + id[MAX_DEVICE_ID_LEN] = 0; + SetLastError(0xdeadbeef); + ret = SetupDiCreateDeviceInfoA(set, id, &guid, NULL, NULL, 0, NULL); + ok(!ret, "Expected failure.\n"); + ok(GetLastError() == ERROR_INVALID_DEVINST_NAME, "Got unexpected error %#x.\n", GetLastError()); + + id[MAX_DEVICE_ID_LEN - 1] = 0; + ret = SetupDiCreateDeviceInfoA(set, id, &guid, NULL, NULL, 0, NULL); + ok(ret, "Failed to create device, error %#x.\n", GetLastError()); + SetupDiDestroyDeviceInfoList(set); }
Signed-off-by: Zebediah Figura z.figura12@gmail.com --- dlls/setupapi/devinst.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-)
diff --git a/dlls/setupapi/devinst.c b/dlls/setupapi/devinst.c index 8ce1344cda..a9145adc44 100644 --- a/dlls/setupapi/devinst.c +++ b/dlls/setupapi/devinst.c @@ -1341,8 +1341,8 @@ BOOL WINAPI SetupDiCreateDeviceInfoA(HDEVINFO DeviceInfoSet, const char *name, const GUID *ClassGuid, PCSTR DeviceDescription, HWND hwndParent, DWORD CreationFlags, PSP_DEVINFO_DATA DeviceInfoData) { + WCHAR nameW[MAX_DEVICE_ID_LEN]; BOOL ret = FALSE; - LPWSTR DeviceNameW = NULL; LPWSTR DeviceDescriptionW = NULL;
if (!name || strlen(name) >= MAX_DEVICE_ID_LEN) @@ -1351,23 +1351,18 @@ BOOL WINAPI SetupDiCreateDeviceInfoA(HDEVINFO DeviceInfoSet, const char *name, return FALSE; }
- DeviceNameW = MultiByteToUnicode(name, CP_ACP); - if (DeviceNameW == NULL) return FALSE; + MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, sizeof(nameW));
if (DeviceDescription) { DeviceDescriptionW = MultiByteToUnicode(DeviceDescription, CP_ACP); if (DeviceDescriptionW == NULL) - { - MyFree(DeviceNameW); return FALSE; - } }
- ret = SetupDiCreateDeviceInfoW(DeviceInfoSet, DeviceNameW, ClassGuid, DeviceDescriptionW, + ret = SetupDiCreateDeviceInfoW(DeviceInfoSet, nameW, ClassGuid, DeviceDescriptionW, hwndParent, CreationFlags, DeviceInfoData);
- MyFree(DeviceNameW); MyFree(DeviceDescriptionW);
return ret;
Signed-off-by: Zebediah Figura z.figura12@gmail.com --- 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 a9145adc44..be7ce641a6 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; }
/***********************************************************************
Signed-off-by: Zebediah Figura z.figura12@gmail.com --- dlls/setupapi/devinst.c | 132 +++++++++++++++++++++--------------------------- 1 file changed, 58 insertions(+), 74 deletions(-)
diff --git a/dlls/setupapi/devinst.c b/dlls/setupapi/devinst.c index be7ce641a6..ff7aca089b 100644 --- a/dlls/setupapi/devinst.c +++ b/dlls/setupapi/devinst.c @@ -1394,19 +1394,18 @@ static DWORD SETUPDI_DevNameToDevID(LPCWSTR devName) /*********************************************************************** * SetupDiCreateDeviceInfoW (SETUPAPI.@) */ -BOOL WINAPI SetupDiCreateDeviceInfoW(HDEVINFO devinfo, PCWSTR DeviceName, - const GUID *ClassGuid, PCWSTR DeviceDescription, HWND hwndParent, DWORD CreationFlags, - SP_DEVINFO_DATA *device_data) +BOOL WINAPI SetupDiCreateDeviceInfoW(HDEVINFO devinfo, const WCHAR *name, const GUID *class, + const WCHAR *description, HWND parent, DWORD flags, SP_DEVINFO_DATA *device_data) { + WCHAR id[MAX_DEVICE_ID_LEN]; struct DeviceInfoSet *set; - BOOL ret = FALSE, allocatedInstanceId = FALSE; - LPCWSTR instanceId = NULL; + struct device *device;
TRACE("devinfo %p, name %s, class %s, description %s, hwnd %p, flags %#x, device_data %p.\n", - devinfo, debugstr_w(DeviceName), debugstr_guid(ClassGuid), debugstr_w(DeviceDescription), - hwndParent, CreationFlags, device_data); + devinfo, debugstr_w(name), debugstr_guid(class), debugstr_w(description), + parent, flags, device_data);
- if (!DeviceName || strlenW(DeviceName) >= MAX_DEVICE_ID_LEN) + if (!name || strlenW(name) >= MAX_DEVICE_ID_LEN) { SetLastError(ERROR_INVALID_DEVINST_NAME); return FALSE; @@ -1415,105 +1414,90 @@ BOOL WINAPI SetupDiCreateDeviceInfoW(HDEVINFO devinfo, PCWSTR DeviceName, if (!(set = get_device_set(devinfo))) return FALSE;
- if (!ClassGuid) + if (!class) { SetLastError(ERROR_INVALID_PARAMETER); return FALSE; }
- if (!IsEqualGUID(&set->ClassGuid, &GUID_NULL) && - !IsEqualGUID(ClassGuid, &set->ClassGuid)) + if (!IsEqualGUID(&set->ClassGuid, &GUID_NULL) && !IsEqualGUID(class, &set->ClassGuid)) { SetLastError(ERROR_CLASS_MISMATCH); return FALSE; } - if ((CreationFlags & DICD_GENERATE_ID)) + if ((flags & DICD_GENERATE_ID)) { - if (strchrW(DeviceName, '\')) + static const WCHAR formatW[] = {'R','O','O','T','\','%','s','\','%','0','4','d',0}; + DWORD devId; + + if (strchrW(name, '\')) + { SetLastError(ERROR_INVALID_DEVINST_NAME); - else + return FALSE; + } + + if (set->cDevices) { - static const WCHAR newDeviceFmt[] = {'R','O','O','T','\','%','s', - '\','%','0','4','d',0}; - DWORD devId; + DWORD highestDevID = 0;
- if (set->cDevices) + LIST_FOR_EACH_ENTRY(device, &set->devices, struct device, entry) { - DWORD highestDevID = 0; - struct device *device; + const WCHAR *devName = strrchrW(device->instanceId, '\'); + DWORD id;
- LIST_FOR_EACH_ENTRY(device, &set->devices, struct device, entry) - { - const WCHAR *devName = strrchrW(device->instanceId, '\'); - DWORD id; - - if (devName) - devName++; - else - devName = device->instanceId; - id = SETUPDI_DevNameToDevID(devName); - if (id != 0xffffffff && id > highestDevID) - highestDevID = id; - } - devId = highestDevID + 1; - } - else - devId = 0; - /* 17 == lstrlenW(L"Root\") + lstrlenW("\") + 1 + %d max size */ - instanceId = HeapAlloc(GetProcessHeap(), 0, - (17 + lstrlenW(DeviceName)) * sizeof(WCHAR)); - if (instanceId) - { - sprintfW((LPWSTR)instanceId, newDeviceFmt, DeviceName, - devId); - allocatedInstanceId = TRUE; - ret = TRUE; + if (devName) + devName++; + else + devName = device->instanceId; + id = SETUPDI_DevNameToDevID(devName); + if (id != 0xffffffff && id > highestDevID) + highestDevID = id; } - else - ret = FALSE; + devId = highestDevID + 1; + } + else + devId = 0; + + if (snprintfW(id, ARRAY_SIZE(id), formatW, name, devId) == -1) + { + SetLastError(ERROR_INVALID_DEVINST_NAME); + return FALSE; } } else { - struct device *device; - - ret = TRUE; - instanceId = DeviceName; + strcpyW(id, name); LIST_FOR_EACH_ENTRY(device, &set->devices, struct device, entry) { - if (!lstrcmpiW(DeviceName, device->instanceId)) + if (!lstrcmpiW(name, device->instanceId)) { SetLastError(ERROR_DEVINST_ALREADY_EXISTS); - ret = FALSE; + return FALSE; } } } - if (ret) + + if (!(device = SETUPDI_CreateDeviceInfo(set, class, id, TRUE))) + return FALSE; + + if (description) { - struct device *device = NULL; + SETUPDI_SetDeviceRegistryPropertyW(device, SPDRP_DEVICEDESC, + (const BYTE *)description, lstrlenW(description) * sizeof(WCHAR)); + }
- if ((device = SETUPDI_CreateDeviceInfo(set, ClassGuid, instanceId, TRUE))) + if (device_data) + { + if (device_data->cbSize != sizeof(SP_DEVINFO_DATA)) { - if (DeviceDescription) - SETUPDI_SetDeviceRegistryPropertyW(device, SPDRP_DEVICEDESC, - (const BYTE *)DeviceDescription, - lstrlenW(DeviceDescription) * sizeof(WCHAR)); - if (device_data) - { - if (device_data->cbSize != sizeof(SP_DEVINFO_DATA)) - { - SetLastError(ERROR_INVALID_USER_BUFFER); - ret = FALSE; - } - else - copy_device_data(device_data, device); - } + SetLastError(ERROR_INVALID_USER_BUFFER); + return FALSE; } + else + copy_device_data(device_data, device); } - if (allocatedInstanceId) - HeapFree(GetProcessHeap(), 0, (LPWSTR)instanceId);
- return ret; + return TRUE; }
/***********************************************************************
Signed-off-by: Zebediah Figura z.figura12@gmail.com --- dlls/setupapi/devinst.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)
diff --git a/dlls/setupapi/devinst.c b/dlls/setupapi/devinst.c index ff7aca089b..55fca897c3 100644 --- a/dlls/setupapi/devinst.c +++ b/dlls/setupapi/devinst.c @@ -2083,6 +2083,7 @@ static void SETUPDI_EnumerateMatchingDeviceInstances(struct DeviceInfoSet *set, LPCWSTR enumerator, LPCWSTR deviceName, HKEY deviceKey, const GUID *class, DWORD flags) { + WCHAR id[MAX_DEVICE_ID_LEN]; DWORD i, len; WCHAR deviceInstance[MAX_PATH]; LONG l = ERROR_SUCCESS; @@ -2120,18 +2121,11 @@ static void SETUPDI_EnumerateMatchingDeviceInstances(struct DeviceInfoSet *set, { static const WCHAR fmt[] = {'%','s','\','%','s','\','%','s',0}; - LPWSTR instanceId;
- instanceId = HeapAlloc(GetProcessHeap(), 0, - (lstrlenW(enumerator) + lstrlenW(deviceName) + - lstrlenW(deviceInstance) + 3) * sizeof(WCHAR)); - if (instanceId) + if (snprintfW(id, ARRAY_SIZE(id), fmt, enumerator, + deviceName, deviceInstance) != -1) { - sprintfW(instanceId, fmt, enumerator, - deviceName, deviceInstance); - SETUPDI_CreateDeviceInfo(set, &deviceClass, - instanceId, FALSE); - HeapFree(GetProcessHeap(), 0, instanceId); + SETUPDI_CreateDeviceInfo(set, &deviceClass, id, FALSE); } } }