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

Side by Side Diff: ui/gfx/geometry/rect.h

Issue 2268423003: Adjust gfx::Rect's width and height to avoid integer overflow (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove repeated test case. Created 4 years, 3 months 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 unified diff | Download patch
« no previous file with comments | « no previous file | ui/gfx/geometry/rect_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Defines a simple integer rectangle class. The containment semantics 5 // Defines a simple integer rectangle class. The containment semantics
6 // are array-like; that is, the coordinate (x, y) is considered to be 6 // are array-like; that is, the coordinate (x, y) is considered to be
7 // contained by the rectangle, but the coordinate (x + width, y) is not. 7 // contained by the rectangle, but the coordinate (x + width, y) is not.
8 // The class will happily let you create malformed rectangles (that is, 8 // The class will happily let you create malformed rectangles (that is,
9 // rectangles with negative width and/or height), but there will be assertions 9 // rectangles with negative width and/or height), but there will be assertions
10 // in the operations (such as Contains()) to complain in this case. 10 // in the operations (such as Contains()) to complain in this case.
(...skipping 19 matching lines...) Expand all
30 30
31 namespace gfx { 31 namespace gfx {
32 32
33 class Insets; 33 class Insets;
34 34
35 class GFX_EXPORT Rect { 35 class GFX_EXPORT Rect {
36 public: 36 public:
37 constexpr Rect() = default; 37 constexpr Rect() = default;
38 constexpr Rect(int width, int height) : size_(width, height) {} 38 constexpr Rect(int width, int height) : size_(width, height) {}
39 constexpr Rect(int x, int y, int width, int height) 39 constexpr Rect(int x, int y, int width, int height)
40 : origin_(x, y), size_(width, height) {} 40 : origin_(x, y),
41 size_(GetClampedValue(x, width), GetClampedValue(y, height)) {}
41 constexpr explicit Rect(const Size& size) : size_(size) {} 42 constexpr explicit Rect(const Size& size) : size_(size) {}
42 constexpr Rect(const Point& origin, const Size& size) 43 constexpr Rect(const Point& origin, const Size& size)
43 : origin_(origin), size_(size) {} 44 : origin_(origin),
45 size_(GetClampedValue(origin.x(), size.width()),
46 GetClampedValue(origin.y(), size.height())) {}
44 47
45 #if defined(OS_WIN) 48 #if defined(OS_WIN)
46 explicit Rect(const RECT& r); 49 explicit Rect(const RECT& r);
47 #elif defined(OS_MACOSX) 50 #elif defined(OS_MACOSX)
48 explicit Rect(const CGRect& r); 51 explicit Rect(const CGRect& r);
49 #endif 52 #endif
50 53
51 #if defined(OS_WIN) 54 #if defined(OS_WIN)
52 // Construct an equivalent Win32 RECT object. 55 // Construct an equivalent Win32 RECT object.
53 RECT ToRECT() const; 56 RECT ToRECT() const;
54 #elif defined(OS_MACOSX) 57 #elif defined(OS_MACOSX)
55 // Construct an equivalent CoreGraphics object. 58 // Construct an equivalent CoreGraphics object.
56 CGRect ToCGRect() const; 59 CGRect ToCGRect() const;
57 #endif 60 #endif
58 61
59 constexpr int x() const { return origin_.x(); } 62 constexpr int x() const { return origin_.x(); }
60 void set_x(int x) { origin_.set_x(x); } 63 void set_x(int x) {
64 origin_.set_x(x);
65 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.
66 }
61 67
62 constexpr int y() const { return origin_.y(); } 68 constexpr int y() const { return origin_.y(); }
63 void set_y(int y) { origin_.set_y(y); } 69 void set_y(int y) {
70 origin_.set_y(y);
71 size_.set_height(GetClampedValue(y, size_.height()));
danakj 2016/08/29 19:51:01 and height()
sunxd 2016/08/29 21:23:06 Done.
72 }
64 73
65 constexpr int width() const { return size_.width(); } 74 constexpr int width() const { return size_.width(); }
66 void set_width(int width) { size_.set_width(width); } 75 void set_width(int width) { size_.set_width(GetClampedValue(x(), width)); }
67 76
68 constexpr int height() const { return size_.height(); } 77 constexpr int height() const { return size_.height(); }
69 void set_height(int height) { size_.set_height(height); } 78 void set_height(int height) {
79 size_.set_height(GetClampedValue(y(), height));
80 }
70 81
71 constexpr const Point& origin() const { return origin_; } 82 constexpr const Point& origin() const { return origin_; }
72 void set_origin(const Point& origin) { origin_ = origin; } 83 void set_origin(const Point& origin) {
84 origin_ = origin;
85 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.
86 set_height(height());
87 }
73 88
74 constexpr const Size& size() const { return size_; } 89 constexpr const Size& size() const { return size_; }
75 void set_size(const Size& size) { size_ = size; } 90 void set_size(const Size& size) {
91 set_width(size.width());
92 set_height(size.height());
93 }
76 94
77 constexpr int right() const { return x() + width(); } 95 constexpr int right() const { return x() + width(); }
78 constexpr int bottom() const { return y() + height(); } 96 constexpr int bottom() const { return y() + height(); }
79 97
80 constexpr Point top_right() const { return Point(right(), y()); } 98 constexpr Point top_right() const { return Point(right(), y()); }
81 constexpr Point bottom_left() const { return Point(x(), bottom()); } 99 constexpr Point bottom_left() const { return Point(x(), bottom()); }
82 constexpr Point bottom_right() const { return Point(right(), bottom()); } 100 constexpr Point bottom_right() const { return Point(right(), bottom()); }
83 101
84 Vector2d OffsetFromOrigin() const { return Vector2d(x(), y()); } 102 Vector2d OffsetFromOrigin() const { return Vector2d(x(), y()); }
85 103
86 void SetRect(int x, int y, int width, int height) { 104 void SetRect(int x, int y, int width, int height) {
87 origin_.SetPoint(x, y); 105 origin_.SetPoint(x, y);
88 size_.SetSize(width, height); 106 size_.SetSize(width, height);
danakj 2016/08/29 19:51:01 here?
sunxd 2016/08/29 21:23:06 Done.
89 } 107 }
90 108
91 // Shrink the rectangle by a horizontal and vertical distance on all sides. 109 // Shrink the rectangle by a horizontal and vertical distance on all sides.
92 void Inset(int horizontal, int vertical) { 110 void Inset(int horizontal, int vertical) {
93 Inset(horizontal, vertical, horizontal, vertical); 111 Inset(horizontal, vertical, horizontal, vertical);
94 } 112 }
95 113
96 // Shrink the rectangle by the given insets. 114 // Shrink the rectangle by the given insets.
97 void Inset(const Insets& insets); 115 void Inset(const Insets& insets);
98 116
99 // Shrink the rectangle by the specified amount on each side. 117 // Shrink the rectangle by the specified amount on each side.
100 void Inset(int left, int top, int right, int bottom); 118 void Inset(int left, int top, int right, int bottom);
danakj 2016/08/29 19:51:01 Inset calls set_width/height so that's good.
sunxd 2016/08/29 21:23:06 Done.
101 119
102 // Move the rectangle by a horizontal and vertical distance. 120 // Move the rectangle by a horizontal and vertical distance.
103 void Offset(int horizontal, int vertical); 121 void Offset(int horizontal, int vertical);
danakj 2016/08/29 19:51:00 Offset calls operator+= on origin_ but needs to ve
104 void Offset(const Vector2d& distance) { Offset(distance.x(), distance.y()); } 122 void Offset(const Vector2d& distance) { Offset(distance.x(), distance.y()); }
105 void operator+=(const Vector2d& offset); 123 void operator+=(const Vector2d& offset);
danakj 2016/08/29 19:51:01 ditto for operator+=
106 void operator-=(const Vector2d& offset); 124 void operator-=(const Vector2d& offset);
107 125
108 Insets InsetsFrom(const Rect& inner) const; 126 Insets InsetsFrom(const Rect& inner) const;
109 127
110 // Returns true if the area of the rectangle is zero. 128 // Returns true if the area of the rectangle is zero.
111 bool IsEmpty() const { return size_.IsEmpty(); } 129 bool IsEmpty() const { return size_.IsEmpty(); }
112 130
113 // A rect is less than another rect if its origin is less than 131 // A rect is less than another rect if its origin is less than
114 // the other rect's origin. If the origins are equal, then the 132 // the other rect's origin. If the origins are equal, then the
115 // shortest rect is less than the other. If the origin and the 133 // shortest rect is less than the other. If the origin and the
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 // returns the smallest non-zero value appropriate for int. 195 // returns the smallest non-zero value appropriate for int.
178 int ManhattanInternalDistance(const Rect& rect) const; 196 int ManhattanInternalDistance(const Rect& rect) const;
179 197
180 std::string ToString() const; 198 std::string ToString() const;
181 199
182 bool ApproximatelyEqual(const Rect& rect, int tolerance) const; 200 bool ApproximatelyEqual(const Rect& rect, int tolerance) const;
183 201
184 private: 202 private:
185 gfx::Point origin_; 203 gfx::Point origin_;
186 gfx::Size size_; 204 gfx::Size size_;
205
206 constexpr int GetClampedValue(int origin, int size) const {
207 return origin > 0 && std::numeric_limits<int>::max() - origin < size
208 ? std::numeric_limits<int>::max() - origin
209 : size;
210 }
187 }; 211 };
188 212
189 inline bool operator==(const Rect& lhs, const Rect& rhs) { 213 inline bool operator==(const Rect& lhs, const Rect& rhs) {
190 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size(); 214 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size();
191 } 215 }
192 216
193 inline bool operator!=(const Rect& lhs, const Rect& rhs) { 217 inline bool operator!=(const Rect& lhs, const Rect& rhs) {
194 return !(lhs == rhs); 218 return !(lhs == rhs);
195 } 219 }
196 220
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 } 294 }
271 295
272 // This is declared here for use in gtest-based unit tests but is defined in 296 // This is declared here for use in gtest-based unit tests but is defined in
273 // the //ui/gfx:test_support target. Depend on that to use this in your unit 297 // the //ui/gfx:test_support target. Depend on that to use this in your unit
274 // test. This should not be used in production code - call ToString() instead. 298 // test. This should not be used in production code - call ToString() instead.
275 void PrintTo(const Rect& rect, ::std::ostream* os); 299 void PrintTo(const Rect& rect, ::std::ostream* os);
276 300
277 } // namespace gfx 301 } // namespace gfx
278 302
279 #endif // UI_GFX_GEOMETRY_RECT_H_ 303 #endif // UI_GFX_GEOMETRY_RECT_H_
OLDNEW
« no previous file with comments | « no previous file | ui/gfx/geometry/rect_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698