From 4c4c645d828ab61f3eb8571f83dc1ac750317eb2 Mon Sep 17 00:00:00 2001 From: Dominik Riebeling Date: Sun, 7 Jun 2015 22:25:54 +0200 Subject: 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 --- rbutil/rbutilqt/base/system.cpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'rbutil/rbutilqt/base/system.cpp') 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 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 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 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); -- cgit v1.2.3