SDL: Make sure we match the same way when adding a mapping and looking up a mapping (903df)

From 903df79b2db1bca931ef4ec5499dd31125a2364c Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 27 Dec 2022 12:14:43 -0800
Subject: [PATCH] Make sure we match the same way when adding a mapping and
 looking up a mapping

Fixes https://github.com/libsdl-org/SDL/issues/6898

(cherry picked from commit 3db9112ef4cbb936b11f941f8a98c1704e37e140)
---
 src/joystick/SDL_gamecontroller.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index 3c6a6fad2794..97767358980c 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -753,9 +753,12 @@ static ControllerMapping_t *SDL_PrivateMatchControllerMappingForGUID(SDL_Joystic
 
         if (SDL_memcmp(&guid, &mapping_guid, sizeof(guid)) == 0) {
             Uint16 mapping_crc = 0;
-            const char *crc_string = SDL_strstr(mapping->mapping, SDL_CONTROLLER_CRC_FIELD);
-            if (crc_string) {
-                mapping_crc = (Uint16)SDL_strtol(crc_string + SDL_CONTROLLER_CRC_FIELD_SIZE, NULL, 16);
+
+            if (match_crc) {
+                const char *crc_string = SDL_strstr(mapping->mapping, SDL_CONTROLLER_CRC_FIELD);
+                if (crc_string) {
+                    mapping_crc = (Uint16)SDL_strtol(crc_string + SDL_CONTROLLER_CRC_FIELD_SIZE, NULL, 16);
+                }
             }
             if (crc == mapping_crc) {
                 return mapping;
@@ -768,7 +771,7 @@ static ControllerMapping_t *SDL_PrivateMatchControllerMappingForGUID(SDL_Joystic
 /*
  * Helper function to scan the mappings database for a controller with the specified GUID
  */
-static ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID guid)
+static ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID guid, SDL_bool create_mapping)
 {
     ControllerMapping_t *mapping;
     Uint16 vendor, product, crc;
@@ -803,6 +806,10 @@ static ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickG
         }
     }
 
+    if (!create_mapping) {
+        return NULL;
+    }
+
 #if SDL_JOYSTICK_XINPUT
     if (SDL_IsJoystickXInput(guid)) {
         /* This is an XInput device */
@@ -1271,7 +1278,7 @@ static ControllerMapping_t *SDL_PrivateAddMappingForGUID(SDL_JoystickGUID jGUID,
         }
     }
 
-    pControllerMapping = SDL_PrivateMatchControllerMappingForGUID(jGUID, SDL_TRUE, SDL_TRUE);
+    pControllerMapping = SDL_PrivateGetControllerMappingForGUID(jGUID, SDL_FALSE);
     if (pControllerMapping) {
         /* Only overwrite the mapping if the priority is the same or higher. */
         if (pControllerMapping->priority <= priority) {
@@ -1333,7 +1340,7 @@ static ControllerMapping_t *SDL_PrivateGetControllerMappingForNameAndGUID(const
 
     SDL_AssertJoysticksLocked();
 
-    mapping = SDL_PrivateGetControllerMappingForGUID(guid);
+    mapping = SDL_PrivateGetControllerMappingForGUID(guid, SDL_TRUE);
 #ifdef __LINUX__
     if (mapping == NULL && name) {
         if (SDL_strstr(name, "Xbox 360 Wireless Receiver")) {
@@ -1770,7 +1777,7 @@ char *SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
 
     SDL_LockJoysticks();
     {
-        ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(guid);
+        ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(guid, SDL_TRUE);
         if (mapping) {
             retval = CreateMappingString(mapping, guid);
         } else {