Skip to content

Commit 2db6b07

Browse files
authoredJun 20, 2021
Inventory: show error on invalid list names (#11368)
1 parent b10091b commit 2db6b07

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed
 

‎src/inventory.h

+1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ class Inventory
298298
void serialize(std::ostream &os, bool incremental = false) const;
299299
void deSerialize(std::istream &is);
300300

301+
// Adds a new list or clears and resizes an existing one
301302
InventoryList * addList(const std::string &name, u32 size);
302303
InventoryList * getList(const std::string &name);
303304
const InventoryList * getList(const std::string &name) const;

‎src/script/common/c_content.cpp

+14-12
Original file line numberDiff line numberDiff line change
@@ -1350,26 +1350,28 @@ void read_inventory_list(lua_State *L, int tableindex,
13501350
{
13511351
if(tableindex < 0)
13521352
tableindex = lua_gettop(L) + 1 + tableindex;
1353+
13531354
// If nil, delete list
13541355
if(lua_isnil(L, tableindex)){
13551356
inv->deleteList(name);
13561357
return;
13571358
}
1358-
// Otherwise set list
1359+
1360+
// Get Lua-specified items to insert into the list
13591361
std::vector<ItemStack> items = read_items(L, tableindex,srv);
1360-
int listsize = (forcesize != -1) ? forcesize : items.size();
1362+
size_t listsize = (forcesize > 0) ? forcesize : items.size();
1363+
1364+
// Create or clear list
13611365
InventoryList *invlist = inv->addList(name, listsize);
1362-
int index = 0;
1363-
for(std::vector<ItemStack>::const_iterator
1364-
i = items.begin(); i != items.end(); ++i){
1365-
if(forcesize != -1 && index == forcesize)
1366-
break;
1367-
invlist->changeItem(index, *i);
1368-
index++;
1366+
if (!invlist) {
1367+
luaL_error(L, "inventory list: cannot create list named '%s'", name);
1368+
return;
13691369
}
1370-
while(forcesize != -1 && index < forcesize){
1371-
invlist->deleteItem(index);
1372-
index++;
1370+
1371+
for (size_t i = 0; i < items.size(); ++i) {
1372+
if (i == listsize)
1373+
break; // Truncate provided list of items
1374+
invlist->changeItem(i, items[i]);
13731375
}
13741376
}
13751377

0 commit comments

Comments
 (0)
Please sign in to comment.