SDL_mixer: Mix_LoadWAV_RW, Mix_FreeChunk: use correct function to free chunk->abuf. (db9da)

From db9da3d416077195a2f84403b2fb40955518b4ab Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Sat, 8 Oct 2022 01:24:24 +0300
Subject: [PATCH] Mix_LoadWAV_RW, Mix_FreeChunk: use correct function to free
 chunk->abuf.

chunk->abuf allocated by SDL_LoadWAV_RW should be freed by SDL_FreeWAV.
Ported from SDL-1.2 branch commit 4c47f6f for sake of being by the book.

Reference issue: https://github.com/libsdl-org/sdl12-compat/issues/215
---
 src/mixer.c | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/src/mixer.c b/src/mixer.c
index a1512559..41997e69 100644
--- a/src/mixer.c
+++ b/src/mixer.c
@@ -748,6 +748,7 @@ Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
     SDL_AudioSpec wavespec, *loaded;
     SDL_AudioCVT wavecvt;
     int samplesize;
+    int wavfree;        /* to decide how to free chunk->abuf. */
 
     /* rcg06012001 Make sure src is valid */
     if (!src) {
@@ -786,7 +787,9 @@ Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
     /* Seek backwards for compatibility with older loaders */
     SDL_RWseek(src, -4, RW_SEEK_CUR);
 
+    wavfree = 0;
     if (SDL_memcmp(magic, "WAVE", 4) == 0 || SDL_memcmp(magic, "RIFF", 4) == 0) {
+        wavfree = 1;
         loaded = SDL_LoadWAV_RW(src, freesrc, &wavespec, (Uint8 **)&chunk->abuf, &chunk->alen);
     } else if (SDL_memcmp(magic, "FORM", 4) == 0) {
         loaded = Mix_LoadAIFF_RW(src, freesrc, &wavespec, (Uint8 **)&chunk->abuf, &chunk->alen);
@@ -813,21 +816,33 @@ Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
         if (SDL_BuildAudioCVT(&wavecvt,
                 wavespec.format, wavespec.channels, wavespec.freq,
                 mixer.format, mixer.channels, mixer.freq) < 0) {
-            SDL_free(chunk->abuf);
+            if (wavfree) {
+                SDL_FreeWAV(chunk->abuf);
+            } else {
+                SDL_free(chunk->abuf);
+            }
             SDL_free(chunk);
             return(NULL);
         }
         samplesize = ((wavespec.format & 0xFF)/8)*wavespec.channels;
-        wavecvt.len = chunk->alen & ~(samplesize-1);
+        wavecvt.len = chunk->alen & ~(samplesize - 1);
         wavecvt.buf = (Uint8 *)SDL_calloc(1, wavecvt.len*wavecvt.len_mult);
         if (wavecvt.buf == NULL) {
             Mix_OutOfMemory();
-            SDL_free(chunk->abuf);
+            if (wavfree) {
+                SDL_FreeWAV(chunk->abuf);
+            } else {
+                SDL_free(chunk->abuf);
+            }
             SDL_free(chunk);
             return(NULL);
         }
         SDL_memcpy(wavecvt.buf, chunk->abuf, wavecvt.len);
-        SDL_free(chunk->abuf);
+        if (wavfree) {
+            SDL_FreeWAV(chunk->abuf);
+        } else {
+            SDL_free(chunk->abuf);
+        }
 
         /* Run the audio converter */
         if (SDL_ConvertAudio(&wavecvt) < 0) {
@@ -838,9 +853,10 @@ Mix_Chunk *Mix_LoadWAV_RW(SDL_RWops *src, int freesrc)
 
         chunk->abuf = wavecvt.buf;
         chunk->alen = wavecvt.len_cvt;
+        wavfree = 0;
     }
 
-    chunk->allocated = 1;
+    chunk->allocated = (wavfree == 0) ? 1 : 2; /* see Mix_FreeChunk() */
     chunk->volume = MIX_MAX_VOLUME;
 
     return(chunk);
@@ -946,8 +962,13 @@ void Mix_FreeChunk(Mix_Chunk *chunk)
         }
         Mix_UnlockAudio();
         /* Actually free the chunk */
-        if (chunk->allocated) {
+        switch (chunk->allocated) {
+        case 1:
             SDL_free(chunk->abuf);
+            break;
+        case 2:
+            SDL_FreeWAV(chunk->abuf);
+            break;
         }
         SDL_free(chunk);
     }