From 860b9a65ee0b76a39575e38df5e76e048e5c1fbc Mon Sep 17 00:00:00 2001 From: inogenous <123803852+inogenous@users.noreply.github.com> Date: Mon, 29 Jul 2024 08:20:23 +0200 Subject: [PATCH] Prevent segfault when target of firstaid_activity_actor disappears Prevents segfault when performing `firstaid_activity_actor` but the bandage tool disappeared. Backtrace of fixed segfault: ``` Thread 1 "cataclysm-tiles" received signal SIGSEGV, Segmentation fault. (gdb) bt #0 0x0000555555ece5a4 in item::get_usable_item_helper (use_name="heal", self=...) at src/item.cpp:11584 #1 item::get_usable_item (this=0x0, use_name="heal") at src/item.cpp:11606 #2 0x000055555599c6bf in firstaid_activity_actor::finish (this=0x55559b583f20, act=..., who=...) at src/activity_actor.cpp:6651 #3 0x00005555564d8c6b in player_activity::do_turn (this=0x555558108218, you=...) at src/player_activity.cpp:391 #4 0x0000555555ce3fa6 in do_turn () at src/do_turn.cpp:532 #5 0x000055555577511a in main (argc=, argv=) at src/main.cpp:873 ``` An example where this happened was when the bandage was inside a spillable container (clay canning pot), that spilled its contents when starting the activity. Spiling the clay canning pot invalidates the `item_location`. Backtrace of when the clay canning pot was spilled: ``` #0 item_pocket::get_name (this=0x555557fa7540) at src/item_pocket.cpp:2117 #1 item_pocket::handle_liquid_or_spill (this=this@entry=0x555557fa7540, guy=..., avoid=0x555557fa8060) at src/item_pocket.cpp:806 #2 0x0000555555a6aa60 in avatar_action::use_item (you=..., loc=..., method="heal") at src/avatar_action.cpp:1212 #3 0x0000555555a6dda7 in avatar_action::eat_or_use (you=..., loc=...) at src/avatar_action.cpp:988 #4 0x0000555555e3e1c5 in game::do_regular_action (this=this@entry=0x555557f94a60, act=@0x7fffffffd1b0: ACTION_EAT, player_character=..., mouse_target=std::optional [no contained value]) at src/handle_action.cpp:2479 #5 0x0000555555e41260 in game::handle_action (this=0x555557f94a60) at src/handle_action.cpp:3176 #6 0x0000555555ce422f in do_turn () at /usr/include/c++/13/bits/unique_ptr.h:199 #7 0x000055555577511a in main (argc=, argv=) at src/main.cpp:873 ``` --- src/activity_actor.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/activity_actor.cpp b/src/activity_actor.cpp index ea97bd6145046..238f548f5dce2 100644 --- a/src/activity_actor.cpp +++ b/src/activity_actor.cpp @@ -6331,6 +6331,11 @@ void firstaid_activity_actor::finish( player_activity &act, Character &who ) static const std::string iuse_name_string( "heal" ); item_location it = act.targets.front(); + if( !it ) { + debugmsg( "Lost tool used for healing" ); + act.set_to_null(); + return; + } item *used_tool = it->get_usable_item( iuse_name_string ); if( used_tool == nullptr ) { debugmsg( "Lost tool used for healing" );