Skip to content

Commit

Permalink
Fixed bug 4294 - Audio: perform more validation on conversion request
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
slouken committed Jun 9, 2019
1 parent 3f19a6d commit 3176524
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/audio/SDL_audiocvt.c
Expand Up @@ -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. */
float *dst = (float *) (cvt->buf + 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;

if (requestedpadding < INT32_MAX / chans) {
paddingsamples = requestedpadding * chans;
} else {
paddingsamples = 0;
}
SDL_assert(format == AUDIO_F32SYS);

/* we keep no streaming state here, so pad with silence on both ends. */
Expand Down Expand Up @@ -889,10 +895,14 @@ SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
return SDL_SetError("Invalid source channels");
} else if (!SDL_SupportedChannelCount(dst_channels)) {
return SDL_SetError("Invalid destination channels");
} else if (src_rate == 0) {
return SDL_SetError("Source rate is zero");
} else if (dst_rate == 0) {
return SDL_SetError("Destination rate is zero");
} else if (src_rate <= 0) {
return SDL_SetError("Source rate is equal to or less than zero");
} else if (dst_rate <= 0) {
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
Expand Down

0 comments on commit 3176524

Please sign in to comment.