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

Unified Diff: third_party/WebKit/Source/platform/geometry/IntRect.cpp

Issue 2685783008: Fix integer-overflow in blink::IntRect::uniteEvenIfEmpty (Closed)
Patch Set: Created 3 years, 10 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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/platform/geometry/IntRect.cpp
diff --git a/third_party/WebKit/Source/platform/geometry/IntRect.cpp b/third_party/WebKit/Source/platform/geometry/IntRect.cpp
index 5cd63b58f16a83c0023641a22b879440e84a6cdc..07ae36c46e5eb57fd41a4870de9b8f60da89d9a5 100644
--- a/third_party/WebKit/Source/platform/geometry/IntRect.cpp
+++ b/third_party/WebKit/Source/platform/geometry/IntRect.cpp
@@ -43,6 +43,13 @@ IntRect::IntRect(const LayoutRect& r)
: m_location(r.x().toInt(), r.y().toInt()),
m_size(r.width().toInt(), r.height().toInt()) {}
+// Use long instead of int to avoid undefined behavior of storing -2147483648
+static inline int GetClampedDiff(const long& a, const long& b) {
+ return a - b <= std::numeric_limits<int>::max()
+ ? a - b
+ : std::numeric_limits<int>::max();
+}
+
bool IntRect::intersects(const IntRect& other) const {
// Checking emptiness handles negative widths as well as zero.
return !isEmpty() && !other.isEmpty() && x() < other.maxX() &&
@@ -70,8 +77,8 @@ void IntRect::intersect(const IntRect& other) {
m_location.setX(left);
m_location.setY(top);
- m_size.setWidth(right - left);
- m_size.setHeight(bottom - top);
+ m_size.setWidth(GetClampedDiff(right, left));
+ m_size.setHeight(GetClampedDiff(bottom, top));
}
void IntRect::unite(const IntRect& other) {
@@ -106,8 +113,8 @@ void IntRect::uniteEvenIfEmpty(const IntRect& other) {
m_location.setX(left);
m_location.setY(top);
- m_size.setWidth(right - left);
- m_size.setHeight(bottom - top);
+ m_size.setWidth(GetClampedDiff(right, left));
+ m_size.setHeight(GetClampedDiff(bottom, top));
}
void IntRect::scale(float s) {
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698