SDL: wayland: Ensure that a NULL internal structure isn't dereferenced when destroying a window

From 6d92de5d3a69ab4842a138da467ad972e7b8bfec Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Fri, 13 Sep 2024 11:38:48 -0400
Subject: [PATCH] wayland: Ensure that a NULL internal structure isn't
 dereferenced when destroying a window

In some cases, such as when recreating a window during renderer initialization, a failure can leave the window in a state where the internal structure has already been freed, but the higher level window object needs to be destroyed separately. Check that the internal handle is valid before attempting to access any data during destruction.

Allows for graceful failure instead of a crash during cleanup if renderer creation fails.
---
 src/video/wayland/SDL_waylandwindow.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/video/wayland/SDL_waylandwindow.c b/src/video/wayland/SDL_waylandwindow.c
index 70560465a9477..44a6d950318ca 100644
--- a/src/video/wayland/SDL_waylandwindow.c
+++ b/src/video/wayland/SDL_waylandwindow.c
@@ -2818,13 +2818,14 @@ void Wayland_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window)
     SDL_VideoData *data = _this->internal;
     SDL_WindowData *wind = window->internal;
 
-    /* Roundtrip before destroying the window to make sure that it has received input leave events, so that
-     * no internal structures are left pointing to the destroyed window. */
-    if (wind->show_hide_sync_required) {
-        WAYLAND_wl_display_roundtrip(data->display);
-    }
-
     if (data && wind) {
+        /* Roundtrip before destroying the window to make sure that it has received input leave events, so that
+         * no internal structures are left pointing to the destroyed window.
+         */
+        if (wind->show_hide_sync_required) {
+            WAYLAND_wl_display_roundtrip(data->display);
+        }
+
 #ifdef SDL_VIDEO_OPENGL_EGL
         if (wind->egl_surface) {
             SDL_EGL_DestroySurface(_this, wind->egl_surface);