From f87f97e4fe0251797b0e00894619ab8a792bea93 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sun, 6 Oct 2013 21:52:05 -0300 Subject: [PATCH] Better parameter checking for SoundData (resolves issue #764) --- src/modules/sound/SoundData.cpp | 27 ++++++++++++++------------- src/modules/sound/wrap_SoundData.cpp | 6 +++--- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/modules/sound/SoundData.cpp b/src/modules/sound/SoundData.cpp index 065a0a0d5..22e10a6a3 100644 --- a/src/modules/sound/SoundData.cpp +++ b/src/modules/sound/SoundData.cpp @@ -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); } diff --git a/src/modules/sound/wrap_SoundData.cpp b/src/modules/sound/wrap_SoundData.cpp index 23bc9c0d3..a5c89de81 100644 --- a/src/modules/sound/wrap_SoundData.cpp +++ b/src/modules/sound/wrap_SoundData.cpp @@ -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;