Index: ui/gfx/geometry/rect.h |
diff --git a/ui/gfx/geometry/rect.h b/ui/gfx/geometry/rect.h |
index d9b3165bf82418ce3937dc63b443fb1a5ff71f49..2f7b037e41576f58c3a26f3f67fd9c84342dc14d 100644 |
--- a/ui/gfx/geometry/rect.h |
+++ b/ui/gfx/geometry/rect.h |
@@ -206,19 +206,28 @@ class GFX_EXPORT Rect { |
gfx::Point origin_; |
gfx::Size size_; |
+ // Returns true iff a+b would overflow max int. |
+ static constexpr bool AddWouldOverflow(int a, int b) { |
+ // In this function, GCC tries to make optimizations that would only work if |
+ // max - a wouldn't overflow but it isn't smart enough to notice that a > 0. |
+ // So cast everything to unsigned to avoid this. As it is guaranteed that |
+ // max - a and b are both already positive, the cast is a noop. |
+ // |
+ // This is intended to be: a > 0 && max - a < b |
+ return a > 0 && b > 0 && |
+ static_cast<unsigned>(std::numeric_limits<int>::max() - a) < |
+ static_cast<unsigned>(b); |
+ } |
+ |
// Clamp the size to avoid integer overflow in bottom() and right(). |
// This returns the width given an origin and a width. |
+ // TODO(enne): this should probably use base::SaturatedAddition, but that |
+ // function is not a constexpr. |
static constexpr int GetClampedValue(int origin, int size) { |
return AddWouldOverflow(origin, size) |
? std::numeric_limits<int>::max() - origin |
: size; |
} |
- |
- // Returns a clamped width given a right and a left, assuming right > left. |
- static constexpr int GetClampedWidthFromExtents(int left, int right) { |
- return SubtractWouldOverflow(right, left) ? std::numeric_limits<int>::max() |
- : right - left; |
- } |
}; |
inline bool operator==(const Rect& lhs, const Rect& rhs) { |