Commit b6a3d76225b7c4d7d2ef11dd576fce6daa56dbce

Sam Lantinga 2022-08-23T07:24:21

Look up mappings by GUID in two passes: first with CRC, second without

diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index 46bbac4..b127ee7 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -663,21 +663,26 @@ static ControllerMapping_t *SDL_CreateMappingForWGIController(SDL_JoystickGUID g
  */
 static ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID guid, SDL_bool exact_match)
 {
-    ControllerMapping_t *mapping = s_pSupportedControllers;
+    ControllerMapping_t *mapping;
     SDL_JoystickGUID zero_crc_guid;
 
+    /* First check for exact match */
+    for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
+        if (SDL_memcmp(&guid, &mapping->guid, sizeof(guid)) == 0) {
+            return mapping;
+        }
+    }
+
+    /* Now check for match with no CRC */
     SDL_memcpy(&zero_crc_guid, &guid, sizeof(guid));
     zero_crc_guid.data[3] = 0;
     zero_crc_guid.data[4] = 0;
     zero_crc_guid.data[5] = 0;
     zero_crc_guid.data[6] = 0;
-
-    while (mapping) {
-        if (SDL_memcmp(&guid, &mapping->guid, sizeof(guid)) == 0 ||
-            SDL_memcmp(&zero_crc_guid, &mapping->guid, sizeof(guid)) == 0) {
+    for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
+        if (SDL_memcmp(&zero_crc_guid, &mapping->guid, sizeof(guid)) == 0) {
             return mapping;
         }
-        mapping = mapping->next;
     }
 
     if (!exact_match) {