Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Unified Diff: base/numerics/safe_math_impl.h

Issue 2537773009: Fix a bunch of undefined behavior in CheckedNumericState (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/numerics/safe_math_impl.h
diff --git a/base/numerics/safe_math_impl.h b/base/numerics/safe_math_impl.h
index 15de619e76fae11ce35d8ecac5b950c4e7ba432b..239c78d4d3d631bd01d9a90dbba98ce2fd853678 100644
--- a/base/numerics/safe_math_impl.h
+++ b/base/numerics/safe_math_impl.h
@@ -800,19 +800,19 @@ class CheckedNumericState {};
template <typename T>
class CheckedNumericState<T, NUMERIC_INTEGER> {
private:
- T value_;
bool is_valid_;
+ T value_;
public:
template <typename Src, NumericRepresentation type>
friend class CheckedNumericState;
- constexpr CheckedNumericState() : value_(0), is_valid_(true) {}
+ constexpr CheckedNumericState() : is_valid_(true), value_(0) {}
template <typename Src>
constexpr CheckedNumericState(Src value, bool is_valid)
- : value_(static_cast<T>(value)),
- is_valid_(is_valid && IsValueInRangeForNumericType<T>(value)) {
+ : is_valid_(is_valid && IsValueInRangeForNumericType<T>(value)),
+ value_(is_valid_ ? static_cast<T>(value) : 0) {
static_assert(std::numeric_limits<Src>::is_specialized,
"Argument must be numeric.");
}
@@ -820,15 +820,15 @@ class CheckedNumericState<T, NUMERIC_INTEGER> {
// Copy constructor.
template <typename Src>
constexpr CheckedNumericState(const CheckedNumericState<Src>& rhs)
- : value_(static_cast<T>(rhs.value())), is_valid_(rhs.IsValid()) {}
+ : is_valid_(rhs.IsValid()), value_(static_cast<T>(rhs.value())) {}
Nico 2016/12/02 16:46:10 This one probably needs this check too, still veri
jschuh 2016/12/02 17:19:15 Agreed, it will.
template <typename Src>
constexpr explicit CheckedNumericState(
Src value,
typename std::enable_if<std::numeric_limits<Src>::is_specialized,
int>::type = 0)
- : value_(static_cast<T>(value)),
- is_valid_(IsValueInRangeForNumericType<T>(value)) {}
+ : is_valid_(IsValueInRangeForNumericType<T>(value)),
+ value_(is_valid_ ? static_cast<T>(value) : 0) {}
constexpr bool is_valid() const { return is_valid_; }
constexpr T value() const { return value_; }
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698