Fixed memory leak when HAVE_ALLOCA isn't defined
authorSam Lantinga <slouken@libsdl.org>
Fri, 22 Sep 2017 08:51:45 -0700
changeset 11519535b0c8ba4ce
parent 11518 0c51e6ef1b73
child 11520 fe2afdc5616c
Fixed memory leak when HAVE_ALLOCA isn't defined
src/audio/SDL_audiocvt.c
     1.1 --- a/src/audio/SDL_audiocvt.c	Fri Sep 22 22:30:02 2017 +0900
     1.2 +++ b/src/audio/SDL_audiocvt.c	Fri Sep 22 08:51:45 2017 -0700
     1.3 @@ -709,15 +709,22 @@
     1.4      float *dst = (float *) (cvt->buf + srclen);
     1.5      const int dstlen = (cvt->len * cvt->len_mult) - srclen;
     1.6      const int paddingsamples = (ResamplerPadding(inrate, outrate) * chans);
     1.7 -    float *padding = SDL_stack_alloc(float, paddingsamples);
     1.8 +    float *padding;
     1.9  
    1.10      SDL_assert(format == AUDIO_F32SYS);
    1.11  
    1.12      /* we keep no streaming state here, so pad with silence on both ends. */
    1.13 +    padding = SDL_stack_alloc(float, paddingsamples);
    1.14 +    if (!padding) {
    1.15 +        SDL_OutOfMemory();
    1.16 +        return;
    1.17 +    }
    1.18      SDL_memset(padding, '\0', paddingsamples * sizeof (float));
    1.19  
    1.20      cvt->len_cvt = SDL_ResampleAudio(chans, inrate, outrate, padding, padding, src, srclen, dst, dstlen);
    1.21  
    1.22 +    SDL_stack_free(padding);
    1.23 +
    1.24      SDL_memcpy(cvt->buf, dst, cvt->len_cvt);  /* !!! FIXME: remove this if we can get the resampler to work in-place again. */
    1.25  
    1.26      if (cvt->filters[++cvt->filter_index]) {
    1.27 @@ -1214,7 +1221,7 @@
    1.28      const int paddingsamples = ResamplerPadding(inrate, outrate) * chans;
    1.29      const int paddingbytes = paddingsamples * sizeof (float);
    1.30      float *lpadding = (float *) stream->resampler_state;
    1.31 -    float *rpadding = SDL_stack_alloc(float, paddingsamples);
    1.32 +    float *rpadding;
    1.33      int retval;
    1.34  
    1.35      if (inbuf == ((const float *) outbuf)) {  /* !!! FIXME can't work in-place (for now!). */
    1.36 @@ -1229,9 +1236,17 @@
    1.37      }
    1.38  
    1.39      /* !!! 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. */
    1.40 +    rpadding = SDL_stack_alloc(float, paddingsamples);
    1.41 +    if (!rpadding) {
    1.42 +        SDL_OutOfMemory();
    1.43 +        return 0;
    1.44 +    }
    1.45      SDL_memset(rpadding, '\0', paddingbytes);
    1.46 +
    1.47      retval = SDL_ResampleAudio(chans, inrate, outrate, lpadding, rpadding, inbuf, inbuflen, outbuf, outbuflen);
    1.48  
    1.49 +    SDL_stack_free(rpadding);
    1.50 +
    1.51      /* update our left padding with end of current input, for next run. */
    1.52      SDL_memcpy(lpadding, ((const Uint8 *) inbuf) + (inbuflen - paddingbytes), paddingbytes);
    1.53