Fixed bug 4294 - Audio: perform more validation on conversion request
authorSam Lantinga
Sat, 08 Jun 2019 18:22:18 -0700
changeset 1280370d338e248c8
parent 12802 f9a9d6c76b21
child 12804 1a330416800d
Fixed bug 4294 - Audio: perform more validation on conversion request

janisozaur

There are many cases which are not able to be handled by SDL's audio conversion routines, including too low (negative) rate, too high rate (impossible to allocate).

This patch aims to report such issues early and handle others in a graceful manner. The "INT32_MAX / RESAMPLER_SAMPLES_PER_ZERO_CROSSING" value is the conservative approach in terms of what can _technically_ be supported, but its value is 4'194'303, or just shy of 4.2MHz. I highly doubt any sane person would use such rates, especially in SDL2, so I would like to drive this limit further down, but would need some assistance to do that, as doing so would have to introduce an arbitrary value. Are you OK with such approach? What would a good value be? Wikipedia (https://en.wikipedia.org/wiki/High-resolution_audio) lists 96kHz as the highest sampling rate in use, even if I quadruple it for a good measure, to 384kHz it's still an order of magnitude lower than 4MHz.
src/audio/SDL_audiocvt.c
     1.1 --- a/src/audio/SDL_audiocvt.c	Sat Jun 08 18:07:58 2019 -0700
     1.2 +++ b/src/audio/SDL_audiocvt.c	Sat Jun 08 18:22:18 2019 -0700
     1.3 @@ -718,9 +718,15 @@
     1.4      /* !!! FIXME: remove this if we can get the resampler to work in-place again. */
     1.5      float *dst = (float *) (cvt->buf + srclen);
     1.6      const int dstlen = (cvt->len * cvt->len_mult) - srclen;
     1.7 -    const int paddingsamples = (ResamplerPadding(inrate, outrate) * chans);
     1.8 +    const int requestedpadding = ResamplerPadding(inrate, outrate);
     1.9 +    int paddingsamples;
    1.10      float *padding;
    1.11  
    1.12 +    if (requestedpadding < INT32_MAX / chans) {
    1.13 +        paddingsamples = requestedpadding * chans;
    1.14 +    } else {
    1.15 +        paddingsamples = 0;
    1.16 +    }
    1.17      SDL_assert(format == AUDIO_F32SYS);
    1.18  
    1.19      /* we keep no streaming state here, so pad with silence on both ends. */
    1.20 @@ -889,10 +895,14 @@
    1.21          return SDL_SetError("Invalid source channels");
    1.22      } else if (!SDL_SupportedChannelCount(dst_channels)) {
    1.23          return SDL_SetError("Invalid destination channels");
    1.24 -    } else if (src_rate == 0) {
    1.25 -        return SDL_SetError("Source rate is zero");
    1.26 -    } else if (dst_rate == 0) {
    1.27 -        return SDL_SetError("Destination rate is zero");
    1.28 +    } else if (src_rate <= 0) {
    1.29 +        return SDL_SetError("Source rate is equal to or less than zero");
    1.30 +    } else if (dst_rate <= 0) {
    1.31 +        return SDL_SetError("Destination rate is equal to or less than zero");
    1.32 +    } else if (src_rate >= INT32_MAX / RESAMPLER_SAMPLES_PER_ZERO_CROSSING) {
    1.33 +        return SDL_SetError("Source rate is too high");
    1.34 +    } else if (dst_rate >= INT32_MAX / RESAMPLER_SAMPLES_PER_ZERO_CROSSING) {
    1.35 +        return SDL_SetError("Destination rate is too high");
    1.36      }
    1.37  
    1.38  #if DEBUG_CONVERT