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 :)
This commit is contained in:
Chris Thrasher 2023-09-23 18:59:17 -06:00
parent d6c18af926
commit 856a81f62b
2 changed files with 8 additions and 0 deletions

View File

@ -28,6 +28,8 @@
#include <SFML/Window/Joystick.hpp>
#include <SFML/Window/JoystickManager.hpp>
#include <cassert>
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];
}

View File

@ -27,6 +27,8 @@
////////////////////////////////////////////////////////////
#include <SFML/Window/JoystickManager.hpp>
#include <cassert>
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;
}