From 181e6f0177e1fcfaff31ae6fa479628c9b9b7358 Mon Sep 17 00:00:00 2001 From: OgelGames Date: Sun, 21 Apr 2024 01:17:37 +1000 Subject: [PATCH] check for negative width in set_width --- doc/lua_api.md | 1 + src/script/lua_api/l_inventory.cpp | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/doc/lua_api.md b/doc/lua_api.md index 2ba75cbcba..43a103c3f9 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -7456,6 +7456,7 @@ An `InvRef` is a reference to an inventory. * returns `false` on error (e.g. invalid `listname` or `size`) * `get_width(listname)`: get width of a list * `set_width(listname, width)`: set width of list; currently used for crafting + * returns `false` on error (e.g. invalid `listname` or `width`) * `get_stack(listname, i)`: get a copy of stack index `i` in list * `set_stack(listname, i, stack)`: copy `stack` to index `i` in list * `get_list(listname)`: returns full list (list of `ItemStack`s) diff --git a/src/script/lua_api/l_inventory.cpp b/src/script/lua_api/l_inventory.cpp index e7244618cc..311cc600f9 100644 --- a/src/script/lua_api/l_inventory.cpp +++ b/src/script/lua_api/l_inventory.cpp @@ -151,19 +151,28 @@ int InvRef::l_set_width(lua_State *L) NO_MAP_LOCK_REQUIRED; InvRef *ref = checkObject(L, 1); const char *listname = luaL_checkstring(L, 2); + int newwidth = luaL_checknumber(L, 3); + if (newwidth < 0) { + lua_pushboolean(L, false); + return 1; + } + Inventory *inv = getinv(L, ref); if(inv == NULL){ - return 0; + lua_pushboolean(L, false); + return 1; } InventoryList *list = inv->getList(listname); if(list){ list->setWidth(newwidth); } else { - return 0; + lua_pushboolean(L, false); + return 1; } reportInventoryChange(L, ref); - return 0; + lua_pushboolean(L, true); + return 1; } // get_stack(self, listname, i) -> itemstack