Skip to content

Commit

Permalink
Fix undefined behavior in recalc_hp (#36470)
Browse files Browse the repository at this point in the history
This is triggered during character creation.
src/character.cpp:1114:66: runtime error: division by zero
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/character.cpp:1114:66 in 
src/character.cpp:1116:21: runtime error: -nan is outside the range of representable values of type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/character.cpp:1116:21 in
  • Loading branch information
ZhilkinSerg authored Dec 26, 2019
2 parents 1f92726 + b9ef7ab commit 9e77218
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,10 @@ 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;
}
float max_hp_ratio = static_cast<float>( new_max_hp[i] ) /
static_cast<float>( hp_max[i] );
hp_cur[i] = std::ceil( static_cast<float>( hp_cur[i] ) * max_hp_ratio );
Expand Down

0 comments on commit 9e77218

Please sign in to comment.