Browse Source

Make sure the window is valid in SDL_SetKeyboardFocus()

Sam Lantinga 2 years ago
parent
commit
e3d90c694c
2 changed files with 10 additions and 3 deletions
  1. 9 2
      src/events/SDL_keyboard.c
  2. 1 1
      src/events/SDL_keyboard_c.h

+ 9 - 2
src/events/SDL_keyboard.c

@@ -746,10 +746,17 @@ SDL_GetKeyboardFocus(void)
     return keyboard->focus;
 }
 
-void SDL_SetKeyboardFocus(SDL_Window *window)
+int SDL_SetKeyboardFocus(SDL_Window *window)
 {
+    SDL_VideoDevice *video = SDL_GetVideoDevice();
     SDL_Keyboard *keyboard = &SDL_keyboard;
 
+    if (window) {
+        if (!video || window->magic != &video->window_magic || window->is_destroying) {
+            return SDL_SetError("Invalid window");
+        }
+    }
+
     if (keyboard->focus && window == NULL) {
         /* We won't get anymore keyboard messages, so reset keyboard state */
         SDL_ResetKeyboard();
@@ -773,7 +780,6 @@ void SDL_SetKeyboardFocus(SDL_Window *window)
 
         /* Ensures IME compositions are committed */
         if (SDL_EventEnabled(SDL_EVENT_TEXT_INPUT)) {
-            SDL_VideoDevice *video = SDL_GetVideoDevice();
             if (video && video->StopTextInput) {
                 video->StopTextInput(video);
             }
@@ -793,6 +799,7 @@ void SDL_SetKeyboardFocus(SDL_Window *window)
             }
         }
     }
+    return 0;
 }
 
 static int SDL_SendKeyboardKeyInternal(Uint64 timestamp, SDL_KeyboardFlags flags, Uint8 state, SDL_Scancode scancode, SDL_Keycode keycode)

+ 1 - 1
src/events/SDL_keyboard_c.h

@@ -42,7 +42,7 @@ extern void SDL_SetKeymap(int start, const SDL_Keycode *keys, int length, SDL_bo
 extern void SDL_SetScancodeName(SDL_Scancode scancode, const char *name);
 
 /* Set the keyboard focus window */
-extern void SDL_SetKeyboardFocus(SDL_Window *window);
+extern int SDL_SetKeyboardFocus(SDL_Window *window);
 
 /* Send a character from an on-screen keyboard as scancode and modifier key events,
    currently assuming ASCII characters on a US keyboard layout