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

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

Issue 2354783004: Fix overflow/underflow in gfx geometry once and for all (Closed)
Patch Set: 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
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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 57
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);
enne (OOO) 2016/09/21 00:48:24 Adding and subtracting to vectors and points and s
68 set_width(std::max(width() - left - right, static_cast<int>(0))); 68 // left+right might overflow/underflow, but width() - (left+right) might too.
69 set_height(std::max(height() - top - bottom, static_cast<int>(0))); 69 set_width(SafeSubtract(width(), SafeAdd(left, right)));
70 set_height(SafeSubtract(height(), SafeAdd(top, bottom)));
70 } 71 }
71 72
72 void Rect::Offset(int horizontal, int vertical) { 73 void Rect::Offset(int horizontal, int vertical) {
73 origin_ += Vector2d(horizontal, vertical); 74 origin_ += Vector2d(horizontal, vertical);
74 // Ensure that width and height remain valid. 75 // Ensure that width and height remain valid.
75 set_width(width()); 76 set_width(width());
76 set_height(height()); 77 set_height(height());
77 } 78 }
78 79
79 void Rect::operator+=(const Vector2d& offset) { 80 void Rect::operator+=(const Vector2d& offset) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 return; 145 return;
145 } 146 }
146 if (rect.IsEmpty()) 147 if (rect.IsEmpty())
147 return; 148 return;
148 149
149 int rx = std::min(x(), rect.x()); 150 int rx = std::min(x(), rect.x());
150 int ry = std::min(y(), rect.y()); 151 int ry = std::min(y(), rect.y());
151 int rr = std::max(right(), rect.right()); 152 int rr = std::max(right(), rect.right());
152 int rb = std::max(bottom(), rect.bottom()); 153 int rb = std::max(bottom(), rect.bottom());
153 154
154 SetRect(rx, ry, rr - rx, rb - ry); 155 // Subtracting to get width/height might overflow integers, so clamp them.
156 SetRect(rx, ry, GetClampedWidthFromExtents(rx, rr),
157 GetClampedWidthFromExtents(ry, rb));
155 } 158 }
156 159
157 void Rect::Subtract(const Rect& rect) { 160 void Rect::Subtract(const Rect& rect) {
158 if (!Intersects(rect)) 161 if (!Intersects(rect))
159 return; 162 return;
160 if (rect.Contains(*this)) { 163 if (rect.Contains(*this)) {
161 SetRect(0, 0, 0, 0); 164 SetRect(0, 0, 0, 0);
162 return; 165 return;
163 } 166 }
164 167
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 293
291 Rect BoundingRect(const Point& p1, const Point& p2) { 294 Rect BoundingRect(const Point& p1, const Point& p2) {
292 int rx = std::min(p1.x(), p2.x()); 295 int rx = std::min(p1.x(), p2.x());
293 int ry = std::min(p1.y(), p2.y()); 296 int ry = std::min(p1.y(), p2.y());
294 int rr = std::max(p1.x(), p2.x()); 297 int rr = std::max(p1.x(), p2.x());
295 int rb = std::max(p1.y(), p2.y()); 298 int rb = std::max(p1.y(), p2.y());
296 return Rect(rx, ry, rr - rx, rb - ry); 299 return Rect(rx, ry, rr - rx, rb - ry);
297 } 300 }
298 301
299 } // namespace gfx 302 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698