Commit 33a2b58ca492e2e1b64ed1a32f9225ed8e3e7516

Philipp Wiesemann 2014-10-26T23:22:53

Fixed SDL_GameControllerMappingForGUID() crashing if no more memory available. The return value of SDL_malloc() was not checked and NULL therefore not handled. NULL returned by SDL_GameControllerMapping()/SDL_GameControllerMappingForGUID() now either means "no mapping" (as before) or "no memory" (just crashed before).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index e995e2a..76b9b06 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -735,6 +735,10 @@ SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
         /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
         needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
         pMappingString = SDL_malloc(needed);
+        if (!pMappingString) {
+            SDL_OutOfMemory();
+            return NULL;
+        }
         SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
     }
     return pMappingString;