SDL: Added SDL_HINT_WAVE_CHUNK_LIMIT

From e1ee0e748d2cd4c09658f114ea410bb246d5e5a7 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 3 Aug 2024 08:49:24 -0700
Subject: [PATCH] Added SDL_HINT_WAVE_CHUNK_LIMIT

---
 include/SDL3/SDL_hints.h | 13 ++++++++++++-
 src/audio/SDL_wave.c     |  8 ++++----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/include/SDL3/SDL_hints.h b/include/SDL3/SDL_hints.h
index 3da8a25ddafea..423fe485a2834 100644
--- a/include/SDL3/SDL_hints.h
+++ b/include/SDL3/SDL_hints.h
@@ -3340,6 +3340,17 @@ extern "C" {
  */
 #define SDL_HINT_WAVE_FACT_CHUNK   "SDL_WAVE_FACT_CHUNK"
 
+/**
+ * A variable controlling the maximum number of chunks in a WAVE file.
+ *
+ * This sets an upper bound on the number of chunks in a WAVE file to avoid wasting time on malformed or corrupt WAVE files. This defaults to "10000".
+ *
+ * This hint should be set before calling SDL_LoadWAV() or SDL_LoadWAV_IO()
+ *
+ * \since This hint is available since SDL 3.0.0.
+ */
+#define SDL_HINT_WAVE_CHUNK_LIMIT   "SDL_WAVE_CHUNK_LIMIT"
+
 /**
  * A variable controlling how the size of the RIFF chunk affects the loading
  * of a WAVE file.
@@ -3351,7 +3362,7 @@ extern "C" {
  * Note that files that have trailing data unrelated to the WAVE file or
  * corrupt files may slow down the loading process without a reliable
  * boundary. By default, SDL stops after 10000 chunks to prevent wasting time.
- * Use the environment variable SDL_WAVE_CHUNK_LIMIT to adjust this value.
+ * Use SDL_HINT_WAVE_CHUNK_LIMIT to adjust this value.
  *
  * The variable can be set to the following values:
  *
diff --git a/src/audio/SDL_wave.c b/src/audio/SDL_wave.c
index f10f24d68d6db..fd2f67af194fb 100644
--- a/src/audio/SDL_wave.c
+++ b/src/audio/SDL_wave.c
@@ -1774,7 +1774,7 @@ static int WaveLoad(SDL_IOStream *src, WaveFile *file, SDL_AudioSpec *spec, Uint
     int result;
     Uint32 chunkcount = 0;
     Uint32 chunkcountlimit = 10000;
-    const char *envchunkcountlimit;
+    const char *hint;
     Sint64 RIFFstart, RIFFend, lastchunkpos;
     SDL_bool RIFFlengthknown = SDL_FALSE;
     WaveFormat *format = &file->format;
@@ -1787,10 +1787,10 @@ static int WaveLoad(SDL_IOStream *src, WaveFile *file, SDL_AudioSpec *spec, Uint
     SDL_zero(fmtchunk);
     SDL_zero(datachunk);
 
-    envchunkcountlimit = SDL_getenv("SDL_WAVE_CHUNK_LIMIT");
-    if (envchunkcountlimit) {
+    hint = SDL_GetHint(SDL_HINT_WAVE_CHUNK_LIMIT);
+    if (hint) {
         unsigned int count;
-        if (SDL_sscanf(envchunkcountlimit, "%u", &count) == 1) {
+        if (SDL_sscanf(hint, "%u", &count) == 1) {
             chunkcountlimit = count <= SDL_MAX_UINT32 ? count : SDL_MAX_UINT32;
         }
     }