diff options
author | Ryan Egesdahl <ryan.egesdahl@mongodb.com> | 2020-05-27 19:29:32 -0700 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-05-29 18:08:35 +0000 |
commit | 078c7d7ee6c47983d5c358ab494840c1b58d8637 (patch) | |
tree | 2c8e0601f16db6f1072e5dc96f69b8cfceb56d10 /src/third_party/SafeInt/SafeInt.hpp | |
parent | da4c3f11362dacf768d9002d167da991303334e1 (diff) | |
download | mongo-078c7d7ee6c47983d5c358ab494840c1b58d8637.tar.gz |
SERVER-44090 Upgrade SafeInt to 3.23
Upgrade SafeInt to version 3.23. This release adds support for safe
negation and fixes some undefined behavior in nonstandard int
multiplication.
Diffstat (limited to 'src/third_party/SafeInt/SafeInt.hpp')
-rw-r--r-- | src/third_party/SafeInt/SafeInt.hpp | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/src/third_party/SafeInt/SafeInt.hpp b/src/third_party/SafeInt/SafeInt.hpp index 2c379727044..dbe2741bf33 100644 --- a/src/third_party/SafeInt/SafeInt.hpp +++ b/src/third_party/SafeInt/SafeInt.hpp @@ -3,7 +3,7 @@ /*----------------------------------------------------------------------------------------------------------- SafeInt.hpp -Version 3.0.20p +Version 3.0.23p This header implements an integer handling class designed to catch unsafe integer operations @@ -565,6 +565,7 @@ SIZE_T_CAST_NEEDED - some compilers complain if there is not a c * */ +// Warning - this very old work-around will be deprecated in future releases. #if defined VISUAL_STUDIO_SAFEINT_COMPAT namespace msl { @@ -1024,6 +1025,17 @@ public: } E::SafeIntOnOverflow(); } + + _CONSTEXPR14 static bool Negative(T t, T& out) + { + // corner case + if (t != std::numeric_limits<T>::min()) + { + out = -t; + return true; + } + return false; + } }; template < typename T > class NegationHelper <T, false> // unsigned @@ -1039,6 +1051,11 @@ public: return (T)SignedNegation<std::int64_t>::Value(t); } + _CONSTEXPR14 static bool Negative(T t, T& /*out*/) + { + // This will only be used by the SafeNegation function + return false; + } }; //core logic to determine casting behavior @@ -1817,7 +1834,7 @@ public: //accepts unsigned, both less than 32-bit _CONSTEXPR14 static bool Multiply( const T& t, const U& u, T& ret ) SAFEINT_NOTHROW { - unsigned int tmp = (unsigned int)(t * u); + unsigned int tmp = (unsigned int)t * (unsigned int)u; if( tmp > std::numeric_limits<T>::max() ) return false; @@ -5597,6 +5614,12 @@ _CONSTEXPR11 inline bool SafeSubtract( T t, U u, T& result ) SAFEINT_NOTHROW return SubtractionHelper< T, U, SubtractionMethod< T, U >::method >::Subtract( t, u, result ); } +template < typename T > +_CONSTEXPR11 inline bool SafeNegation(T t, T& result) SAFEINT_NOTHROW +{ + return NegationHelper< T, std::numeric_limits<T>::is_signed>::Negative(t, result); +} + /***************** end external functions ************************************/ // Main SafeInt class @@ -7094,10 +7117,9 @@ _CONSTEXPR11 SafeInt< T, E > operator |( U lhs, SafeInt< T, E > rhs ) SAFEINT_NO return SafeInt< T, E >( BinaryOrHelper< T, U, BinaryMethod< T, U >::method >::Or( (T)rhs, lhs ) ); } -#endif //SAFEINT_HPP - #if defined VISUAL_STUDIO_SAFEINT_COMPAT } // utilities } // msl #endif +#endif //SAFEINT_HPP |