From 856a81f62ba1baf10f6c2d77acf146409d984b60 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sat, 23 Sep 2023 18:59:17 -0600 Subject: [PATCH] Assert against out of bounds array access in `sf::Joystick` sf::JoystickManager is a private type but the sf::Joystick API calls into it so those asserts are still prone to fail due to incorrect user input. Yay for catching more UB :) --- src/SFML/Window/Joystick.cpp | 3 +++ src/SFML/Window/JoystickManager.cpp | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/src/SFML/Window/Joystick.cpp b/src/SFML/Window/Joystick.cpp index ca6818635..9989e3e8a 100644 --- a/src/SFML/Window/Joystick.cpp +++ b/src/SFML/Window/Joystick.cpp @@ -28,6 +28,8 @@ #include #include +#include + namespace sf { @@ -55,6 +57,7 @@ bool Joystick::hasAxis(unsigned int joystick, Axis axis) //////////////////////////////////////////////////////////// bool Joystick::isButtonPressed(unsigned int joystick, unsigned int button) { + assert(button < Joystick::ButtonCount && "Button must be less than Joystick::ButtonCount"); return priv::JoystickManager::getInstance().getState(joystick).buttons[button]; } diff --git a/src/SFML/Window/JoystickManager.cpp b/src/SFML/Window/JoystickManager.cpp index 1719cd731..c6e5b3dab 100644 --- a/src/SFML/Window/JoystickManager.cpp +++ b/src/SFML/Window/JoystickManager.cpp @@ -27,6 +27,8 @@ //////////////////////////////////////////////////////////// #include +#include + namespace sf::priv { @@ -41,6 +43,7 @@ JoystickManager& JoystickManager::getInstance() //////////////////////////////////////////////////////////// const JoystickCaps& JoystickManager::getCapabilities(unsigned int joystick) const { + assert(joystick < Joystick::Count && "Joystick index must be less than Joystick::Count"); return m_joysticks[joystick].capabilities; } @@ -48,6 +51,7 @@ const JoystickCaps& JoystickManager::getCapabilities(unsigned int joystick) cons //////////////////////////////////////////////////////////// const JoystickState& JoystickManager::getState(unsigned int joystick) const { + assert(joystick < Joystick::Count && "Joystick index must be less than Joystick::Count"); return m_joysticks[joystick].state; } @@ -55,6 +59,7 @@ const JoystickState& JoystickManager::getState(unsigned int joystick) const //////////////////////////////////////////////////////////// const Joystick::Identification& JoystickManager::getIdentification(unsigned int joystick) const { + assert(joystick < Joystick::Count && "Joystick index must be less than Joystick::Count"); return m_joysticks[joystick].identification; }