Fixed infinite loop when SDL_SendKeyboardText() is passed invalid UTF-8 text

This commit is contained in:
Sam Lantinga 2022-08-01 09:43:15 -07:00
parent 954bbdf3ae
commit 0cd5b08bfe

View File

@ -914,12 +914,16 @@ SDL_SendKeyboardText(const char *text)
posted = 0;
if (SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) {
SDL_Event event;
size_t i = 0, length = SDL_strlen(text);
size_t pos = 0, advance, length = SDL_strlen(text);
event.text.type = SDL_TEXTINPUT;
event.text.windowID = keyboard->focus ? keyboard->focus->id : 0;
while (i < length) {
i += SDL_utf8strlcpy(event.text.text, text + i, SDL_arraysize(event.text.text));
while (pos < length) {
advance = SDL_utf8strlcpy(event.text.text, text + pos, SDL_arraysize(event.text.text));
if (!advance) {
break;
}
pos += advance;
posted |= (SDL_PushEvent(&event) > 0);
}
}