Skip to content

Commit

Permalink
range check in IsLosslessFloat to avoid undefined double->float cast
Browse files Browse the repository at this point in the history
UBSAN gave in Value.IsLosslessFloat:
include/rapidjson/document.h:981:38: runtime error: value 3.40282e+38 is outside the range of representable values of type 'float'
  • Loading branch information
efidler committed Jun 13, 2016
1 parent 82f1660 commit b9535e9
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/rapidjson/document.h
Original file line number Diff line number Diff line change
@@ -978,6 +978,9 @@ class GenericValue {
bool IsLosslessFloat() const {
if (!IsNumber()) return false;
double a = GetDouble();
if (a < static_cast<double>(-std::numeric_limits<float>::max())
|| a > static_cast<double>(std::numeric_limits<float>::max()))
return false;
double b = static_cast<double>(static_cast<float>(a));
return a >= b && a <= b; // Prevent -Wfloat-equal
}

0 comments on commit b9535e9

Please sign in to comment.