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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/geometry/rect.cc
diff --git a/ui/gfx/geometry/rect.cc b/ui/gfx/geometry/rect.cc
index 2a78d0b5c6e3696adeee8d5ffffa32b034e819f9..b3cabb9d352cc35dc11accce6ce5fa82cbebdc78 100644
--- a/ui/gfx/geometry/rect.cc
+++ b/ui/gfx/geometry/rect.cc
@@ -65,8 +65,10 @@ void Rect::Inset(const Insets& insets) {
void Rect::Inset(int left, int top, int right, int bottom) {
origin_ += Vector2d(left, top);
- set_width(std::max(width() - left - right, static_cast<int>(0)));
- set_height(std::max(height() - top - bottom, static_cast<int>(0)));
+ // left+right might overflow/underflow, but width() - (left+right) might
+ // overflow as well.
+ set_width(SafeSubtract(width(), SafeAdd(left, right)));
+ set_height(SafeSubtract(height(), SafeAdd(top, bottom)));
}
void Rect::Offset(int horizontal, int vertical) {
@@ -151,7 +153,9 @@ void Rect::Union(const Rect& rect) {
int rr = std::max(right(), rect.right());
int rb = std::max(bottom(), rect.bottom());
- SetRect(rx, ry, rr - rx, rb - ry);
+ // Subtracting to get width/height might overflow integers, so clamp them.
+ SetRect(rx, ry, GetClampedWidthFromExtents(rx, rr),
+ GetClampedWidthFromExtents(ry, rb));
}
void Rect::Subtract(const Rect& rect) {
« 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