diff options
author | ShadowNinja <shadowninja@minetest.net> | 2013-12-30 14:00:05 -0500 |
---|---|---|
committer | ShadowNinja <shadowninja@minetest.net> | 2013-12-30 14:00:05 -0500 |
commit | 829426c71456f9980f42ebd5435c5eee6615dd6f (patch) | |
tree | 45a07b804207056a9e2cafdd9ed756eeb4049b6d | |
parent | 8e1d78e9ded9bd478edca3a638cea2ae4148a551 (diff) | |
download | minetest-829426c71456f9980f42ebd5435c5eee6615dd6f.tar.gz minetest-829426c71456f9980f42ebd5435c5eee6615dd6f.tar.bz2 minetest-829426c71456f9980f42ebd5435c5eee6615dd6f.zip |
Fix InventoryList reading order
Lua does not guarantee that the indexes of a table will be in numerical order.
-rw-r--r-- | src/script/common/c_content.cpp | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index cb5a92ae4..a8f874241 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -884,7 +884,7 @@ void push_items(lua_State *L, const std::vector<ItemStack> &items) } /******************************************************************************/ -std::vector<ItemStack> read_items(lua_State *L, int index,Server* srv) +std::vector<ItemStack> read_items(lua_State *L, int index, Server *srv) { if(index < 0) index = lua_gettop(L) + 1 + index; @@ -892,10 +892,15 @@ std::vector<ItemStack> read_items(lua_State *L, int index,Server* srv) std::vector<ItemStack> items; luaL_checktype(L, index, LUA_TTABLE); lua_pushnil(L); - while(lua_next(L, index) != 0){ - // key at index -2 and value at index -1 - items.push_back(read_item(L, -1, srv)); - // removes value, keeps key for next iteration + while (lua_next(L, index)) { + s32 key = luaL_checkinteger(L, -2); + if (key < 1) { + throw LuaError(NULL, "Invalid inventory list index"); + } + if (items.size() < (u32) key) { + items.resize(key); + } + items[key - 1] = read_item(L, -1, srv); lua_pop(L, 1); } return items; |