summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDominik Riebeling <Dominik.Riebeling@gmail.com>2015-06-07 22:25:54 +0200
committerDominik Riebeling <Dominik.Riebeling@gmail.com>2015-06-07 22:25:54 +0200
commit4c4c645d828ab61f3eb8571f83dc1ac750317eb2 (patch)
tree364c4806d4da9f3b84f1e01f07af9720361e69fb
parent8360937ac255b871cd759fe6914a3803795e3128 (diff)
downloadrockbox-4c4c645.tar.gz
rockbox-4c4c645.zip
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
-rw-r--r--rbutil/rbutilqt/base/system.cpp30
1 files changed, 18 insertions, 12 deletions
diff --git a/rbutil/rbutilqt/base/system.cpp b/rbutil/rbutilqt/base/system.cpp
index c36c95b9a1..855d9e3b06 100644
--- a/rbutil/rbutilqt/base/system.cpp
+++ b/rbutil/rbutilqt/base/system.cpp
@@ -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);