From b9ef7ab39046ea2dade6600f7842b2fc5eeac82b Mon Sep 17 00:00:00 2001 From: hexagonrecursion <52621858+hexagonrecursion@users.noreply.github.com> Date: Thu, 26 Dec 2019 18:07:32 +0000 Subject: [PATCH] Fix undefined behavior in recalc_hp 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 --- src/character.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/character.cpp b/src/character.cpp index aceba9460129c..8488082f42ab7 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -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( new_max_hp[i] ) / static_cast( hp_max[i] ); hp_cur[i] = std::ceil( static_cast( hp_cur[i] ) * max_hp_ratio );