Skip to content

Fix null ammo_data dereferences in NPC reload and turret code - #86253

Merged
Maleclypse merged 1 commit into
CleverRaven:masterfrom
dobbry-vechur:fix/npc-reload-crash-ammo-data
Apr 5, 2026
Merged

Maleclypse merged 1 commit into
CleverRaven:masterfrom
dobbry-vechur:fix/npc-reload-crash-ammo-data

Conversation

@dobbry-vechur

Copy link
Copy Markdown
Contributor

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_ammo returns it as valid ammo, then do_reload calls usable_ammo->ammo_data()->ammo->type which segfaults because comestibles have no islot_ammo. Same unguarded ammo_data()->ammo->type pattern exists in turret_data::can_reload and item::remaining_ammo_capacity.

Describe the solution

  • npc::do_reload: use reload_option::qty() which already handles all reload types correctly (containers, speedloaders, liquids, regular ammo) instead of reimplementing the quantity computation with raw ammo_data() access.
  • turret_data::can_reload: replace ammo_remaining() < ammo_capacity(ammo_data()->ammo->type) with remaining_ammo_capacity() > 0.
  • item::remaining_ammo_capacity: add null guards for ammo_data() and islot_ammo, return 0 instead of crashing.

Describe alternatives you've considered

Could guard select_ammo to never return non-ammo items, but container reloads are a legitimate use of the reload system -- the problem is do_reload assuming 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

- 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.
@github-actions github-actions Bot added <Bugfix> This is a fix for a bug (or closes open issue) NPC / Factions NPCs, AI, Speech, Factions, Ownership [C++] Changes (can be) made in C++. Previously named `Code` Items: Ammo / Guns Ammunition for all kinds of weapons and these weapons themselves astyled astyled PR, label is assigned by github actions json-styled JSON lint passed, label assigned by github actions BasicBuildPassed This PR builds correctly, label assigned by github actions labels Apr 5, 2026
@Maleclypse
Maleclypse merged commit 6fcd703 into CleverRaven:master Apr 5, 2026
34 checks passed
@dobbry-vechur
dobbry-vechur deleted the fix/npc-reload-crash-ammo-data branch May 25, 2026 00:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

astyled astyled PR, label is assigned by github actions BasicBuildPassed This PR builds correctly, label assigned by github actions <Bugfix> This is a fix for a bug (or closes open issue) [C++] Changes (can be) made in C++. Previously named `Code` Items: Ammo / Guns Ammunition for all kinds of weapons and these weapons themselves json-styled JSON lint passed, label assigned by github actions NPC / Factions NPCs, AI, Speech, Factions, Ownership

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants