Check if SoundData can allocate the memory and limit maximum size to INT_MAX

This commit is contained in:
Bart van Strien
2011-08-13 15:30:29 +02:00
parent 611db40592
commit 73d062e8fe
+15
View File
@@ -44,6 +44,9 @@ namespace sound
// memory to other locations.
data = (char*)realloc(data, size + decoder->getSize());
if (!data)
throw love::Exception("Not enough memory."); // I know, I know, little memory, creating objects..
// Copy memory into new part of memory.
memcpy(data + size, decoder->getBuffer(), decoded);
@@ -61,13 +64,25 @@ namespace sound
SoundData::SoundData(int samples, int sampleRate, int bits, int channels)
: data(0), size(samples*(bits/8)*channels), sampleRate(sampleRate), bits(bits), channels(channels)
{
unsigned double realsize = samples;
realsize *= (bits/8)*channels;
if (realsize > INT_MAX)
throw love::Exception("Data is too big!");
data = (char*)malloc(size);
if (!data)
throw love::Exception("Not enough memory.");
}
SoundData::SoundData(void * d, int samples, int sampleRate, int bits, int channels)
: data(0), size(samples*(bits/8)*channels), sampleRate(sampleRate), bits(bits), channels(channels)
{
unsigned double realsize = samples;
realsize *= (bits/8)*channels;
if (realsize > INT_MAX)
throw love::Exception("Data is too big!");
data = (char*)malloc(size);
if (!data)
throw love::Exception("Not enough memory.");
memcpy(data, d, size);
}