From ddfdb0ca7b46aaaf0c37e519430449236db9013e Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 26 Dec 2024 20:18:26 -0800
Subject: [PATCH] Don't overwrite non-CRC gamepad mapping with a new mapping
that specifies a CRC
Fixes https://github.com/libsdl-org/SDL/issues/11723
(cherry picked from commit 1413bfb1850c351a889af420cda97d35af21efdd)
---
src/joystick/SDL_gamecontroller.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index d3a4668a623b3..72529862a13dc 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -688,7 +688,7 @@ static ControllerMapping_t *SDL_CreateMappingForWGIController(SDL_JoystickGUID g
/*
* Helper function to scan the mappings database for a controller with the specified GUID
*/
-static ControllerMapping_t *SDL_PrivateMatchControllerMappingForGUID(SDL_JoystickGUID guid, SDL_bool match_version)
+static ControllerMapping_t *SDL_PrivateMatchControllerMappingForGUID(SDL_JoystickGUID guid, SDL_bool match_version, SDL_bool exact_match_crc)
{
ControllerMapping_t *mapping, *best_match = NULL;
Uint16 crc = 0;
@@ -728,9 +728,10 @@ static ControllerMapping_t *SDL_PrivateMatchControllerMappingForGUID(SDL_Joystic
/* An exact match, including CRC */
return mapping;
+ } else if (crc && exact_match_crc) {
+ return NULL;
}
-
if (!best_match) {
best_match = mapping;
}
@@ -746,7 +747,7 @@ static ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickG
{
ControllerMapping_t *mapping;
- mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_TRUE);
+ mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_TRUE, adding_mapping);
if (mapping) {
return mapping;
}
@@ -760,7 +761,7 @@ static ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickG
if (SDL_JoystickGUIDUsesVersion(guid)) {
/* Try again, ignoring the version */
- mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_FALSE);
+ mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_FALSE, SDL_FALSE);
if (mapping) {
return mapping;
}