drivelist: fix handling zero length wstrings

Ref #142
This commit is contained in:
Floris Bos 2021-01-06 12:18:09 +01:00
parent d129d12b00
commit 8e87933320

View file

@ -30,6 +30,7 @@
#include <stdint.h> #include <stdint.h>
#include <inttypes.h> #include <inttypes.h>
#include <nan.h> #include <nan.h>
#include <wchar.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include <set> #include <set>
@ -46,27 +47,26 @@ DEFINE_DEVPROPKEY(DEVPKEY_Device_EnumeratorName, 0xa45c254e,0xdf1c,0x4efd,0x80,0
namespace Drivelist { namespace Drivelist {
char* WCharToUtf8(const wchar_t* wstr) { std::string WCharToUtf8String(const wchar_t* wstr) {
char *str = NULL; if (!wstr) {
size_t size = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); return std::string();
if (size <= 1) {
return NULL;
} }
if ((str = reinterpret_cast<char*>(calloc(size, 1))) == NULL) { int wstrSize = (int) wcslen(wstr);
return NULL; int size = WideCharToMultiByte(
CP_UTF8, 0, wstr, wstrSize, NULL, 0, NULL, NULL);
if (!size) {
return std::string();
} }
size_t utf8Size = WideCharToMultiByte( std::string result(size, 0);
CP_UTF8, 0, wstr, -1, str, size, NULL, NULL); int utf8Size = WideCharToMultiByte(
CP_UTF8, 0, wstr, wstrSize, &result[0], size, NULL, NULL);
if (utf8Size != size) { if (utf8Size != size) {
free(str); return std::string();
return NULL;
} }
return str; return result;
} }
char* GetEnumeratorName(HDEVINFO hDeviceInfo, SP_DEVINFO_DATA deviceInfoData) { char* GetEnumeratorName(HDEVINFO hDeviceInfo, SP_DEVINFO_DATA deviceInfoData) {
@ -96,7 +96,7 @@ std::string GetFriendlyName(HDEVINFO hDeviceInfo,
hDeviceInfo, &deviceInfoData, SPDRP_FRIENDLYNAME, hDeviceInfo, &deviceInfoData, SPDRP_FRIENDLYNAME,
NULL, (PBYTE) wbuffer, sizeof(wbuffer), NULL); NULL, (PBYTE) wbuffer, sizeof(wbuffer), NULL);
return hasFriendlyName ? WCharToUtf8(wbuffer) : std::string(""); return hasFriendlyName ? WCharToUtf8String(wbuffer) : std::string("");
} }
bool IsSCSIDevice(std::string enumeratorName) { bool IsSCSIDevice(std::string enumeratorName) {
@ -172,7 +172,7 @@ bool IsSystemDevice(HDEVINFO hDeviceInfo, DeviceDescriptor *device) {
folderId, 0, NULL, &folderPath); folderId, 0, NULL, &folderPath);
if (result == S_OK) { if (result == S_OK) {
std::string systemPath = WCharToUtf8(folderPath); std::string systemPath = WCharToUtf8String(folderPath);
// printf("systemPath %s\n", systemPath.c_str()); // printf("systemPath %s\n", systemPath.c_str());
for (std::string mountpoint : device->mountpoints) { for (std::string mountpoint : device->mountpoints) {
// printf("%s find %s\n", systemPath.c_str(), mountpoint.c_str()); // printf("%s find %s\n", systemPath.c_str(), mountpoint.c_str());