Added sanity checks against illegal input audio stream parameters e.g. wildly excessive samplerate

This commit is contained in:
oparviai 2017-08-27 15:23:28 +00:00
parent b56859a3fe
commit 6e8d58cbcc
4 changed files with 36 additions and 22 deletions

View File

@ -222,8 +222,17 @@ void WavInFile::init()
if (hdrsOk != 0) if (hdrsOk != 0)
{ {
// Something didn't match in the wav file headers // Something didn't match in the wav file headers
string msg = "Input file is corrupt or not a WAV file"; ST_THROW_RT_ERROR("Input file is corrupt or not a WAV file");
ST_THROW_RT_ERROR(msg.c_str()); }
// sanity check for format parameters
if ((header.format.channel_number < 1) || (header.format.channel_number > 9) ||
(header.format.sample_rate < 4000) || (header.format.sample_rate > 192000) ||
(header.format.byte_per_sample < 1) || (header.format.byte_per_sample > 320) ||
(header.format.bits_per_sample < 8) || (header.format.bits_per_sample > 32))
{
// Something didn't match in the wav file headers
ST_THROW_RT_ERROR("Error: Illegal wav file header format parameters.");
} }
/* Ignore 'fixed' field value as 32bit signed linear data can have other value than 1. /* Ignore 'fixed' field value as 32bit signed linear data can have other value than 1.
@ -544,12 +553,12 @@ int WavInFile::readHeaderBlock()
if (fread(&(header.format.fixed), nLen, 1, fptr) != 1) return -1; if (fread(&(header.format.fixed), nLen, 1, fptr) != 1) return -1;
// swap byte order if necessary // swap byte order if necessary
_swap16(header.format.fixed); // short int fixed; _swap16((short &)header.format.fixed); // short int fixed;
_swap16(header.format.channel_number); // short int channel_number; _swap16((short &)header.format.channel_number); // short int channel_number;
_swap32((int &)header.format.sample_rate); // int sample_rate; _swap32((int &)header.format.sample_rate); // int sample_rate;
_swap32((int &)header.format.byte_rate); // int byte_rate; _swap32((int &)header.format.byte_rate); // int byte_rate;
_swap16(header.format.byte_per_sample); // short int byte_per_sample; _swap16((short &)header.format.byte_per_sample); // short int byte_per_sample;
_swap16(header.format.bits_per_sample); // short int bits_per_sample; _swap16((short &)header.format.bits_per_sample); // short int bits_per_sample;
// if format_len is larger than expected, skip the extra data // if format_len is larger than expected, skip the extra data
if (nDump > 0) if (nDump > 0)

View File

@ -58,7 +58,7 @@ typedef unsigned int uint;
typedef struct typedef struct
{ {
char riff_char[4]; char riff_char[4];
int package_len; uint package_len;
char wave[4]; char wave[4];
} WavRiff; } WavRiff;
@ -66,20 +66,20 @@ typedef struct
typedef struct typedef struct
{ {
char fmt[4]; char fmt[4];
int format_len; unsigned int format_len;
short fixed; unsigned short fixed;
short channel_number; unsigned short channel_number;
int sample_rate; unsigned int sample_rate;
int byte_rate; unsigned int byte_rate;
short byte_per_sample; unsigned short byte_per_sample;
short bits_per_sample; unsigned short bits_per_sample;
} WavFormat; } WavFormat;
/// WAV audio file 'fact' section header /// WAV audio file 'fact' section header
typedef struct typedef struct
{ {
char fact_field[4]; char fact_field[4];
int fact_len; uint fact_len;
uint fact_sample_len; uint fact_sample_len;
} WavFact; } WavFact;

View File

@ -286,9 +286,9 @@ void SoundTouch::calcEffectiveRateAndTempo()
// Sets sample rate. // Sets sample rate.
void SoundTouch::setSampleRate(uint srate) void SoundTouch::setSampleRate(uint srate)
{ {
bSrateSet = true;
// set sample rate, leave other tempo changer parameters as they are. // set sample rate, leave other tempo changer parameters as they are.
pTDStretch->setParameters((int)srate); pTDStretch->setParameters((int)srate);
bSrateSet = true;
} }

View File

@ -134,7 +134,12 @@ void TDStretch::setParameters(int aSampleRate, int aSequenceMS,
int aSeekWindowMS, int aOverlapMS) int aSeekWindowMS, int aOverlapMS)
{ {
// accept only positive parameter values - if zero or negative, use old values instead // accept only positive parameter values - if zero or negative, use old values instead
if (aSampleRate > 0) this->sampleRate = aSampleRate; if (aSampleRate > 0)
{
if (aSampleRate > 192000) ST_THROW_RT_ERROR("Error: Excessive samplerate");
this->sampleRate = aSampleRate;
}
if (aOverlapMS > 0) this->overlapMs = aOverlapMS; if (aOverlapMS > 0) this->overlapMs = aOverlapMS;
if (aSequenceMS > 0) if (aSequenceMS > 0)