Fixes an out of bounds joystick array access

Testing with an Xbox Series X controller. The extra button they
added directly under the Xbox button emits 0xA7 for me, which is
lower than BTN_MISC. As a result, when pressed, handleKeyEvent
was indexing an array with a negative index.

This commit adds a bounds check preventing the negative index, and
adds a second bounds check to a similar function preemptively.

It's possible that the better fix for this is to extend the range of
the buttons array so that this button can be mapped, but that may
need some disucssion first.
This commit is contained in:
Mason Remaley 2024-07-05 20:17:02 -07:00
parent b35641f4a3
commit 5e1ec2444f

View File

@ -49,6 +49,7 @@
// //
static void handleKeyEvent(_GLFWjoystick* js, int code, int value) static void handleKeyEvent(_GLFWjoystick* js, int code, int value)
{ {
if (code < BTN_MISC || code >= KEY_CNT - BTN_MISC) return;
_glfwInputJoystickButton(js, _glfwInputJoystickButton(js,
js->linjs.keyMap[code - BTN_MISC], js->linjs.keyMap[code - BTN_MISC],
value ? GLFW_PRESS : GLFW_RELEASE); value ? GLFW_PRESS : GLFW_RELEASE);
@ -58,6 +59,7 @@ static void handleKeyEvent(_GLFWjoystick* js, int code, int value)
// //
static void handleAbsEvent(_GLFWjoystick* js, int code, int value) static void handleAbsEvent(_GLFWjoystick* js, int code, int value)
{ {
if (code >= ABS_CNT) return;
const int index = js->linjs.absMap[code]; const int index = js->linjs.absMap[code];
if (code >= ABS_HAT0X && code <= ABS_HAT3Y) if (code >= ABS_HAT0X && code <= ABS_HAT3Y)