Skip to content

Commit

Permalink
Prevent segfault when target of firstaid_activity_actor disappears
Browse files Browse the repository at this point in the history
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<item> (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=<optimized out>, argv=<optimized out>) 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=<optimized out>, argv=<optimized out>) at src/main.cpp:873
```
  • Loading branch information
inogenous committed Jul 30, 2024
1 parent 8062226 commit 6172c11
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/activity_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6648,6 +6648,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" );
Expand Down

0 comments on commit 6172c11

Please sign in to comment.