Skip to content

Commit

Permalink
Bugfix: Prevent segfault for npc get_restock_interval
Browse files Browse the repository at this point in the history
* Prevents segfault that previously happened when choosing the talk option "Find anything good?" when talking to scrap trader.

Previous problem was caused by:
* `const npc *guy` was `nullptr`
* This was in turn caused by `talker::get_character` returning `nullptr`
* The reason for that `nullptr` was that `const talker &u` was of type `talker_character`, created from `get_talker_for( const Creature &me )` in `creature.cpp`
* `talker_character` previously did not override `get_character`, so it used the default impl from `talker` which always returns `nullptr`.

Therefore, this commit updates `talker_character` so that `get_character` returns a valid pointer.
  • Loading branch information
inogenous authored and detahramet committed Nov 6, 2023
1 parent da2f181 commit 32dd02c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/npctalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2041,8 +2041,9 @@ void parse_tags( std::string &phrase, const talker &u, const talker &me, const d
int price = total_price( u, item_type );
phrase.replace( fa, l, format_money( price ) );
} else if( tag == "<interval>" ) {
const npc *guy = dynamic_cast<const npc *>( &me );
phrase.replace( fa, l, guy->get_restock_interval() );
const npc *guy = dynamic_cast<const npc *>( me_chr );
std::string restock_interval = guy ? guy->get_restock_interval() : _( "a few days" );
phrase.replace( fa, l, restock_interval );
} else if( tag.find( "<u_val:" ) != std::string::npos ) {
//adding a user variable to the string
std::string var = tag.substr( tag.find( ':' ) + 1 );
Expand Down
14 changes: 14 additions & 0 deletions src/talker_character.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ class talker_character_const: public talker_cloner<talker_character_const>
}
~talker_character_const() override = default;

// underlying element accessor functions
Character *get_character() override {
return nullptr;
}
const Character *get_character() const override {
return me_chr_const;
}
Creature *get_creature() override {
return nullptr;
}
const Creature *get_creature() const override {
return me_chr_const;
}

// identity and location
std::string disp_name() const override;
std::string get_name() const override;
Expand Down

0 comments on commit 32dd02c

Please sign in to comment.