From 959c39133b792d13ec27616e7ade82b73adc2086 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Mon, 15 Jan 2024 15:17:32 +0100 Subject: [PATCH 1/4] InputCommon/SDL: Fix incorrect use of std::vector::assign() and check bounds. --- Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp index 7887075743..a8546d10e7 100644 --- a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp +++ b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp @@ -354,13 +354,16 @@ GameController::GameController(SDL_GameController* const gamecontroller, case SDL_CONTROLLER_BINDTYPE_NONE: return; case SDL_CONTROLLER_BINDTYPE_BUTTON: - is_button_mapped.assign(bind.value.button, true); + if (bind.value.button >= 0 && bind.value.button < n_legacy_buttons) + is_button_mapped[bind.value.button] = true; break; case SDL_CONTROLLER_BINDTYPE_AXIS: - is_axis_mapped.assign(bind.value.axis, true); + if (bind.value.axis >= 0 && bind.value.axis < n_legacy_axes) + is_axis_mapped[bind.value.axis] = true; break; case SDL_CONTROLLER_BINDTYPE_HAT: - is_hat_mapped.assign(bind.value.hat.hat, true); + if (bind.value.hat.hat >= 0 && bind.value.hat.hat < n_legacy_hats) + is_hat_mapped[bind.value.hat.hat] = true; break; } }; From d657ad5932fc678037aa98a32048e8d70cca6562 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Mon, 15 Jan 2024 15:18:38 +0100 Subject: [PATCH 2/4] InputCommon/SDL: Check for errors from SDL_JoystickNumButtons(), SDL_JoystickNumAxes(), SDL_JoystickNumHats(). --- .../ControllerInterface/SDL/SDL.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp index a8546d10e7..3240a74914 100644 --- a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp +++ b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp @@ -342,8 +342,26 @@ GameController::GameController(SDL_GameController* const gamecontroller, // If a Joystick Button has a GameController equivalent, don't detect it int n_legacy_buttons = SDL_JoystickNumButtons(joystick); + if (n_legacy_buttons < 0) + { + ERROR_LOG_FMT(CONTROLLERINTERFACE, "Error in SDL_JoystickNumButtons(): {}", SDL_GetError()); + n_legacy_buttons = 0; + } + int n_legacy_axes = SDL_JoystickNumAxes(joystick); + if (n_legacy_axes < 0) + { + ERROR_LOG_FMT(CONTROLLERINTERFACE, "Error in SDL_JoystickNumAxes(): {}", SDL_GetError()); + n_legacy_axes = 0; + } + int n_legacy_hats = SDL_JoystickNumHats(joystick); + if (n_legacy_hats < 0) + { + ERROR_LOG_FMT(CONTROLLERINTERFACE, "Error in SDL_JoystickNumHats(): {}", SDL_GetError()); + n_legacy_hats = 0; + } + std::vector is_button_mapped(n_legacy_buttons); std::vector is_axis_mapped(n_legacy_axes); std::vector is_hat_mapped(n_legacy_hats); From 5e6e61c723285feb5287c5816e80b56be7212fa8 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Mon, 15 Jan 2024 15:19:24 +0100 Subject: [PATCH 3/4] InputCommon/SDL: Avoid potential infinite loops from integer truncation. --- .../InputCommon/ControllerInterface/SDL/SDL.cpp | 6 +++--- .../InputCommon/ControllerInterface/SDL/SDL.h | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp index 3240a74914..edef47aeda 100644 --- a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp +++ b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp @@ -453,11 +453,11 @@ GameController::GameController(SDL_GameController* const gamecontroller, // Legacy inputs // Buttons - for (u8 i = 0; i != n_legacy_buttons; ++i) + for (int i = 0; i != n_legacy_buttons; ++i) AddInput(new LegacyButton(m_joystick, i, !is_button_mapped[i])); // Axes - for (u8 i = 0; i != n_legacy_axes; ++i) + for (int i = 0; i != n_legacy_axes; ++i) { // each axis gets a negative and a positive input instance associated with it AddAnalogInputs(new LegacyAxis(m_joystick, i, -32768, !is_axis_mapped[i]), @@ -465,7 +465,7 @@ GameController::GameController(SDL_GameController* const gamecontroller, } // Hats - for (u8 i = 0; i != n_legacy_hats; ++i) + for (int i = 0; i != n_legacy_hats; ++i) { // each hat gets 4 input instances associated with it, (up down left right) for (u8 d = 0; d != 4; ++d) diff --git a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.h b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.h index abcf41a478..ebc0d4cac0 100644 --- a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.h +++ b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.h @@ -50,7 +50,7 @@ private: { public: std::string GetName() const override; - LegacyButton(SDL_Joystick* js, u8 index, bool is_detectable) + LegacyButton(SDL_Joystick* js, int index, bool is_detectable) : m_js(js), m_index(index), m_is_detectable(is_detectable) { } @@ -67,8 +67,8 @@ private: { public: std::string GetName() const override; - LegacyAxis(SDL_Joystick* js, u8 index, Sint16 range, bool is_detectable) - : m_js(js), m_range(range), m_index(index), m_is_detectable(is_detectable) + LegacyAxis(SDL_Joystick* js, int index, s16 range, bool is_detectable) + : m_js(js), m_index(index), m_range(range), m_is_detectable(is_detectable) { } bool IsDetectable() const override { return m_is_detectable; } @@ -76,8 +76,8 @@ private: private: SDL_Joystick* const m_js; - const Sint16 m_range; - const u8 m_index; + const int m_index; + const s16 m_range; const bool m_is_detectable; }; @@ -85,8 +85,8 @@ private: { public: std::string GetName() const override; - LegacyHat(SDL_Joystick* js, u8 index, u8 direction, bool is_detectable) - : m_js(js), m_direction(direction), m_index(index), m_is_detectable(is_detectable) + LegacyHat(SDL_Joystick* js, int index, u8 direction, bool is_detectable) + : m_js(js), m_index(index), m_direction(direction), m_is_detectable(is_detectable) { } bool IsDetectable() const override { return m_is_detectable; } @@ -94,8 +94,8 @@ private: private: SDL_Joystick* const m_js; + const int m_index; const u8 m_direction; - const u8 m_index; const bool m_is_detectable; }; From c7d7ae49120bbc79797698a9f534a29cddcdbd98 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Mon, 15 Jan 2024 15:19:41 +0100 Subject: [PATCH 4/4] InputCommon/SDL: Code style fixes. --- .../ControllerInterface/SDL/SDL.cpp | 30 +++++++++---------- .../InputCommon/ControllerInterface/SDL/SDL.h | 7 ++--- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp index edef47aeda..28ee21d6d7 100644 --- a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp +++ b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.cpp @@ -338,7 +338,7 @@ GameController::GameController(SDL_GameController* const gamecontroller, name = SDL_GameControllerName(gamecontroller); else name = SDL_JoystickName(joystick); - m_name = name != NULL ? name : "Unknown"; + m_name = name != nullptr ? name : "Unknown"; // If a Joystick Button has a GameController equivalent, don't detect it int n_legacy_buttons = SDL_JoystickNumButtons(joystick); @@ -362,11 +362,11 @@ GameController::GameController(SDL_GameController* const gamecontroller, n_legacy_hats = 0; } - std::vector is_button_mapped(n_legacy_buttons); - std::vector is_axis_mapped(n_legacy_axes); - std::vector is_hat_mapped(n_legacy_hats); + std::vector is_button_mapped(static_cast(n_legacy_buttons), false); + std::vector is_axis_mapped(static_cast(n_legacy_axes), false); + std::vector is_hat_mapped(static_cast(n_legacy_hats), false); - auto RegisterMapping = [&](SDL_GameControllerButtonBind bind) { + const auto register_mapping = [&](const SDL_GameControllerButtonBind& bind) { switch (bind.bindType) { case SDL_CONTROLLER_BINDTYPE_NONE: @@ -397,7 +397,7 @@ GameController::GameController(SDL_GameController* const gamecontroller, if (SDL_GameControllerHasButton(m_gamecontroller, button)) { AddInput(new Button(gamecontroller, button)); - RegisterMapping(SDL_GameControllerGetBindForButton(gamecontroller, button)); + register_mapping(SDL_GameControllerGetBindForButton(gamecontroller, button)); } } @@ -418,7 +418,7 @@ GameController::GameController(SDL_GameController* const gamecontroller, { AddInput(new Axis(m_gamecontroller, 32767, axis)); } - RegisterMapping(SDL_GameControllerGetBindForAxis(gamecontroller, axis)); + register_mapping(SDL_GameControllerGetBindForAxis(gamecontroller, axis)); } } // Rumble @@ -430,8 +430,8 @@ GameController::GameController(SDL_GameController* const gamecontroller, } // Motion - auto AddSensor = [this](SDL_SensorType type, std::string_view name, - const SDLMotionAxisList& axes) { + const auto add_sensor = [this](SDL_SensorType type, std::string_view name, + const SDLMotionAxisList& axes) { if (SDL_GameControllerSetSensorEnabled(m_gamecontroller, type, SDL_TRUE) == 0) { for (const SDLMotionAxis& axis : axes) @@ -442,12 +442,12 @@ GameController::GameController(SDL_GameController* const gamecontroller, } }; - AddSensor(SDL_SENSOR_ACCEL, "Accel", SDL_AXES_ACCELEROMETER); - AddSensor(SDL_SENSOR_GYRO, "Gyro", SDL_AXES_GYRO); - AddSensor(SDL_SENSOR_ACCEL_L, "Accel L", SDL_AXES_ACCELEROMETER); - AddSensor(SDL_SENSOR_GYRO_L, "Gyro L", SDL_AXES_GYRO); - AddSensor(SDL_SENSOR_ACCEL_R, "Accel R", SDL_AXES_ACCELEROMETER); - AddSensor(SDL_SENSOR_GYRO_R, "Gyro R", SDL_AXES_GYRO); + add_sensor(SDL_SENSOR_ACCEL, "Accel", SDL_AXES_ACCELEROMETER); + add_sensor(SDL_SENSOR_GYRO, "Gyro", SDL_AXES_GYRO); + add_sensor(SDL_SENSOR_ACCEL_L, "Accel L", SDL_AXES_ACCELEROMETER); + add_sensor(SDL_SENSOR_GYRO_L, "Gyro L", SDL_AXES_GYRO); + add_sensor(SDL_SENSOR_ACCEL_R, "Accel R", SDL_AXES_ACCELEROMETER); + add_sensor(SDL_SENSOR_GYRO_R, "Gyro R", SDL_AXES_GYRO); } // Legacy inputs diff --git a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.h b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.h index ebc0d4cac0..1187553264 100644 --- a/Source/Core/InputCommon/ControllerInterface/SDL/SDL.h +++ b/Source/Core/InputCommon/ControllerInterface/SDL/SDL.h @@ -103,7 +103,7 @@ private: class Motor : public Output { public: - explicit Motor(SDL_GameController* gc) : m_gc(gc){}; + explicit Motor(SDL_GameController* gc) : m_gc(gc) {} std::string GetName() const override; void SetState(ControlState state) override; @@ -114,7 +114,7 @@ private: class MotorL : public Output { public: - explicit MotorL(SDL_GameController* gc) : m_gc(gc){}; + explicit MotorL(SDL_GameController* gc) : m_gc(gc) {} std::string GetName() const override; void SetState(ControlState state) override; @@ -122,11 +122,10 @@ private: SDL_GameController* const m_gc; }; - // Rumble class MotorR : public Output { public: - explicit MotorR(SDL_GameController* gc) : m_gc(gc){}; + explicit MotorR(SDL_GameController* gc) : m_gc(gc) {} std::string GetName() const override; void SetState(ControlState state) override;