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

From b6a3d76225b7c4d7d2ef11dd576fce6daa56dbce Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 23 Aug 2022 07:24:21 -0700
Subject: [PATCH] Look up mappings by GUID in two passes: first with CRC,
 second without

---
 src/joystick/SDL_gamecontroller.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index 46bbac486de..b127ee7355c 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) {