SDL: Free any temporary environment memory at SDL_Quit()

From 156ca356b59098aaf458c2a63f7f6011458c7553 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 23 May 2024 11:22:00 -0700
Subject: [PATCH] Free any temporary environment memory at SDL_Quit()

Fixes https://github.com/libsdl-org/SDL/issues/9860
---
 src/SDL.c               |  3 +++
 src/stdlib/SDL_getenv.c | 13 +++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/src/SDL.c b/src/SDL.c
index 6295fa6662efe..2504d0338e3b2 100644
--- a/src/SDL.c
+++ b/src/SDL.c
@@ -58,6 +58,7 @@
 extern int SDL_HelperWindowCreate(void);
 extern int SDL_HelperWindowDestroy(void);
 #endif
+extern void SDL_FreeEnvironmentMemory(void);
 
 #ifdef SDL_BUILD_MAJOR_VERSION
 SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MAJOR_VERSION,
@@ -557,6 +558,8 @@ void SDL_Quit(void)
 
     SDL_CleanupTLS();
 
+    SDL_FreeEnvironmentMemory();
+
     SDL_bInMainQuit = SDL_FALSE;
 }
 
diff --git a/src/stdlib/SDL_getenv.c b/src/stdlib/SDL_getenv.c
index 0eec441fc7566..866b072b954a2 100644
--- a/src/stdlib/SDL_getenv.c
+++ b/src/stdlib/SDL_getenv.c
@@ -32,6 +32,19 @@
 /* Note this isn't thread-safe! */
 static char *SDL_envmem = NULL; /* Ugh, memory leak */
 static size_t SDL_envmemlen = 0;
+
+void SDL_FreeEnvironmentMemory(void)
+{
+    if (SDL_envmem) {
+        SDL_free(SDL_envmem);
+        SDL_envmem = NULL;
+        SDL_envmemlen = 0;
+    }
+}
+#else
+void SDL_FreeEnvironmentMemory(void)
+{
+}
 #endif
 
 /* Put a variable into the environment */