From 775f1a042d83c238963651bdd08804c8d4efe113 Mon Sep 17 00:00:00 2001 From: Silent Date: Sun, 22 Oct 2023 13:22:00 +0200 Subject: [PATCH] DInputSource: Limit the amount of reported buttons and POV hats to what the data format supports Fixes a crash when the device reports more buttons than what DIJOYSTATE2 can handle. --- pcsx2/Input/DInputSource.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/pcsx2/Input/DInputSource.cpp b/pcsx2/Input/DInputSource.cpp index d85c3e4743..e3afb700e5 100644 --- a/pcsx2/Input/DInputSource.cpp +++ b/pcsx2/Input/DInputSource.cpp @@ -210,12 +210,6 @@ bool DInputSource::AddDevice(ControllerData& cd, const std::string& name) return false; } - if (caps.dwButtons == 0) - { - Console.Error("Ignoring device '%s' because it has no buttons (%u axes, %u POVs).", name.c_str(), caps.dwAxes, caps.dwPOVs); - return false; - } - static constexpr const u32 axis_offsets[] = {offsetof(DIJOYSTATE2, lX), offsetof(DIJOYSTATE2, lY), offsetof(DIJOYSTATE2, lZ), offsetof(DIJOYSTATE2, lRz), offsetof(DIJOYSTATE2, lRx), offsetof(DIJOYSTATE2, lRy), offsetof(DIJOYSTATE2, rglSlider[0]), offsetof(DIJOYSTATE2, rglSlider[1])}; @@ -246,8 +240,8 @@ bool DInputSource::AddDevice(ControllerData& cd, const std::string& name) if (hr != DI_OK) Console.Warning("GetDeviceState() for '%s' failed: %08X", name.c_str(), hr); - cd.num_buttons = caps.dwButtons; - cd.num_hats = caps.dwPOVs; + cd.num_buttons = std::min(caps.dwButtons, std::size(cd.last_state.rgbButtons)); + cd.num_hats = std::min(caps.dwPOVs, std::size(cd.last_state.rgdwPOV)); Console.WriteLn( "%s has %u buttons, %u axes, %u hats", name.c_str(), cd.num_buttons, static_cast(cd.axis_offsets.size()), cd.num_hats);