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 NPC wielding items from within holsters and the like #36506

Merged
merged 2 commits into from
Dec 30, 2019

Conversation

BevapDin
Copy link
Contributor

SUMMARY: None

The culprit is in npc::wield. If the item to wield is a non-empty holster, it invokes that item. This will usually wield the contained item.

But npc::wield continues to wield the originally given item (the contained item). This will effectively wield the item twice.

However, the item reference given to npc::wield is a reference to the contained item (an entry in item::contents). This reference is invalid after the item has been wielded from the holster. So the remaining code would invoke UB.


Hopefully fixes issues like #35140 (I can't reproduce the issue from the linked save).

- Check result of `inventory::find_item` before using it. Using `INT_MIN` is safe (`inventory::find_item` will return a null item reference), but this is clearer.
- Add assertions to catch future errors.
- Compare item pointers, not names.
@ZhilkinSerg ZhilkinSerg added <Bugfix> This is a fix for a bug (or closes open issue) NPC / Factions NPCs, AI, Speech, Factions, Ownership labels Dec 29, 2019
@kevingranade kevingranade merged commit a0d976e into CleverRaven:master Dec 30, 2019
@BevapDin BevapDin deleted the ucw branch December 30, 2019 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
<Bugfix> This is a fix for a bug (or closes open issue) NPC / Factions NPCs, AI, Speech, Factions, Ownership
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants