Index: ui/gfx/geometry/rect.h |
diff --git a/ui/gfx/geometry/rect.h b/ui/gfx/geometry/rect.h |
index 2a5fe8e39d2d0714d893e27dd7ee3fc950480e71..f998a57bc67aada6fd7453fc55a13868b7b6041c 100644 |
--- a/ui/gfx/geometry/rect.h |
+++ b/ui/gfx/geometry/rect.h |
@@ -37,10 +37,13 @@ class GFX_EXPORT Rect { |
constexpr Rect() = default; |
constexpr Rect(int width, int height) : size_(width, height) {} |
constexpr Rect(int x, int y, int width, int height) |
- : origin_(x, y), size_(width, height) {} |
+ : origin_(x, y), |
+ size_(GetClampedValue(x, width), GetClampedValue(y, height)) {} |
constexpr explicit Rect(const Size& size) : size_(size) {} |
constexpr Rect(const Point& origin, const Size& size) |
- : origin_(origin), size_(size) {} |
+ : origin_(origin), |
+ size_(GetClampedValue(origin.x(), size.width()), |
+ GetClampedValue(origin.y(), size.height())) {} |
#if defined(OS_WIN) |
explicit Rect(const RECT& r); |
@@ -57,22 +60,37 @@ class GFX_EXPORT Rect { |
#endif |
constexpr int x() const { return origin_.x(); } |
- void set_x(int x) { origin_.set_x(x); } |
+ void set_x(int x) { |
+ origin_.set_x(x); |
+ size_.set_width(GetClampedValue(x, size_.width())); |
danakj
2016/08/29 19:51:01
nit: you could write this as width() instead of si
sunxd
2016/08/29 21:23:06
Done.
|
+ } |
constexpr int y() const { return origin_.y(); } |
- void set_y(int y) { origin_.set_y(y); } |
+ void set_y(int y) { |
+ origin_.set_y(y); |
+ size_.set_height(GetClampedValue(y, size_.height())); |
danakj
2016/08/29 19:51:01
and height()
sunxd
2016/08/29 21:23:06
Done.
|
+ } |
constexpr int width() const { return size_.width(); } |
- void set_width(int width) { size_.set_width(width); } |
+ void set_width(int width) { size_.set_width(GetClampedValue(x(), width)); } |
constexpr int height() const { return size_.height(); } |
- void set_height(int height) { size_.set_height(height); } |
+ void set_height(int height) { |
+ size_.set_height(GetClampedValue(y(), height)); |
+ } |
constexpr const Point& origin() const { return origin_; } |
- void set_origin(const Point& origin) { origin_ = origin; } |
+ void set_origin(const Point& origin) { |
+ origin_ = origin; |
+ set_width(width()); |
danakj
2016/08/29 19:51:01
Add a comment // Ensure that width remains valid.
sunxd
2016/08/29 21:23:06
Done.
|
+ set_height(height()); |
+ } |
constexpr const Size& size() const { return size_; } |
- void set_size(const Size& size) { size_ = size; } |
+ void set_size(const Size& size) { |
+ set_width(size.width()); |
+ set_height(size.height()); |
+ } |
constexpr int right() const { return x() + width(); } |
constexpr int bottom() const { return y() + height(); } |
@@ -184,6 +202,12 @@ class GFX_EXPORT Rect { |
private: |
gfx::Point origin_; |
gfx::Size size_; |
+ |
+ constexpr int GetClampedValue(int origin, int size) const { |
+ return origin > 0 && std::numeric_limits<int>::max() - origin < size |
+ ? std::numeric_limits<int>::max() - origin |
+ : size; |
+ } |
}; |
inline bool operator==(const Rect& lhs, const Rect& rhs) { |