Repository navigation
Fix null ammo_data dereferences in NPC reload and turret code - #86253
Merged
Maleclypse merged 1 commit intoApr 5, 2026
Merged
Conversation
- npc::do_reload: use reload_option::qty() instead of manually computing quantity via ammo_data()->ammo->type. Crashes when select_ammo returns a non-ammo item (e.g. food for containers). - turret_data::can_reload: use remaining_ammo_capacity() > 0 instead of raw ammo_data()->ammo->type dereference. - item::remaining_ammo_capacity: guard against null ammo_data() and null islot_ammo, return 0 instead of crashing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Bugfixes "Fix segfault in NPC reload when ammo has no ammo_data"
Purpose of change
NPC tries to reload a container with food (e.g. peanut butter),
select_ammoreturns it as valid ammo, thendo_reloadcallsusable_ammo->ammo_data()->ammo->typewhich segfaults because comestibles have noislot_ammo. Same unguardedammo_data()->ammo->typepattern exists inturret_data::can_reloadanditem::remaining_ammo_capacity.Describe the solution
npc::do_reload: usereload_option::qty()which already handles all reload types correctly (containers, speedloaders, liquids, regular ammo) instead of reimplementing the quantity computation with rawammo_data()access.turret_data::can_reload: replaceammo_remaining() < ammo_capacity(ammo_data()->ammo->type)withremaining_ammo_capacity() > 0.item::remaining_ammo_capacity: add null guards forammo_data()andislot_ammo, return 0 instead of crashing.Describe alternatives you've considered
Could guard
select_ammoto never return non-ammo items, but container reloads are a legitimate use of the reload system -- the problem isdo_reloadassuming gun/tool-only ammo.Testing
Reproduced the crash with a save where an NPC companion attempts to reload a container with peanut butter. After the fix, the NPC reload proceeds without crashing.
Additional context
N/A