SDL: Changed 'freesrc' parameter from int to SDL_bool

From bea99d48f2d6738fdd10d4a8aabf90cf752d8414 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 9 Jul 2023 13:49:04 -0700
Subject: [PATCH] Changed 'freesrc' parameter from int to SDL_bool

---
 include/SDL3/SDL_audio.h      |  9 +++------
 include/SDL3/SDL_rwops.h      |  4 ++--
 include/SDL3/SDL_surface.h    |  4 ++--
 src/audio/SDL_wave.c          | 26 ++++++++++++++++----------
 src/dynapi/SDL_dynapi_procs.h |  6 +++---
 src/file/SDL_rwops.c          |  2 +-
 src/video/SDL_bmp.c           |  2 +-
 7 files changed, 28 insertions(+), 25 deletions(-)

diff --git a/include/SDL3/SDL_audio.h b/include/SDL3/SDL_audio.h
index b36ceb1b213f..4326d0cbc69d 100644
--- a/include/SDL3/SDL_audio.h
+++ b/include/SDL3/SDL_audio.h
@@ -594,9 +594,6 @@ extern DECLSPEC int SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev);
  * be valid pointers. The entire data portion of the file is then loaded into
  * memory and decoded if necessary.
  *
- * If `freesrc` is non-zero, the data source gets automatically closed and
- * freed before the function returns.
- *
  * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
  * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
  * A-law and mu-law (8 bits). Other formats are currently unsupported and
@@ -643,11 +640,11 @@ extern DECLSPEC int SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev);
  * ```
  *
  * \param src The data source for the WAVE data
- * \param freesrc If non-zero, SDL will _always_ free the data source
+ * \param freesrc if SDL_TRUE, calls SDL_RWclose() on `src` before returning, even in the case of an error
  * \param spec An SDL_AudioSpec that will be filled in with the wave file's
  *             format details
  * \param audio_buf A pointer filled with the audio data, allocated by the
- *                  function.
+ *                  function
  * \param audio_len A pointer filled with the length of the audio data buffer
  *                  in bytes
  * \returns This function, if successfully called, returns `spec`, which will
@@ -669,7 +666,7 @@ extern DECLSPEC int SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev);
  * \sa SDL_LoadWAV
  */
 extern DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops * src,
-                                                      int freesrc,
+                                                      SDL_bool freesrc,
                                                       SDL_AudioSpec * spec,
                                                       Uint8 ** audio_buf,
                                                       Uint32 * audio_len);
diff --git a/include/SDL3/SDL_rwops.h b/include/SDL3/SDL_rwops.h
index df242c4c0d71..e8183e4fab08 100644
--- a/include/SDL3/SDL_rwops.h
+++ b/include/SDL3/SDL_rwops.h
@@ -530,14 +530,14 @@ extern DECLSPEC int SDLCALL SDL_RWclose(SDL_RWops *context);
  *
  * \param src the SDL_RWops to read all available data from
  * \param datasize if not NULL, will store the number of bytes read
- * \param freesrc if non-zero, calls SDL_RWclose() on `src` before returning
+ * \param freesrc if SDL_TRUE, calls SDL_RWclose() on `src` before returning, even in the case of an error
  * \returns the data, or NULL if there was an error.
  *
  * \since This function is available since SDL 3.0.0.
  */
 extern DECLSPEC void *SDLCALL SDL_LoadFile_RW(SDL_RWops *src,
                                               size_t *datasize,
-                                              int freesrc);
+                                              SDL_bool freesrc);
 
 /**
  * Load all the data from a file path.
diff --git a/include/SDL3/SDL_surface.h b/include/SDL3/SDL_surface.h
index 6d6f6b279d08..6f34e4769053 100644
--- a/include/SDL3/SDL_surface.h
+++ b/include/SDL3/SDL_surface.h
@@ -228,7 +228,7 @@ extern DECLSPEC void SDLCALL SDL_UnlockSurface(SDL_Surface *surface);
  * will result in a memory leak.
  *
  * \param src the data stream for the surface
- * \param freesrc non-zero to close the stream after being read
+ * \param freesrc if SDL_TRUE, calls SDL_RWclose() on `src` before returning, even in the case of an error
  * \returns a pointer to a new SDL_Surface structure or NULL if there was an
  *          error; call SDL_GetError() for more information.
  *
@@ -238,7 +238,7 @@ extern DECLSPEC void SDLCALL SDL_UnlockSurface(SDL_Surface *surface);
  * \sa SDL_LoadBMP
  * \sa SDL_SaveBMP_RW
  */
-extern DECLSPEC SDL_Surface *SDLCALL SDL_LoadBMP_RW(SDL_RWops *src, int freesrc);
+extern DECLSPEC SDL_Surface *SDLCALL SDL_LoadBMP_RW(SDL_RWops *src, SDL_bool freesrc);
 
 /**
  * Load a BMP image from a file.
diff --git a/src/audio/SDL_wave.c b/src/audio/SDL_wave.c
index d7e6ac3c36a4..3f442042c8c4 100644
--- a/src/audio/SDL_wave.c
+++ b/src/audio/SDL_wave.c
@@ -2075,9 +2075,9 @@ static int WaveLoad(SDL_RWops *src, WaveFile *file, SDL_AudioSpec *spec, Uint8 *
     return 0;
 }
 
-SDL_AudioSpec *SDL_LoadWAV_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
+SDL_AudioSpec *SDL_LoadWAV_RW(SDL_RWops *src, SDL_bool freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
 {
-    int result;
+    int result = -1;
     WaveFile file;
 
     SDL_zero(file);
@@ -2085,16 +2085,16 @@ SDL_AudioSpec *SDL_LoadWAV_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec,
     /* Make sure we are passed a valid data source */
     if (src == NULL) {
         /* Error may come from RWops. */
-        return NULL;
+        goto done;
     } else if (spec == NULL) {
         SDL_InvalidParamError("spec");
-        return NULL;
+        goto done;
     } else if (audio_buf == NULL) {
         SDL_InvalidParamError("audio_buf");
-        return NULL;
+        goto done;
     } else if (audio_len == NULL) {
         SDL_InvalidParamError("audio_len");
-        return NULL;
+        goto done;
     }
 
     *audio_buf = NULL;
@@ -2113,13 +2113,19 @@ SDL_AudioSpec *SDL_LoadWAV_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec,
     }
 
     /* Cleanup */
-    if (freesrc) {
-        SDL_RWclose(src);
-    } else {
+    if (!freesrc) {
         SDL_RWseek(src, file.chunk.position, SDL_RW_SEEK_SET);
     }
     WaveFreeChunkData(&file.chunk);
     SDL_free(file.decoderdata);
 
-    return spec;
+done:
+    if (freesrc && src) {
+        SDL_RWclose(src);
+    }
+    if (result == 0) {
+        return spec;
+    } else {
+        return NULL;
+    }
 }
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index e308400925f3..c5416f33cbc8 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -550,12 +550,12 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_JoystickHasRumble,(SDL_Joystick *a),(a),return)
 SDL_DYNAPI_PROC(SDL_bool,SDL_JoystickHasRumbleTriggers,(SDL_Joystick *a),(a),return)
 SDL_DYNAPI_PROC(int,SDL_JoystickIsHaptic,(SDL_Joystick *a),(a),return)
 SDL_DYNAPI_PROC(SDL_Surface*,SDL_LoadBMP,(const char *a),(a),return)
-SDL_DYNAPI_PROC(SDL_Surface*,SDL_LoadBMP_RW,(SDL_RWops *a, int b),(a,b),return)
+SDL_DYNAPI_PROC(SDL_Surface*,SDL_LoadBMP_RW,(SDL_RWops *a, SDL_bool b),(a,b),return)
 SDL_DYNAPI_PROC(void*,SDL_LoadFile,(const char *a, size_t *b),(a,b),return)
-SDL_DYNAPI_PROC(void*,SDL_LoadFile_RW,(SDL_RWops *a, size_t *b, int c),(a,b,c),return)
+SDL_DYNAPI_PROC(void*,SDL_LoadFile_RW,(SDL_RWops *a, size_t *b, SDL_bool c),(a,b,c),return)
 SDL_DYNAPI_PROC(SDL_FunctionPointer,SDL_LoadFunction,(void *a, const char *b),(a,b),return)
 SDL_DYNAPI_PROC(void*,SDL_LoadObject,(const char *a),(a),return)
-SDL_DYNAPI_PROC(SDL_AudioSpec*,SDL_LoadWAV_RW,(SDL_RWops *a, int b, SDL_AudioSpec *c, Uint8 **d, Uint32 *e),(a,b,c,d,e),return)
+SDL_DYNAPI_PROC(SDL_AudioSpec*,SDL_LoadWAV_RW,(SDL_RWops *a, SDL_bool b, SDL_AudioSpec *c, Uint8 **d, Uint32 *e),(a,b,c,d,e),return)
 SDL_DYNAPI_PROC(int,SDL_LockAudioDevice,(SDL_AudioDeviceID a),(a),return)
 SDL_DYNAPI_PROC(void,SDL_LockJoysticks,(void),(),)
 SDL_DYNAPI_PROC(int,SDL_LockMutex,(SDL_Mutex *a),(a),return)
diff --git a/src/file/SDL_rwops.c b/src/file/SDL_rwops.c
index c59f177eb1ea..2f9cef06cef3 100644
--- a/src/file/SDL_rwops.c
+++ b/src/file/SDL_rwops.c
@@ -666,7 +666,7 @@ void SDL_DestroyRW(SDL_RWops *area)
 }
 
 /* Load all the data from an SDL data stream */
-void *SDL_LoadFile_RW(SDL_RWops *src, size_t *datasize, int freesrc)
+void *SDL_LoadFile_RW(SDL_RWops *src, size_t *datasize, SDL_bool freesrc)
 {
     static const Sint64 FILE_CHUNK_SIZE = 1024;
     Sint64 size;
diff --git a/src/video/SDL_bmp.c b/src/video/SDL_bmp.c
index 4133f62d4f45..126baefec03c 100644
--- a/src/video/SDL_bmp.c
+++ b/src/video/SDL_bmp.c
@@ -193,7 +193,7 @@ static void CorrectAlphaChannel(SDL_Surface *surface)
     }
 }
 
-SDL_Surface *SDL_LoadBMP_RW(SDL_RWops *src, int freesrc)
+SDL_Surface *SDL_LoadBMP_RW(SDL_RWops *src, SDL_bool freesrc)
 {
     SDL_bool was_error;
     Sint64 fp_offset = 0;