mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
Better parameter checking for SoundData (resolves issue #764)
This commit is contained in:
@@ -41,7 +41,7 @@ SoundData::SoundData(Decoder *decoder)
|
||||
, bitDepth(0)
|
||||
, channels(0)
|
||||
{
|
||||
size_t bufferSize = 524288;
|
||||
size_t bufferSize = 524288; // 0x80000
|
||||
int decoded = decoder->decode();
|
||||
|
||||
while (decoded > 0)
|
||||
@@ -90,6 +90,18 @@ SoundData::SoundData(int samples, int sampleRate, int bitDepth, int channels)
|
||||
, bitDepth(bitDepth)
|
||||
, channels(channels)
|
||||
{
|
||||
if (samples <= 0)
|
||||
throw love::Exception("Invalid sample count: %d", samples);
|
||||
|
||||
if (sampleRate <= 0)
|
||||
throw love::Exception("Invalid sample rate: %d", sampleRate);
|
||||
|
||||
if (bitDepth <= 0)
|
||||
throw love::Exception("Invalid bit depth: %d", bitDepth);
|
||||
|
||||
if (channels <= 0)
|
||||
throw love::Exception("Invalid channel count: %d", channels);
|
||||
|
||||
double realsize = samples;
|
||||
realsize *= (bitDepth/8)*channels;
|
||||
if (realsize > INT_MAX)
|
||||
@@ -100,19 +112,8 @@ SoundData::SoundData(int samples, int sampleRate, int bitDepth, int channels)
|
||||
}
|
||||
|
||||
SoundData::SoundData(void *d, int samples, int sampleRate, int bitDepth, int channels)
|
||||
: data(0)
|
||||
, size(samples*(bitDepth/8)*channels)
|
||||
, sampleRate(sampleRate)
|
||||
, bitDepth(bitDepth)
|
||||
, channels(channels)
|
||||
: SoundData(samples, sampleRate, bitDepth, channels)
|
||||
{
|
||||
double realsize = samples;
|
||||
realsize *= (bitDepth/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);
|
||||
}
|
||||
|
||||
|
||||
@@ -70,8 +70,8 @@ int w_SoundData_getDuration(lua_State *L)
|
||||
int w_SoundData_setSample(lua_State *L)
|
||||
{
|
||||
SoundData *sd = luax_checksounddata(L, 1);
|
||||
int i = (int)lua_tointeger(L, 2);
|
||||
float sample = (float)lua_tonumber(L, 3);
|
||||
int i = (int) luaL_checkinteger(L, 2);
|
||||
float sample = (float) luaL_checknumber(L, 3);
|
||||
|
||||
EXCEPT_GUARD(sd->setSample(i, sample);)
|
||||
return 0;
|
||||
@@ -80,7 +80,7 @@ int w_SoundData_setSample(lua_State *L)
|
||||
int w_SoundData_getSample(lua_State *L)
|
||||
{
|
||||
SoundData *sd = luax_checksounddata(L, 1);
|
||||
int i = (int)lua_tointeger(L, 2);
|
||||
int i = (int) luaL_checkinteger(L, 2);
|
||||
|
||||
EXCEPT_GUARD(lua_pushnumber(L, sd->getSample(i));)
|
||||
return 1;
|
||||
|
||||
Reference in New Issue
Block a user