SDL: Fixed crash when setting the default cursor twice

From 9129e1d5577ef2eeaa7008eee14473dcf27d92c3 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 10 Aug 2023 12:13:40 -0700
Subject: [PATCH] Fixed crash when setting the default cursor twice

This happens in the KMSDRM driver, once after video init, setting a blank default cursor, and once when creating a window when the KMSDRM mouse is initialized.

Also fixed a memory leak freeing the default cursor at shutdown
---
 src/events/SDL_mouse.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c
index e61671efac04..fd8b62cee569 100644
--- a/src/events/SDL_mouse.c
+++ b/src/events/SDL_mouse.c
@@ -234,7 +234,11 @@ void SDL_SetDefaultCursor(SDL_Cursor *cursor)
     if (mouse->def_cursor) {
         SDL_Cursor *default_cursor = mouse->def_cursor;
 
+        if (mouse->cur_cursor == mouse->def_cursor) {
+            mouse->cur_cursor = NULL;
+        }
         mouse->def_cursor = NULL;
+
         SDL_DestroyCursor(default_cursor);
     }
 
@@ -865,6 +869,10 @@ void SDL_QuitMouse(void)
     SDL_SetRelativeMouseMode(SDL_FALSE);
     SDL_ShowCursor();
 
+    if (mouse->def_cursor) {
+        SDL_SetDefaultCursor(NULL);
+    }
+
     cursor = mouse->cursors;
     while (cursor) {
         next = cursor->next;
@@ -874,10 +882,6 @@ void SDL_QuitMouse(void)
     mouse->cursors = NULL;
     mouse->cur_cursor = NULL;
 
-    if (mouse->def_cursor) {
-        SDL_SetDefaultCursor(NULL);
-    }
-
     if (mouse->sources) {
         SDL_free(mouse->sources);
         mouse->sources = NULL;