Update length after shortening string (thanks mayraud705!)

Closes https://github.com/libsdl-org/SDL/pull/4698
This commit is contained in:
Sam Lantinga 2021-10-01 15:11:25 -07:00
parent 77acd44f28
commit 9706feacae
1 changed files with 7 additions and 4 deletions
src/joystick

View File

@ -1757,8 +1757,13 @@ SDL_CreateJoystickName(Uint16 vendor, Uint16 product, const char *vendor_name, c
size_t prefixlen = SDL_strlen(replacements[i].prefix); size_t prefixlen = SDL_strlen(replacements[i].prefix);
if (SDL_strncasecmp(name, replacements[i].prefix, prefixlen) == 0) { if (SDL_strncasecmp(name, replacements[i].prefix, prefixlen) == 0) {
size_t replacementlen = SDL_strlen(replacements[i].replacement); size_t replacementlen = SDL_strlen(replacements[i].replacement);
SDL_memcpy(name, replacements[i].replacement, replacementlen); if (replacementlen <= prefixlen) {
SDL_memmove(name+replacementlen, name+prefixlen, (len-prefixlen+1)); SDL_memcpy(name, replacements[i].replacement, replacementlen);
SDL_memmove(name+replacementlen, name+prefixlen, (len-prefixlen)+1);
len -= (prefixlen - replacementlen);
} else {
/* FIXME: Need to handle the expand case by reallocating the string */
}
break; break;
} }
} }
@ -1768,11 +1773,9 @@ SDL_CreateJoystickName(Uint16 vendor, Uint16 product, const char *vendor_name, c
int matchlen = PrefixMatch(name, &name[i]); int matchlen = PrefixMatch(name, &name[i]);
if (matchlen > 0 && name[matchlen-1] == ' ') { if (matchlen > 0 && name[matchlen-1] == ' ') {
SDL_memmove(name, name+matchlen, len-matchlen+1); SDL_memmove(name, name+matchlen, len-matchlen+1);
len -= matchlen;
break; break;
} else if (matchlen > 0 && name[matchlen] == ' ') { } else if (matchlen > 0 && name[matchlen] == ' ') {
SDL_memmove(name, name+matchlen+1, len-matchlen); SDL_memmove(name, name+matchlen+1, len-matchlen);
len -= (matchlen + 1);
break; break;
} }
} }