From 25cda4e8b4844aecf8cdc70ffcab8e0adaab3486 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sun, 12 May 2013 15:58:29 +0000 Subject: [PATCH] cItems: Added runtime bounds-checking to avoid server crashes with badly written plugins. git-svn-id: http://mc-server.googlecode.com/svn/trunk@1472 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/Bindings.cpp | 112 ++++++++++++++------------------------------ source/Bindings.h | 6 +-- source/Item.cpp | 59 +++++++++++++++++++++++ source/Item.h | 15 +++--- 4 files changed, 103 insertions(+), 89 deletions(-) diff --git a/source/Bindings.cpp b/source/Bindings.cpp index 3bcc6dc8c..7bba8ceab 100644 --- a/source/Bindings.cpp +++ b/source/Bindings.cpp @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 05/09/13 16:34:30. +** Generated automatically by tolua++-1.0.92 on 05/12/13 17:56:56. */ #ifndef __cplusplus @@ -11594,45 +11594,6 @@ static int tolua_AllToLua_cWorld_GetSpawnZ00(lua_State* tolua_S) } #endif //#ifndef TOLUA_DISABLE -/* method: DoExplosiontAt of class cWorld */ -#ifndef TOLUA_DISABLE_tolua_AllToLua_cWorld_DoExplosiontAt00 -static int tolua_AllToLua_cWorld_DoExplosiontAt00(lua_State* tolua_S) -{ -#ifndef TOLUA_RELEASE - tolua_Error tolua_err; - if ( - !tolua_isusertype(tolua_S,1,"cWorld",0,&tolua_err) || - !tolua_isnumber(tolua_S,2,0,&tolua_err) || - !tolua_isnumber(tolua_S,3,0,&tolua_err) || - !tolua_isnumber(tolua_S,4,0,&tolua_err) || - !tolua_isnumber(tolua_S,5,0,&tolua_err) || - !tolua_isnoobj(tolua_S,6,&tolua_err) - ) - goto tolua_lerror; - else -#endif - { - cWorld* self = (cWorld*) tolua_tousertype(tolua_S,1,0); - float a_ExplosionSzie = ((float) tolua_tonumber(tolua_S,2,0)); - int a_BlockX = ((int) tolua_tonumber(tolua_S,3,0)); - int a_BlockY = ((int) tolua_tonumber(tolua_S,4,0)); - int a_BlockZ = ((int) tolua_tonumber(tolua_S,5,0)); -#ifndef TOLUA_RELEASE - if (!self) tolua_error(tolua_S,"invalid 'self' in function 'DoExplosiontAt'", NULL); -#endif - { - self->DoExplosiontAt(a_ExplosionSzie,a_BlockX,a_BlockY,a_BlockZ); - } - } - return 0; -#ifndef TOLUA_RELEASE - tolua_lerror: - tolua_error(tolua_S,"#ferror in function 'DoExplosiontAt'.",&tolua_err); - return 0; -#endif -} -#endif //#ifndef TOLUA_DISABLE - /* method: GetSignLines of class cWorld */ #ifndef TOLUA_DISABLE_tolua_AllToLua_cWorld_GetSignLines00 static int tolua_AllToLua_cWorld_GetSignLines00(lua_State* tolua_S) @@ -13623,8 +13584,8 @@ static int tolua_AllToLua_cItems_Get00(lua_State* tolua_S) if (!self) tolua_error(tolua_S,"invalid 'self' in function 'Get'", NULL); #endif { - cItem& tolua_ret = (cItem&) self->Get(a_Idx); - tolua_pushusertype(tolua_S,(void*)&tolua_ret,"cItem"); + cItem* tolua_ret = (cItem*) self->Get(a_Idx); + tolua_pushusertype(tolua_S,(void*)tolua_ret,"cItem"); } } return 1; @@ -13800,38 +13761,6 @@ static int tolua_AllToLua_cItems_Size00(lua_State* tolua_S) } #endif //#ifndef TOLUA_DISABLE -/* method: Add of class cItems */ -#ifndef TOLUA_DISABLE_tolua_AllToLua_cItems_Add01 -static int tolua_AllToLua_cItems_Add01(lua_State* tolua_S) -{ - tolua_Error tolua_err; - if ( - !tolua_isusertype(tolua_S,1,"cItems",0,&tolua_err) || - !tolua_isnumber(tolua_S,2,0,&tolua_err) || - !tolua_isnumber(tolua_S,3,0,&tolua_err) || - !tolua_isnumber(tolua_S,4,0,&tolua_err) || - !tolua_isnoobj(tolua_S,5,&tolua_err) - ) - goto tolua_lerror; - else - { - cItems* self = (cItems*) tolua_tousertype(tolua_S,1,0); - ENUM_ITEM_ID a_ItemType = ((ENUM_ITEM_ID) (int) tolua_tonumber(tolua_S,2,0)); - char a_ItemCount = ((char) tolua_tonumber(tolua_S,3,0)); - short a_ItemDamage = ((short) tolua_tonumber(tolua_S,4,0)); -#ifndef TOLUA_RELEASE - if (!self) tolua_error(tolua_S,"invalid 'self' in function 'Add'", NULL); -#endif - { - self->Add(a_ItemType,a_ItemCount,a_ItemDamage); - } - } - return 0; -tolua_lerror: - return tolua_AllToLua_cItems_Add00(tolua_S); -} -#endif //#ifndef TOLUA_DISABLE - /* method: Set of class cItems */ #ifndef TOLUA_DISABLE_tolua_AllToLua_cItems_Set01 static int tolua_AllToLua_cItems_Set01(lua_State* tolua_S) @@ -13866,6 +13795,38 @@ tolua_lerror: } #endif //#ifndef TOLUA_DISABLE +/* method: Add of class cItems */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_cItems_Add01 +static int tolua_AllToLua_cItems_Add01(lua_State* tolua_S) +{ + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"cItems",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isnumber(tolua_S,3,0,&tolua_err) || + !tolua_isnumber(tolua_S,4,0,&tolua_err) || + !tolua_isnoobj(tolua_S,5,&tolua_err) + ) + goto tolua_lerror; + else + { + cItems* self = (cItems*) tolua_tousertype(tolua_S,1,0); + ENUM_ITEM_ID a_ItemType = ((ENUM_ITEM_ID) (int) tolua_tonumber(tolua_S,2,0)); + char a_ItemCount = ((char) tolua_tonumber(tolua_S,3,0)); + short a_ItemDamage = ((short) tolua_tonumber(tolua_S,4,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'Add'", NULL); +#endif + { + self->Add(a_ItemType,a_ItemCount,a_ItemDamage); + } + } + return 0; +tolua_lerror: + return tolua_AllToLua_cItems_Add00(tolua_S); +} +#endif //#ifndef TOLUA_DISABLE + /* method: GetWidth of class cItemGrid */ #ifndef TOLUA_DISABLE_tolua_AllToLua_cItemGrid_GetWidth00 static int tolua_AllToLua_cItemGrid_GetWidth00(lua_State* tolua_S) @@ -25042,7 +25003,6 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S) tolua_function(tolua_S,"GetSpawnX",tolua_AllToLua_cWorld_GetSpawnX00); tolua_function(tolua_S,"GetSpawnY",tolua_AllToLua_cWorld_GetSpawnY00); tolua_function(tolua_S,"GetSpawnZ",tolua_AllToLua_cWorld_GetSpawnZ00); - tolua_function(tolua_S,"DoExplosiontAt",tolua_AllToLua_cWorld_DoExplosiontAt00); tolua_function(tolua_S,"GetSignLines",tolua_AllToLua_cWorld_GetSignLines00); tolua_function(tolua_S,"GrowTree",tolua_AllToLua_cWorld_GrowTree00); tolua_function(tolua_S,"GrowTreeFromSapling",tolua_AllToLua_cWorld_GrowTreeFromSapling00); @@ -25126,8 +25086,8 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S) tolua_function(tolua_S,"Delete",tolua_AllToLua_cItems_Delete00); tolua_function(tolua_S,"Clear",tolua_AllToLua_cItems_Clear00); tolua_function(tolua_S,"Size",tolua_AllToLua_cItems_Size00); - tolua_function(tolua_S,"Add",tolua_AllToLua_cItems_Add01); tolua_function(tolua_S,"Set",tolua_AllToLua_cItems_Set01); + tolua_function(tolua_S,"Add",tolua_AllToLua_cItems_Add01); tolua_endmodule(tolua_S); tolua_cclass(tolua_S,"cItemGrid","cItemGrid","",NULL); tolua_beginmodule(tolua_S,"cItemGrid"); diff --git a/source/Bindings.h b/source/Bindings.h index 1965974d1..733a8e3de 100644 --- a/source/Bindings.h +++ b/source/Bindings.h @@ -1,10 +1,6 @@ /* ** Lua binding: AllToLua -<<<<<<< .mine -** Generated automatically by tolua++-1.0.92 on 05/09/13 14:14:48. -======= -** Generated automatically by tolua++-1.0.92 on 05/08/13 11:35:08. ->>>>>>> .r1462 +** Generated automatically by tolua++-1.0.92 on 05/12/13 17:56:56. */ /* Exported function */ diff --git a/source/Item.cpp b/source/Item.cpp index fde4f7ba9..60bd7da95 100644 --- a/source/Item.cpp +++ b/source/Item.cpp @@ -138,3 +138,62 @@ bool cItem::IsEnchantable(short item) + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// cItems: + +cItem * cItems::Get(int a_Idx) +{ + if ((a_Idx < 0) || (a_Idx >= (int)size())) + { + LOGWARNING("cItems: Attempt to get an out-of-bounds item at index %d; there are currently %d items. Returning a nil.", a_Idx, size()); + return NULL; + } + return &at(a_Idx); +} + + + + + +void cItems::Set(int a_Idx, const cItem & a_Item) +{ + if ((a_Idx < 0) || (a_Idx >= (int)size())) + { + LOGWARNING("cItems: Attempt to set an item at an out-of-bounds index %d; there are currently %d items. Not setting.", a_Idx, size()); + return; + } + at(a_Idx) = a_Item; +} + + + + + +void cItems::Delete(int a_Idx) +{ + if ((a_Idx < 0) || (a_Idx >= (int)size())) + { + LOGWARNING("cItems: Attempt to delete an item at an out-of-bounds index %d; there are currently %d items. Ignoring.", a_Idx, size()); + return; + } + erase(begin() + a_Idx); +} + + + + + +void cItems::Set(int a_Idx, ENUM_ITEM_ID a_ItemType, char a_ItemCount, short a_ItemDamage) +{ + if ((a_Idx < 0) || (a_Idx >= (int)size())) + { + LOGWARNING("cItems: Attempt to set an item at an out-of-bounds index %d; there are currently %d items. Not setting.", a_Idx, size()); + return; + } + at(a_Idx) = cItem(a_ItemType, a_ItemCount, a_ItemDamage); +} + + + + diff --git a/source/Item.h b/source/Item.h index 570182bae..3b485d3de 100644 --- a/source/Item.h +++ b/source/Item.h @@ -92,8 +92,10 @@ public: -// This stupid construct is here only so that Lua can access cItems in an API +/** This class bridges a vector of cItem for safe access via Lua. It checks boundaries for all accesses +Note that this class is zero-indexed! +*/ class cItems // tolua_export : public std::vector { // tolua_export @@ -103,22 +105,19 @@ public: /// Need a Lua-accessible constructor cItems(void) {} - cItem & Get (int a_Idx) {return at(a_Idx); } - void Set (int a_Idx, const cItem & a_Item) {at(a_Idx) = a_Item; } + cItem * Get (int a_Idx); + void Set (int a_Idx, const cItem & a_Item); void Add (const cItem & a_Item) {push_back(a_Item); } - void Delete(int a_Idx) {erase(begin() + a_Idx); } + void Delete(int a_Idx); void Clear (void) {clear(); } int Size (void) {return size(); } + void Set (int a_Idx, ENUM_ITEM_ID a_ItemType, char a_ItemCount, short a_ItemDamage); void Add (ENUM_ITEM_ID a_ItemType, char a_ItemCount, short a_ItemDamage) { push_back(cItem(a_ItemType, a_ItemCount, a_ItemDamage)); } - void Set (int a_Idx, ENUM_ITEM_ID a_ItemType, char a_ItemCount, short a_ItemDamage) - { - at(a_Idx) = cItem(a_ItemType, a_ItemCount, a_ItemDamage); - } // tolua_end } ; // tolua_export