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

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

Issue 2354783004: Fix overflow/underflow in gfx geometry once and for all (Closed)
Patch Set: Remove accessibility test changes Created 4 years, 2 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 | « ui/gfx/geometry/rect.h ('k') | 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 #include "ui/gfx/geometry/rect.h" 5 #include "ui/gfx/geometry/rect.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #if defined(OS_WIN) 9 #if defined(OS_WIN)
10 #include <windows.h> 10 #include <windows.h>
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 } // namespace 58 } // namespace
59 59
60 namespace gfx { 60 namespace gfx {
61 61
62 void Rect::Inset(const Insets& insets) { 62 void Rect::Inset(const Insets& insets) {
63 Inset(insets.left(), insets.top(), insets.right(), insets.bottom()); 63 Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
64 } 64 }
65 65
66 void Rect::Inset(int left, int top, int right, int bottom) { 66 void Rect::Inset(int left, int top, int right, int bottom) {
67 origin_ += Vector2d(left, top); 67 origin_ += Vector2d(left, top);
68 set_width(std::max(width() - left - right, static_cast<int>(0))); 68 // left+right might overflow/underflow, but width() - (left+right) might
69 set_height(std::max(height() - top - bottom, static_cast<int>(0))); 69 // overflow as well.
70 set_width(SafeSubtract(width(), SafeAdd(left, right)));
71 set_height(SafeSubtract(height(), SafeAdd(top, bottom)));
70 } 72 }
71 73
72 void Rect::Offset(int horizontal, int vertical) { 74 void Rect::Offset(int horizontal, int vertical) {
73 origin_ += Vector2d(horizontal, vertical); 75 origin_ += Vector2d(horizontal, vertical);
74 // Ensure that width and height remain valid. 76 // Ensure that width and height remain valid.
75 set_width(width()); 77 set_width(width());
76 set_height(height()); 78 set_height(height());
77 } 79 }
78 80
79 void Rect::operator+=(const Vector2d& offset) { 81 void Rect::operator+=(const Vector2d& offset) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 return; 146 return;
145 } 147 }
146 if (rect.IsEmpty()) 148 if (rect.IsEmpty())
147 return; 149 return;
148 150
149 int rx = std::min(x(), rect.x()); 151 int rx = std::min(x(), rect.x());
150 int ry = std::min(y(), rect.y()); 152 int ry = std::min(y(), rect.y());
151 int rr = std::max(right(), rect.right()); 153 int rr = std::max(right(), rect.right());
152 int rb = std::max(bottom(), rect.bottom()); 154 int rb = std::max(bottom(), rect.bottom());
153 155
154 SetRect(rx, ry, rr - rx, rb - ry); 156 // Subtracting to get width/height might overflow integers, so clamp them.
157 SetRect(rx, ry, GetClampedWidthFromExtents(rx, rr),
158 GetClampedWidthFromExtents(ry, rb));
155 } 159 }
156 160
157 void Rect::Subtract(const Rect& rect) { 161 void Rect::Subtract(const Rect& rect) {
158 if (!Intersects(rect)) 162 if (!Intersects(rect))
159 return; 163 return;
160 if (rect.Contains(*this)) { 164 if (rect.Contains(*this)) {
161 SetRect(0, 0, 0, 0); 165 SetRect(0, 0, 0, 0);
162 return; 166 return;
163 } 167 }
164 168
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 294
291 Rect BoundingRect(const Point& p1, const Point& p2) { 295 Rect BoundingRect(const Point& p1, const Point& p2) {
292 int rx = std::min(p1.x(), p2.x()); 296 int rx = std::min(p1.x(), p2.x());
293 int ry = std::min(p1.y(), p2.y()); 297 int ry = std::min(p1.y(), p2.y());
294 int rr = std::max(p1.x(), p2.x()); 298 int rr = std::max(p1.x(), p2.x());
295 int rb = std::max(p1.y(), p2.y()); 299 int rb = std::max(p1.y(), p2.y());
296 return Rect(rx, ry, rr - rx, rb - ry); 300 return Rect(rx, ry, rr - rx, rb - ry);
297 } 301 }
298 302
299 } // namespace gfx 303 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/geometry/rect.h ('k') | ui/gfx/geometry/rect_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698