From 0cd5b08bfee49a40de8d41e8d16b356b43dbf612 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 1 Aug 2022 09:43:15 -0700 Subject: [PATCH] Fixed infinite loop when SDL_SendKeyboardText() is passed invalid UTF-8 text --- src/events/SDL_keyboard.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c index 2ec22853f..f83191251 100644 --- a/src/events/SDL_keyboard.c +++ b/src/events/SDL_keyboard.c @@ -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); } }