Browse Source

Error if getting/setting out-of-range samples (issue #674)

Bart van Strien 12 years ago
parent
commit
aaa301ead2
2 changed files with 18 additions and 4 deletions
  1. 2 2
      src/modules/sound/SoundData.cpp
  2. 16 2
      src/modules/sound/wrap_SoundData.cpp

+ 2 - 2
src/modules/sound/SoundData.cpp

@@ -161,7 +161,7 @@ void SoundData::setSample(int i, float sample)
 {
 {
 	// Check range.
 	// Check range.
 	if (i < 0 || i >= size/(bits/8))
 	if (i < 0 || i >= size/(bits/8))
-		return;
+		throw love::Exception("Attempt to set out-of-range sample!");
 
 
 	if (bits == 16)
 	if (bits == 16)
 	{
 	{
@@ -180,7 +180,7 @@ float SoundData::getSample(int i) const
 {
 {
 	// Check range.
 	// Check range.
 	if (i < 0 || i >= size/(bits/8))
 	if (i < 0 || i >= size/(bits/8))
-		return 0;
+		throw love::Exception("Attempt to get out-of-range sample!");
 
 
 	if (bits == 16)
 	if (bits == 16)
 	{
 	{

+ 16 - 2
src/modules/sound/wrap_SoundData.cpp

@@ -72,7 +72,14 @@ int w_SoundData_setSample(lua_State *L)
 	SoundData *sd = luax_checksounddata(L, 1);
 	SoundData *sd = luax_checksounddata(L, 1);
 	int i = (int)lua_tointeger(L, 2);
 	int i = (int)lua_tointeger(L, 2);
 	float sample = (float)lua_tonumber(L, 3);
 	float sample = (float)lua_tonumber(L, 3);
-	sd->setSample(i, sample);
+	try
+	{
+		sd->setSample(i, sample);
+	}
+	catch (love::Exception &e)
+	{
+		return luaL_error(L, "%s", e.what());
+	}
 	return 0;
 	return 0;
 }
 }
 
 
@@ -80,7 +87,14 @@ int w_SoundData_getSample(lua_State *L)
 {
 {
 	SoundData *sd = luax_checksounddata(L, 1);
 	SoundData *sd = luax_checksounddata(L, 1);
 	int i = (int)lua_tointeger(L, 2);
 	int i = (int)lua_tointeger(L, 2);
-	lua_pushnumber(L, sd->getSample(i));
+	try
+	{
+		lua_pushnumber(L, sd->getSample(i));
+	}
+	catch (love::Exception &e)
+	{
+		return luaL_error(L, "%s", e.what());
+	}
 	return 1;
 	return 1;
 }
 }