Commit 0cd5b08bfee49a40de8d41e8d16b356b43dbf612

Sam Lantinga 2022-08-01T09:43:15

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

diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c
index 2ec2285..f831912 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);
         }
     }