Skip to content

Commit 90e7832

Browse files
sapierPilzAdam
sapier
authored andcommittedNov 16, 2013
Fix invalid listname and listsize not handled correctly in set_size
1 parent 35606cf commit 90e7832

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed
 

‎doc/lua_api.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,7 @@ methods:
17261726
- is_empty(listname): return true if list is empty
17271727
- get_size(listname): get size of a list
17281728
- set_size(listname, size): set size of a list
1729+
^ returns false on error (e.g. invalid listname or listsize)
17291730
- get_width(listname): get width of a list
17301731
- set_width(listname, width): set width of list; currently used for crafting
17311732
- get_stack(listname, i): get a copy of stack index i in list

‎src/inventory.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,9 @@ InventoryList * Inventory::addList(const std::string &name, u32 size)
957957
}
958958
else
959959
{
960+
//don't create list with invalid name
961+
if (name.find(" ") != std::string::npos) return NULL;
962+
960963
InventoryList *list = new InventoryList(name, size, m_itemdef);
961964
m_lists.push_back(list);
962965
return list;

‎src/script/lua_api/l_inventory.cpp

+17-3
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,38 @@ int InvRef::l_set_size(lua_State *L)
117117
NO_MAP_LOCK_REQUIRED;
118118
InvRef *ref = checkobject(L, 1);
119119
const char *listname = luaL_checkstring(L, 2);
120+
120121
int newsize = luaL_checknumber(L, 3);
122+
if (newsize < 0) {
123+
lua_pushboolean(L, false);
124+
return 1;
125+
}
126+
121127
Inventory *inv = getinv(L, ref);
122128
if(inv == NULL){
123-
return 0;
129+
lua_pushboolean(L, false);
130+
return 1;
124131
}
125132
if(newsize == 0){
126133
inv->deleteList(listname);
127134
reportInventoryChange(L, ref);
128-
return 0;
135+
lua_pushboolean(L, true);
136+
return 1;
129137
}
130138
InventoryList *list = inv->getList(listname);
131139
if(list){
132140
list->setSize(newsize);
133141
} else {
134142
list = inv->addList(listname, newsize);
143+
if (!list)
144+
{
145+
lua_pushboolean(L, false);
146+
return 1;
147+
}
135148
}
136149
reportInventoryChange(L, ref);
137-
return 0;
150+
lua_pushboolean(L, true);
151+
return 1;
138152
}
139153

140154
// set_width(self, listname, size)

0 commit comments

Comments
 (0)
Please sign in to comment.