Skip to content

Commit

Permalink
Fix invalid listname and listsize not handled correctly in set_size
Browse files Browse the repository at this point in the history
  • Loading branch information
sapier authored and PilzAdam committed Nov 16, 2013
1 parent 35606cf commit 90e7832
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/lua_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,7 @@ methods:
- is_empty(listname): return true if list is empty
- get_size(listname): get size of a list
- set_size(listname, size): set size of a list
^ returns false on error (e.g. invalid listname or listsize)
- get_width(listname): get width of a list
- set_width(listname, width): set width of list; currently used for crafting
- get_stack(listname, i): get a copy of stack index i in list
Expand Down
3 changes: 3 additions & 0 deletions src/inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,9 @@ InventoryList * Inventory::addList(const std::string &name, u32 size)
}
else
{
//don't create list with invalid name
if (name.find(" ") != std::string::npos) return NULL;

InventoryList *list = new InventoryList(name, size, m_itemdef);
m_lists.push_back(list);
return list;
Expand Down
20 changes: 17 additions & 3 deletions src/script/lua_api/l_inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,38 @@ int InvRef::l_set_size(lua_State *L)
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);

int newsize = luaL_checknumber(L, 3);
if (newsize < 0) {
lua_pushboolean(L, false);
return 1;
}

Inventory *inv = getinv(L, ref);
if(inv == NULL){
return 0;
lua_pushboolean(L, false);
return 1;
}
if(newsize == 0){
inv->deleteList(listname);
reportInventoryChange(L, ref);
return 0;
lua_pushboolean(L, true);
return 1;
}
InventoryList *list = inv->getList(listname);
if(list){
list->setSize(newsize);
} else {
list = inv->addList(listname, newsize);
if (!list)
{
lua_pushboolean(L, false);
return 1;
}
}
reportInventoryChange(L, ref);
return 0;
lua_pushboolean(L, true);
return 1;
}

// set_width(self, listname, size)
Expand Down

0 comments on commit 90e7832

Please sign in to comment.