The vulnerability as described in ISO/IEC TR 24772-1 clause 6.4 is applicable to C++.
The C++ standard assumes IEC 60559 if std::numeric_limits\<T>::is_iec559
is true for the types in use. In the absence of this, C++ makes few guarantees about the behaviour of floating point numbers. In particular std::less
is not a total order; std::equal
is not equivalent to substitutability (NaNs compare unequal to themselves, but neither less nor greater, and negative zero compares equal to positive zero).
Sorting floating point numbers with the built-in operators violates the preconditions of sorting predicates in the presence of NaN values and may raise floating point errors. The default sorting predicate std::less
is suspect to this precondition violation, resulting in undefined behavior [EWF] when sorting a range of floating point values that contains NaNs.
To avoid the vulnerability or mitigate its ill effects, C++ software developers can:
Follow the avoidance mechanisms of ISO/IEC 24772-1 clause 6.4.5
Verify compliance to ISO/IEC/IEEE 60559:2011 at compile time through std::numeric_limits<T>::is_iec559
. Other numeric characteristics such as min()
, max()
, existence of NaNs, has_denorm
, and infinities can be determined in this class template.
Be aware that the default comparisons in the standard library may produce wrong results when used on floating point members.