Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix use-after-free in Browse Field handler #4862

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ Container::Container(Tile* tile) : Container(ITEM_BROWSEFIELD, 30, false, true)
TileItemVector* itemVector = tile->getItemList();
if (itemVector) {
for (Item* item : *itemVector) {
if ((item->getContainer() || item->hasProperty(CONST_PROP_MOVEABLE)) &&
!item->hasAttribute(ITEM_ATTRIBUTE_UNIQUEID)) {
if (item->shouldTriggerBrowseFieldUpdate() && !item->hasAttribute(ITEM_ATTRIBUTE_UNIQUEID)) {
itemlist.push_front(item);
item->setParent(this);
}
Expand Down
2 changes: 2 additions & 0 deletions src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,8 @@ class Item : virtual public Thing
return attributes;
}

bool shouldTriggerBrowseFieldUpdate() const { return getContainer() || hasProperty(CONST_PROP_MOVEABLE); }

void incrementReferenceCounter() { ++referenceCounter; }
void decrementReferenceCounter()
{
Expand Down
13 changes: 8 additions & 5 deletions src/tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ Thing* Tile::getTopVisibleThing(const Creature* creature)

void Tile::onAddTileItem(Item* item)
{
if (item->hasProperty(CONST_PROP_MOVEABLE) || item->getContainer()) {
if (item->shouldTriggerBrowseFieldUpdate()) {
auto it = g_game.browseFields.find(this);
if (it != g_game.browseFields.end()) {
it->second->addItemBack(item);
Expand Down Expand Up @@ -386,7 +386,7 @@ void Tile::onAddTileItem(Item* item)

void Tile::onUpdateTileItem(Item* oldItem, const ItemType& oldType, Item* newItem, const ItemType& newType)
{
if (newItem->hasProperty(CONST_PROP_MOVEABLE) || newItem->getContainer()) {
if (newItem->shouldTriggerBrowseFieldUpdate()) {
auto it = g_game.browseFields.find(this);
if (it != g_game.browseFields.end()) {
int32_t index = it->second->getThingIndex(oldItem);
Expand All @@ -395,7 +395,7 @@ void Tile::onUpdateTileItem(Item* oldItem, const ItemType& oldType, Item* newIte
newItem->setParent(it->second);
}
}
} else if (oldItem->hasProperty(CONST_PROP_MOVEABLE) || oldItem->getContainer()) {
} else if (oldItem->shouldTriggerBrowseFieldUpdate()) {
auto it = g_game.browseFields.find(this);
if (it != g_game.browseFields.end()) {
Cylinder* oldParent = oldItem->getParent();
Expand Down Expand Up @@ -424,7 +424,7 @@ void Tile::onUpdateTileItem(Item* oldItem, const ItemType& oldType, Item* newIte

void Tile::onRemoveTileItem(const SpectatorVec& spectators, const std::vector<int32_t>& oldStackPosVector, Item* item)
{
if (item->hasProperty(CONST_PROP_MOVEABLE) || item->getContainer()) {
if (item->shouldTriggerBrowseFieldUpdate()) {
auto it = g_game.browseFields.find(this);
if (it != g_game.browseFields.end()) {
it->second->removeThing(item, item->getItemCount());
Expand Down Expand Up @@ -967,13 +967,16 @@ void Tile::updateThing(Thing* thing, uint16_t itemId, uint32_t count)
return /*RETURNVALUE_NOTPOSSIBLE*/;
}

Item* realOldItem = item->clone();

const ItemType& oldType = Item::items[item->getID()];
const ItemType& newType = Item::items[itemId];

resetTileFlags(item);
item->setID(itemId);
item->setSubType(count);
setTileFlags(item);
onUpdateTileItem(item, oldType, item, newType);
onUpdateTileItem(realOldItem, oldType, item, newType);
}

void Tile::replaceThing(uint32_t index, Thing* thing)
Expand Down
Loading