Win32: fix possible crash when listing USB devices.

Make sure to handle if retrieving the device description ends up with a NULL
data buffer pointer. Also switch handling the retrieved string using QString.

Fixes a crash reported in the forums.

Change-Id: I6e95a411308e85656cd78ddcecb1bcee165864d0
This commit is contained in:
Dominik Riebeling 2015-06-07 22:25:54 +02:00
parent 8360937ac2
commit 4c4c645d82

View file

@ -432,7 +432,7 @@ QMap<uint32_t, QString> System::listUsbDevices(void)
DWORD buffersize = 0;
QString description;
// get device desriptor first
// get device descriptor first
// for some reason not doing so results in bad things (tm)
while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData,
SPDRP_DEVICEDESC, &data, (PBYTE)buffer, buffersize, &buffersize)) {
@ -445,6 +445,11 @@ QMap<uint32_t, QString> System::listUsbDevices(void)
break;
}
}
if(!buffer) {
LOG_WARNING() << "Got no device description"
<< "(SetupDiGetDeviceRegistryProperty), item" << i;
continue;
}
description = QString::fromWCharArray(buffer);
// now get the hardware id, which contains PID and VID.
@ -460,18 +465,19 @@ QMap<uint32_t, QString> System::listUsbDevices(void)
}
}
unsigned int vid, pid;
// convert buffer text to upper case to avoid depending on the case of
// the keys (W7 uses different casing than XP at least).
int len = _tcslen(buffer);
while(len--) buffer[len] = _totupper(buffer[len]);
if(_stscanf(buffer, _TEXT("USB\\VID_%x&PID_%x"), &vid, &pid) == 2) {
uint32_t id;
id = vid << 16 | pid;
usbids.insert(id, description);
LOG_INFO("USB VID: %04x, PID: %04x", vid, pid);
if(buffer) {
// convert buffer text to upper case to avoid depending on the case of
// the keys (W7 uses different casing than XP at least).
QString data = QString::fromWCharArray(buffer);
QRegExp rex("USB\\\\VID_([0-9a-fA-F]{4})&PID_([0-9a-fA-F]{4}).*");
if(rex.indexIn(data) >= 0) {
uint32_t id;
id = rex.cap(1).toUInt(0, 16) << 16 | rex.cap(2).toUInt(0, 16);
usbids.insert(id, description);
LOG_INFO() << "USB:" << QString("0x%1").arg(id, 8, 16);
}
free(buffer);
}
if(buffer) free(buffer);
}
SetupDiDestroyDeviceInfoList(deviceInfo);