From d136e464257262858600e8851cb04d58ddf1962b Mon Sep 17 00:00:00 2001 From: Kevin Granade Date: Fri, 27 Dec 2019 07:36:54 +0000 Subject: [PATCH] Properly avoid division by zero by forcing max_hp to stay at 1 or higher --- src/character.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 8488082f42ab7..eee5bd5c8c1ea 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -210,8 +210,6 @@ static const species_id HUMAN( "HUMAN" ); Character::Character() : visitable(), - hp_cur( {{ 0 }} ), - hp_max( {{ 0 }} ), damage_bandaged( {{ 0 }} ), damage_disinfected( {{ 0 }} ), cached_time( calendar::before_time_starts ), @@ -219,6 +217,8 @@ Character::Character() : next_climate_control_check( calendar::before_time_starts ), last_climate_control_ret( false ) { + hp_cur.fill( 0 ); + hp_max.fill( 1 ); str_max = 0; dex_max = 0; per_max = 0; @@ -1111,10 +1111,8 @@ void Character::recalc_hp() if( new_max_hp[i] == hp_max[i] ) { continue; } - // Avoid NaN because converting NaN to int is undefined behavior. - if( hp_max[i] == 0 ) { - continue; - } + // hp_max must be positive to avoiud undefined behavior. + hp_max[i] = std::max( hp_max[i], 1 ); float max_hp_ratio = static_cast( new_max_hp[i] ) / static_cast( hp_max[i] ); hp_cur[i] = std::ceil( static_cast( hp_cur[i] ) * max_hp_ratio ); @@ -4042,6 +4040,7 @@ int Character::blood_loss( body_part bp ) const } hp_cur_sum = std::min( hp_max_sum, std::max( 0, hp_cur_sum ) ); + hp_max_sum = std::max( hp_max_sum, 1 ); return 100 - ( 100 * hp_cur_sum ) / hp_max_sum; }