From 4912202cfa27aba7e02b43394b1934d0bcf7998d Mon Sep 17 00:00:00 2001 From: Megamouse Date: Sun, 5 Jun 2022 14:35:58 +0200 Subject: [PATCH] HID: zeroize buffers before getting a report, use std::array --- rpcs3/Input/ds3_pad_handler.cpp | 11 ++++++----- rpcs3/Input/ds4_pad_handler.cpp | 7 +++++-- rpcs3/Input/dualsense_pad_handler.cpp | 1 + 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/rpcs3/Input/ds3_pad_handler.cpp b/rpcs3/Input/ds3_pad_handler.cpp index eea5edaf3c..f61256320e 100644 --- a/rpcs3/Input/ds3_pad_handler.cpp +++ b/rpcs3/Input/ds3_pad_handler.cpp @@ -39,7 +39,7 @@ struct ds3_output_report ds3_led led_5; // reserved for another LED }; -constexpr u8 battery_capacity[] = {0, 1, 25, 50, 75, 100}; +constexpr std::array battery_capacity = {0, 1, 25, 50, 75, 100}; constexpr id_pair SONY_DS3_ID_0 = {0x054C, 0x0268}; @@ -275,15 +275,16 @@ void ds3_pad_handler::check_add_device(hid_device* hidDevice, std::string_view p // Uses libusb for windows as hidapi will never work with UsbHid driver for the ds3 and it won't work with WinUsb either(windows hid api needs the UsbHid in the driver stack as far as I can tell) // For other os use hidapi and hope for the best! #ifdef _WIN32 - u8 buf[0xFF]; + std::array buf{}; buf[0] = 0xF2; - int res = hid_get_feature_report(hidDevice, buf, 0xFF); + int res = hid_get_feature_report(hidDevice, buf.data(), 0xFF); if (res < 0) { ds3_log.warning("check_add_device: hid_get_feature_report 0xF2 failed! Trying again with 0x0. (result=%d, error=%s)", res, hid_error(hidDevice)); + buf = {}; buf[0] = 0; - res = hid_get_feature_report(hidDevice, buf, 0xFF); + res = hid_get_feature_report(hidDevice, buf.data(), 0xFF); } if (res < 0) { @@ -354,7 +355,7 @@ ds3_pad_handler::DataStatus ds3_pad_handler::get_data(ds3_device* ds3dev) } else { - ds3dev->battery_level = battery_capacity[std::min(battery_status, 5)]; + ds3dev->battery_level = battery_capacity.at(std::min(battery_status, battery_capacity.size() - 1)); ds3dev->cable_state = 0; } diff --git a/rpcs3/Input/ds4_pad_handler.cpp b/rpcs3/Input/ds4_pad_handler.cpp index 3f459a81ad..eb57cc4001 100644 --- a/rpcs3/Input/ds4_pad_handler.cpp +++ b/rpcs3/Input/ds4_pad_handler.cpp @@ -377,11 +377,13 @@ bool ds4_pad_handler::GetCalibrationData(DS4Device* ds4Dev) const return false; } - std::array buf; + std::array buf{}; + if (ds4Dev->bt_controller) { for (int tries = 0; tries < 3; ++tries) { + buf = {}; buf[0] = 0x05; if (int res = hid_get_feature_report(ds4Dev->hidDevice, buf.data(), DS4_FEATURE_REPORT_0x05_SIZE); res <= 0) @@ -541,8 +543,8 @@ void ds4_pad_handler::check_add_device(hid_device* hidDevice, std::string_view p ds4_log.warning("check_add_device: DS4 controller may not be genuine. Workaround enabled."); // Read feature report 0x12 instead which is what the console uses. + buf = {}; buf[0] = 0x12; - buf[1] = 0; if (res = hid_get_feature_report(hidDevice, buf.data(), DS4_FEATURE_REPORT_0x12_SIZE); res < 0) { ds4_log.error("check_add_device: hid_get_feature_report 0x12 failed! result=%d, error=%s", res, hid_error(hidDevice)); @@ -572,6 +574,7 @@ void ds4_pad_handler::check_add_device(hid_device* hidDevice, std::string_view p u32 hw_version{}; u32 fw_version{}; + buf = {}; buf[0] = 0xA3; res = hid_get_feature_report(hidDevice, buf.data(), DS4_FEATURE_REPORT_0xA3_SIZE); diff --git a/rpcs3/Input/dualsense_pad_handler.cpp b/rpcs3/Input/dualsense_pad_handler.cpp index c2d733fa11..e7b517e781 100644 --- a/rpcs3/Input/dualsense_pad_handler.cpp +++ b/rpcs3/Input/dualsense_pad_handler.cpp @@ -424,6 +424,7 @@ bool dualsense_pad_handler::get_calibration_data(DualSenseDevice* dualsense_devi { for (int tries = 0; tries < 3; ++tries) { + buf = {}; buf[0] = 0x05; if (int res = hid_get_feature_report(dualsense_device->hidDevice, buf.data(), DUALSENSE_CALIBRATION_REPORT_SIZE); res <= 0)