Commits

Alex Szpakowski committed 57e5330

Better parameter checking for SoundData (resolves issue #764)

Comments (0)

Files changed (2)

src/modules/sound/SoundData.cpp

 	, bitDepth(0)
 	, channels(0)
 {
-	size_t bufferSize = 524288;
+	size_t bufferSize = 524288; // 0x80000
 	int decoded = decoder->decode();
 
 	while (decoded > 0)
 	, 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)
 }
 
 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);
 }
 

src/modules/sound/wrap_SoundData.cpp

 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;
 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;