From 67359073ca2672744dba8b257bad372e6c8a87af Mon Sep 17 00:00:00 2001 From: greymoth Date: Sun, 28 Jun 2026 04:06:49 +0900 Subject: [PATCH] [rcore] Add gamepad >= 0 lower-bound guard to six sibling functions (#5938) Commit 3edfe194 added a `gamepad >= 0` lower-bound check to GetGamepadAxisCount() and GetGamepadName(), but six sibling functions were left with only the upper-bound check (`gamepad < MAX_GAMEPADS`). A negative signed int passes that check and triggers out-of-bounds access on CORE.Input.Gamepad.ready[gamepad] and related arrays (UB in C). Apply the same `(gamepad >= 0) &&` guard added in 3edfe194 to: - IsGamepadAvailable - IsGamepadButtonPressed - IsGamepadButtonDown - IsGamepadButtonReleased - IsGamepadButtonUp - GetGamepadAxisMovement --- src/rcore.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/rcore.c b/src/rcore.c index 12de4141e..070b8c0c4 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -3926,7 +3926,7 @@ bool IsGamepadAvailable(int gamepad) { bool result = false; - if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad]) result = true; + if ((gamepad >= 0) && (gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad]) result = true; return result; } @@ -3946,7 +3946,7 @@ bool IsGamepadButtonPressed(int gamepad, int button) { bool pressed = false; - if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS)) + if ((gamepad >= 0) && (gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS)) { if ((CORE.Input.Gamepad.previousButtonState[gamepad][button] == 0) && (CORE.Input.Gamepad.currentButtonState[gamepad][button] == 1)) pressed = true; } @@ -3959,7 +3959,7 @@ bool IsGamepadButtonDown(int gamepad, int button) { bool down = false; - if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS)) + if ((gamepad >= 0) && (gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS)) { if (CORE.Input.Gamepad.currentButtonState[gamepad][button] == 1) down = true; } @@ -3972,7 +3972,7 @@ bool IsGamepadButtonReleased(int gamepad, int button) { bool released = false; - if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS)) + if ((gamepad >= 0) && (gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS)) { if ((CORE.Input.Gamepad.previousButtonState[gamepad][button] == 1) && (CORE.Input.Gamepad.currentButtonState[gamepad][button] == 0)) released = true; } @@ -3985,7 +3985,7 @@ bool IsGamepadButtonUp(int gamepad, int button) { bool up = false; - if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS)) + if ((gamepad >= 0) && (gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS)) { if (CORE.Input.Gamepad.currentButtonState[gamepad][button] == 0) up = true; } @@ -4015,7 +4015,7 @@ float GetGamepadAxisMovement(int gamepad, int axis) { float value = ((axis == GAMEPAD_AXIS_LEFT_TRIGGER) || (axis == GAMEPAD_AXIS_RIGHT_TRIGGER))? -1.0f : 0.0f; - if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (axis < MAX_GAMEPAD_AXES)) + if ((gamepad >= 0) && (gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (axis < MAX_GAMEPAD_AXES)) { float movement = (value < 0.0f)? CORE.Input.Gamepad.axisState[gamepad][axis] : fabsf(CORE.Input.Gamepad.axisState[gamepad][axis]);