From c47c79e77152a8853e9687fdb148662a1f266d49 Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 5 Jul 2024 16:00:16 +0200 Subject: [PATCH] Make sure to only access valid buffers in VPADRead and WPADRead hooks --- source/patcher/hooks_patcher_static.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/source/patcher/hooks_patcher_static.cpp b/source/patcher/hooks_patcher_static.cpp index 5e9a84a..8435faa 100644 --- a/source/patcher/hooks_patcher_static.cpp +++ b/source/patcher/hooks_patcher_static.cpp @@ -80,20 +80,25 @@ DECL_FUNCTION(void, OSReleaseForeground) { real_OSReleaseForeground(); } -DECL_FUNCTION(int32_t, VPADRead, int32_t chan, VPADStatus *buffer, uint32_t buffer_size, int32_t *error) { +DECL_FUNCTION(int32_t, VPADRead, int32_t chan, VPADStatus *buffer, uint32_t buffer_size, VPADReadError *error) { if (sConfigMenuOpened) { // Ignore reading vpad input only from other threads if the config menu is opened if (OSGetCurrentThread() != gOnlyAcceptFromThread) { return 0; } } - int32_t result = real_VPADRead(chan, buffer, buffer_size, error); + VPADReadError real_error = VPAD_READ_SUCCESS; + int32_t result = real_VPADRead(chan, buffer, buffer_size, &real_error); + + if (result > 0 && real_error == VPAD_READ_SUCCESS && buffer && ((buffer[0].hold & 0xFFFFF) == (VPAD_BUTTON_L | VPAD_BUTTON_DOWN | VPAD_BUTTON_MINUS)) && sVpadPressCooldown == 0 && !sConfigMenuOpened) { - if (result > 0 && ((buffer[0].hold & 0xFFFFF) == (VPAD_BUTTON_L | VPAD_BUTTON_DOWN | VPAD_BUTTON_MINUS)) && sVpadPressCooldown == 0 && !sConfigMenuOpened) { sWantsToOpenConfigMenu = true; sVpadPressCooldown = 0x3C; return 0; } + if (error) { + *error = real_error; + } if (sVpadPressCooldown > 0) { sVpadPressCooldown--; @@ -104,8 +109,7 @@ DECL_FUNCTION(int32_t, VPADRead, int32_t chan, VPADStatus *buffer, uint32_t buff DECL_FUNCTION(void, WPADRead, WPADChan chan, WPADStatusProController *data) { real_WPADRead(chan, data); - - if (!sConfigMenuOpened && data[0].err == 0) { + if (!sConfigMenuOpened && data && data[0].err == 0) { if (data[0].extensionType != 0xFF) { if (data[0].extensionType == WPAD_EXT_CORE || data[0].extensionType == WPAD_EXT_NUNCHUK) { // button data is in the first 2 bytes for wiimotes