# SDL: Make sure the mapping string doesn't have extraneous whitespace

**URL:** <https://discourse.libsdl.org/t/sdl-make-sure-the-mapping-string-doesnt-have-extraneous-whitespace/44952>\
**Category:** SDL Commits\
**Created:** [July 18, 2023, 8:28pm UTC](https://discourse.libsdl.org/t/sdl-make-sure-the-mapping-string-doesnt-have-extraneous-whitespace/44952 "2023-07-18T20:28:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SDLPushPostBot](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sdlpushpostbot/32/4370_2.png) [@SDLPushPostBot](https://discourse.libsdl.org/u/SDLPushPostBot)\
**Post date:** [July 18, 2023, 8:28pm UTC](https://discourse.libsdl.org/t/sdl-make-sure-the-mapping-string-doesnt-have-extraneous-whitespace/44952/1 "2023-07-18T20:28:02Z")

</div>

> <https://github.com/libsdl-org/SDL/commit/7aec9ad4a6d6b5a9f330911e6d6ab2b19d5252bc>
>
> If that whitespace is extra newlines in the mapping hint, we would create extra …empty mapping strings

```diff
From 7aec9ad4a6d6b5a9f330911e6d6ab2b19d5252bc Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 18 Jul 2023 13:25:47 -0700
Subject: [PATCH] Make sure the mapping string doesn't have extraneous
 whitespace

If that whitespace is extra newlines in the mapping hint, we would create extra empty mapping strings
---
 src/joystick/SDL_gamepad.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/joystick/SDL_gamepad.c b/src/joystick/SDL_gamepad.c
index 25a850fef505..50660a6ec7f0 100644
--- a/src/joystick/SDL_gamepad.c
+++ b/src/joystick/SDL_gamepad.c
@@ -1401,6 +1401,8 @@ static char *SDL_PrivateGetGamepadNameFromMappingString(const char *pMapping)
 static char *SDL_PrivateGetGamepadMappingFromMappingString(const char *pMapping)
 {
     const char *pFirstComma, *pSecondComma;
+ char *result;
+ size_t length;
 
     pFirstComma = SDL_strchr(pMapping, ',');
     if (pFirstComma == NULL) {
@@ -1412,7 +1414,24 @@ static char *SDL_PrivateGetGamepadMappingFromMappingString(const char *pMapping)
         return NULL;
     }
 
- return SDL_strdup(pSecondComma + 1); /* mapping is everything after the 3rd comma */
+ /* Skip whitespace */
+ while (SDL_isspace(pSecondComma[1])) {
+ ++pSecondComma;
+ }
+ if (pSecondComma[1] == '\0') {
+ return NULL;
+ }
+
+ result = SDL_strdup(pSecondComma + 1); /* mapping is everything after the 3rd comma */
+
+ /* Trim whitespace */
+ length = SDL_strlen(result);
+ while (SDL_isspace(result[length - 1])) {
+ --length;
+ }
+ result[length] = '\0';
+
+ return result;
 }
 
 /*

```
