Skip to content

Commit

Permalink
Prevent attempt to removing an item from itself.
Browse files Browse the repository at this point in the history
The code calls `visitable::visit_items` and adds all items to be removed as reported by the lambda to the vector and later calls `visitable::remove_item` for all those listed items. But `visit_items` will visit `this` as well. So the vector ends up containing `this`, which is subsequently given to `remove_item`, which complains (correctly) that `this` is not contains in `this`.
  • Loading branch information
BevapDin committed May 3, 2020
1 parent cf7473e commit 1ed056b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9488,9 +9488,13 @@ bool item::process( player *carrier, const tripoint &pos, bool activate, float i
return VisitResponse::NEXT;
} );
for( item *it : removed_items ) {
remove_item( *it );
// remove_item can not remove `this` itself, so don't even try.
if( it != this ) {
remove_item( *it );
}
}
return !removed_items.empty();
// Inform the caller that they need to remove `this`.
return std::find( removed_items.begin(), removed_items.end(), this ) != removed_items.end();
}

bool item::process_internal( player *carrier, const tripoint &pos, bool activate,
Expand Down

0 comments on commit 1ed056b

Please sign in to comment.