Fixed memory leak when HAVE_ALLOCA isn't defined
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
diff --git a/src/audio/SDL_audiocvt.c b/src/audio/SDL_audiocvt.c
index 7c977e6..4b24ccd 100644
--- a/src/audio/SDL_audiocvt.c
+++ b/src/audio/SDL_audiocvt.c
@@ -709,15 +709,22 @@ SDL_ResampleCVT(SDL_AudioCVT *cvt, const int chans, const SDL_AudioFormat format
float *dst = (float *) (cvt->buf + srclen);
const int dstlen = (cvt->len * cvt->len_mult) - srclen;
const int paddingsamples = (ResamplerPadding(inrate, outrate) * chans);
- float *padding = SDL_stack_alloc(float, paddingsamples);
+ float *padding;
SDL_assert(format == AUDIO_F32SYS);
/* we keep no streaming state here, so pad with silence on both ends. */
+ padding = SDL_stack_alloc(float, paddingsamples);
+ if (!padding) {
+ SDL_OutOfMemory();
+ return;
+ }
SDL_memset(padding, '\0', paddingsamples * sizeof (float));
cvt->len_cvt = SDL_ResampleAudio(chans, inrate, outrate, padding, padding, src, srclen, dst, dstlen);
+ SDL_stack_free(padding);
+
SDL_memcpy(cvt->buf, dst, cvt->len_cvt); /* !!! FIXME: remove this if we can get the resampler to work in-place again. */
if (cvt->filters[++cvt->filter_index]) {
@@ -1214,7 +1221,7 @@ SDL_ResampleAudioStream(SDL_AudioStream *stream, const void *_inbuf, const int i
const int paddingsamples = ResamplerPadding(inrate, outrate) * chans;
const int paddingbytes = paddingsamples * sizeof (float);
float *lpadding = (float *) stream->resampler_state;
- float *rpadding = SDL_stack_alloc(float, paddingsamples);
+ float *rpadding;
int retval;
if (inbuf == ((const float *) outbuf)) { /* !!! FIXME can't work in-place (for now!). */
@@ -1229,9 +1236,17 @@ SDL_ResampleAudioStream(SDL_AudioStream *stream, const void *_inbuf, const int i
}
/* !!! FIXME: streaming current resamples on Put, because of probably good reasons I can't remember right now, but if we resample on Get, we'd be able to access legit right padding values. */
+ rpadding = SDL_stack_alloc(float, paddingsamples);
+ if (!rpadding) {
+ SDL_OutOfMemory();
+ return 0;
+ }
SDL_memset(rpadding, '\0', paddingbytes);
+
retval = SDL_ResampleAudio(chans, inrate, outrate, lpadding, rpadding, inbuf, inbuflen, outbuf, outbuflen);
+ SDL_stack_free(rpadding);
+
/* update our left padding with end of current input, for next run. */
SDL_memcpy(lpadding, ((const Uint8 *) inbuf) + (inbuflen - paddingbytes), paddingbytes);