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.
This commit is contained in:
Sam Lantinga 2019-06-08 18:22:18 -07:00
parent 3f19a6d5e8
commit 31765242d6

View File

@ -718,9 +718,15 @@ SDL_ResampleCVT(SDL_AudioCVT *cvt, const int chans, const SDL_AudioFormat format
/* !!! FIXME: remove this if we can get the resampler to work in-place again. */ /* !!! FIXME: remove this if we can get the resampler to work in-place again. */
float *dst = (float *) (cvt->buf + srclen); float *dst = (float *) (cvt->buf + srclen);
const int dstlen = (cvt->len * cvt->len_mult) - srclen; const int dstlen = (cvt->len * cvt->len_mult) - srclen;
const int paddingsamples = (ResamplerPadding(inrate, outrate) * chans); const int requestedpadding = ResamplerPadding(inrate, outrate);
int paddingsamples;
float *padding; float *padding;
if (requestedpadding < INT32_MAX / chans) {
paddingsamples = requestedpadding * chans;
} else {
paddingsamples = 0;
}
SDL_assert(format == AUDIO_F32SYS); SDL_assert(format == AUDIO_F32SYS);
/* we keep no streaming state here, so pad with silence on both ends. */ /* we keep no streaming state here, so pad with silence on both ends. */
@ -889,10 +895,14 @@ SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
return SDL_SetError("Invalid source channels"); return SDL_SetError("Invalid source channels");
} else if (!SDL_SupportedChannelCount(dst_channels)) { } else if (!SDL_SupportedChannelCount(dst_channels)) {
return SDL_SetError("Invalid destination channels"); return SDL_SetError("Invalid destination channels");
} else if (src_rate == 0) { } else if (src_rate <= 0) {
return SDL_SetError("Source rate is zero"); return SDL_SetError("Source rate is equal to or less than zero");
} else if (dst_rate == 0) { } else if (dst_rate <= 0) {
return SDL_SetError("Destination rate is zero"); return SDL_SetError("Destination rate is equal to or less than zero");
} else if (src_rate >= INT32_MAX / RESAMPLER_SAMPLES_PER_ZERO_CROSSING) {
return SDL_SetError("Source rate is too high");
} else if (dst_rate >= INT32_MAX / RESAMPLER_SAMPLES_PER_ZERO_CROSSING) {
return SDL_SetError("Destination rate is too high");
} }
#if DEBUG_CONVERT #if DEBUG_CONVERT