Browse Source

bounds check ChainShape:getPoint, and make functions that take indices 1-indexed on the Lua end of things

Bill Meltsner 13 years ago
parent
commit
ddb1492c1f

+ 2 - 0
src/modules/physics/box2d/ChainShape.cpp

@@ -99,6 +99,8 @@ namespace box2d
 	b2Vec2 ChainShape::getPoint(int index) const
 	{
 		b2ChainShape * c = (b2ChainShape *)shape;
+		if (index < 0 || index >= c->m_count)
+			throw love::Exception("Physics error: index out of bounds");
 		const b2Vec2 & v = c->m_vertices[index];
 		return Physics::scaleUp(v);
 	}

+ 2 - 2
src/modules/physics/box2d/Fixture.cpp

@@ -268,7 +268,7 @@ namespace box2d
 		float p2x = Physics::scaleDown((float)luaL_checknumber(L, 3));
 		float p2y = Physics::scaleDown((float)luaL_checknumber(L, 4));
 		float maxFraction = (float)luaL_checknumber(L, 5);
-		int childIndex = (int)luaL_optint(L, 6, 0);
+		int childIndex = (int)luaL_optint(L, 6, 1) - 1; // Convert from 1-based index
 		b2RayCastInput input;
 		input.p1.Set(p1x, p1y);
 		input.p2.Set(p2x, p2y);
@@ -284,7 +284,7 @@ namespace box2d
 
 	int Fixture::getBoundingBox(lua_State * L) const
 	{
-		int childIndex = (int)luaL_optint(L, 1, 0);
+		int childIndex = (int)luaL_optint(L, 1, 1) - 1; // Convert from 1-based index
 		b2AABB box = fixture->GetAABB(childIndex);
 		box = Physics::scaleUp(box);
 		lua_pushnumber(L, box.lowerBound.x);

+ 2 - 2
src/modules/physics/box2d/Shape.cpp

@@ -100,7 +100,7 @@ namespace box2d
 		float x = Physics::scaleDown((float)luaL_checknumber(L, 6));
 		float y = Physics::scaleDown((float)luaL_checknumber(L, 7));
 		float r = (float)luaL_checknumber(L, 8);
-		int childIndex = (int)luaL_optint(L, 9, 0);
+		int childIndex = (int)luaL_optint(L, 9, 1) - 1; // Convert from 1-based index
 		b2RayCastInput input;
 		input.p1.Set(p1x, p1y);
 		input.p2.Set(p2x, p2y);
@@ -120,7 +120,7 @@ namespace box2d
 		float x = Physics::scaleDown((float)luaL_checknumber(L, 1));
 		float y = Physics::scaleDown((float)luaL_checknumber(L, 2));
 		float r = (float)luaL_checknumber(L, 3);
-		int childIndex = (int)luaL_optint(L, 4, 0);
+		int childIndex = (int)luaL_optint(L, 4, 1) - 1; // Convert from 1-based index
 		b2Transform transform(b2Vec2(x, y), b2Rot(r));
 		b2AABB box;
 		shape->ComputeAABB(&box, transform, childIndex);

+ 5 - 3
src/modules/physics/box2d/wrap_ChainShape.cpp

@@ -19,6 +19,7 @@
 **/
 
 #include "wrap_ChainShape.h"
+#include "wrap_Physics.h"
 #include "Physics.h"
 
 namespace love
@@ -60,7 +61,7 @@ namespace box2d
 	int w_ChainShape_getChildEdge(lua_State * L)
 	{
 		ChainShape * c = luax_checkchainshape(L, 1);
-		int index = luaL_checkint(L, 2);
+		int index = luaL_checkint(L, 2) - 1; // Convert from 1-based index
 		EdgeShape * e = c->getChildEdge(index);
 		luax_newtype(L, "EdgeShape", PHYSICS_EDGE_SHAPE_T, e);
 		return 1;
@@ -77,8 +78,9 @@ namespace box2d
 	int w_ChainShape_getPoint(lua_State * L)
 	{
 		ChainShape * c = luax_checkchainshape(L, 1);
-		int index = luaL_checkint(L, 2);
-		b2Vec2 v = c->getPoint(index);
+		int index = luaL_checkint(L, 2) - 1; // Convert from 1-based index
+		b2Vec2 v;
+		ASSERT_GUARD(v = c->getPoint(index);)
 		lua_pushnumber(L, v.x);
 		lua_pushnumber(L, v.y);
 		return 2;