wiimotereal: add some error handling. replace malloc with make_unique

This commit is contained in:
Shawn Hoffman
2020-08-17 17:32:14 -07:00
parent 938fd4e438
commit 3ee4c53d65

View File

@ -418,6 +418,10 @@ int WriteToHandle(HANDLE& dev_handle, WinWriteMethod& method, const u8* buf, siz
{ {
OVERLAPPED hid_overlap_write = OVERLAPPED(); OVERLAPPED hid_overlap_write = OVERLAPPED();
hid_overlap_write.hEvent = CreateEvent(nullptr, true, false, nullptr); hid_overlap_write.hEvent = CreateEvent(nullptr, true, false, nullptr);
if (!hid_overlap_write.hEvent)
{
return 0;
}
DWORD written = 0; DWORD written = 0;
IOWrite(dev_handle, hid_overlap_write, method, buf, size, &written); IOWrite(dev_handle, hid_overlap_write, method, buf, size, &written);
@ -431,6 +435,10 @@ int ReadFromHandle(HANDLE& dev_handle, u8* buf)
{ {
OVERLAPPED hid_overlap_read = OVERLAPPED(); OVERLAPPED hid_overlap_read = OVERLAPPED();
hid_overlap_read.hEvent = CreateEvent(nullptr, true, false, nullptr); hid_overlap_read.hEvent = CreateEvent(nullptr, true, false, nullptr);
if (!hid_overlap_read.hEvent)
{
return 0;
}
const int read = IORead(dev_handle, hid_overlap_read, buf, 1); const int read = IORead(dev_handle, hid_overlap_read, buf, 1);
CloseHandle(hid_overlap_read.hEvent); CloseHandle(hid_overlap_read.hEvent);
return read; return read;
@ -533,7 +541,6 @@ void WiimoteScannerWindows::FindWiimotes(std::vector<Wiimote*>& found_wiimotes,
SP_DEVICE_INTERFACE_DATA device_data = {}; SP_DEVICE_INTERFACE_DATA device_data = {};
device_data.cbSize = sizeof(device_data); device_data.cbSize = sizeof(device_data);
PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data = nullptr;
for (int index = 0; for (int index = 0;
SetupDiEnumDeviceInterfaces(device_info, nullptr, &device_id, index, &device_data); ++index) SetupDiEnumDeviceInterfaces(device_info, nullptr, &device_id, index, &device_data); ++index)
@ -541,7 +548,8 @@ void WiimoteScannerWindows::FindWiimotes(std::vector<Wiimote*>& found_wiimotes,
// Get the size of the data block required // Get the size of the data block required
DWORD len; DWORD len;
SetupDiGetDeviceInterfaceDetail(device_info, &device_data, nullptr, 0, &len, nullptr); SetupDiGetDeviceInterfaceDetail(device_info, &device_data, nullptr, 0, &len, nullptr);
detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(len); auto detail_data_buf = std::make_unique<u8[]>(len);
auto detail_data = reinterpret_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA>(detail_data_buf.get());
detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
SP_DEVINFO_DATA device_info_data = {}; SP_DEVINFO_DATA device_info_data = {};
@ -558,7 +566,6 @@ void WiimoteScannerWindows::FindWiimotes(std::vector<Wiimote*>& found_wiimotes,
if (!IsNewWiimote(WStringToUTF8(device_path)) || !IsWiimote(device_path, write_method)) if (!IsNewWiimote(WStringToUTF8(device_path)) || !IsWiimote(device_path, write_method))
{ {
free(detail_data);
continue; continue;
} }
@ -568,8 +575,6 @@ void WiimoteScannerWindows::FindWiimotes(std::vector<Wiimote*>& found_wiimotes,
else else
found_wiimotes.push_back(wiimote); found_wiimotes.push_back(wiimote);
} }
free(detail_data);
} }
SetupDiDestroyDeviceInfoList(device_info); SetupDiDestroyDeviceInfoList(device_info);