raw_mouse: Fix button map out of bounds access

- Reload the mouse config when the index changes.
- Clear the button map before reloading the config.
- Check if the button really is in the map.
This commit is contained in:
Megamouse 2024-07-30 21:11:01 +02:00
parent 30f49a61df
commit 56b6b7e732
2 changed files with 17 additions and 6 deletions

View File

@ -45,6 +45,8 @@ raw_mouse::~raw_mouse()
void raw_mouse::reload_config()
{
m_buttons.clear();
if (m_index < ::size32(g_cfg_raw_mouse.players))
{
if (const auto& player = ::at32(g_cfg_raw_mouse.players, m_index))
@ -63,6 +65,12 @@ void raw_mouse::reload_config()
}
}
void raw_mouse::set_index(u32 index)
{
m_index = index;
reload_requested = true;
}
std::pair<int, int> raw_mouse::get_mouse_button(const cfg::string& button)
{
const std::string value = button.to_string();
@ -131,7 +139,10 @@ void raw_mouse::update_values(const RAWMOUSE& state)
const auto get_button_pressed = [this](u8 button, int button_flags)
{
const auto& [down, up] = ::at32(m_buttons, button);
const auto it = m_buttons.find(button);
if (it == m_buttons.cend()) return;
const auto& [down, up] = it->second;
// Only update the value if either down or up flags are present
if ((button_flags & down))
@ -328,12 +339,12 @@ void raw_mouse_handler::update_devices()
const std::string device_name = player->device.to_string();
// Check if the configured device for this player is connected
if (auto it = std::find_if(enumerated.begin(), enumerated.end(), [&device_name](const auto& entry){ return entry.second.device_name() == device_name; });
it != enumerated.end())
if (const auto it = std::find_if(enumerated.begin(), enumerated.end(), [&device_name](const auto& entry){ return entry.second.device_name() == device_name; });
it != enumerated.cend())
{
// Check if the device was already known
auto it_exists = m_raw_mice.find(it->first);
const bool exists = it_exists != m_raw_mice.end();
const auto it_exists = m_raw_mice.find(it->first);
const bool exists = it_exists != m_raw_mice.cend();
// Copy by value to allow for the same device for multiple players
raw_mouse& mouse = updated_mice[it->first];

View File

@ -42,7 +42,7 @@ public:
const std::string& device_name() const { return m_device_name; }
u32 index() const { return m_index; }
void set_index(u32 index) { m_index = index; }
void set_index(u32 index);
void request_reload() { reload_requested = true; }
private: