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

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

Issue 1371753004: ui/gfx: Make gfx::Size::GetArea use checked math. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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/size.h ('k') | no next file » | 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/size.h" 5 #include "ui/gfx/geometry/size.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #elif defined(OS_IOS) 9 #elif defined(OS_IOS)
10 #include <CoreGraphics/CoreGraphics.h> 10 #include <CoreGraphics/CoreGraphics.h>
11 #elif defined(OS_MACOSX) 11 #elif defined(OS_MACOSX)
12 #include <ApplicationServices/ApplicationServices.h> 12 #include <ApplicationServices/ApplicationServices.h>
13 #endif 13 #endif
14 14
15 #include "base/numerics/safe_math.h"
15 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
16 #include "ui/gfx/geometry/size_conversions.h" 17 #include "ui/gfx/geometry/size_conversions.h"
17 18
18 namespace gfx { 19 namespace gfx {
19 20
20 #if defined(OS_MACOSX) 21 #if defined(OS_MACOSX)
21 Size::Size(const CGSize& s) 22 Size::Size(const CGSize& s)
22 : width_(s.width < 0 ? 0 : s.width), 23 : width_(s.width < 0 ? 0 : s.width),
23 height_(s.height < 0 ? 0 : s.height) { 24 height_(s.height < 0 ? 0 : s.height) {
24 } 25 }
(...skipping 12 matching lines...) Expand all
37 s.cy = height(); 38 s.cy = height();
38 return s; 39 return s;
39 } 40 }
40 #elif defined(OS_MACOSX) 41 #elif defined(OS_MACOSX)
41 CGSize Size::ToCGSize() const { 42 CGSize Size::ToCGSize() const {
42 return CGSizeMake(width(), height()); 43 return CGSizeMake(width(), height());
43 } 44 }
44 #endif 45 #endif
45 46
46 int Size::GetArea() const { 47 int Size::GetArea() const {
47 return width() * height(); 48 base::CheckedNumeric<int> checked_area = width();
49 checked_area *= height();
50 return checked_area.ValueOrDie();
48 } 51 }
49 52
50 void Size::Enlarge(int grow_width, int grow_height) { 53 void Size::Enlarge(int grow_width, int grow_height) {
51 SetSize(width() + grow_width, height() + grow_height); 54 SetSize(width() + grow_width, height() + grow_height);
52 } 55 }
53 56
54 void Size::SetToMin(const Size& other) { 57 void Size::SetToMin(const Size& other) {
55 width_ = width() <= other.width() ? width() : other.width(); 58 width_ = width() <= other.width() ? width() : other.width();
56 height_ = height() <= other.height() ? height() : other.height(); 59 height_ = height() <= other.height() ? height() : other.height();
57 } 60 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 return ToRoundedSize(ScaleSize(gfx::SizeF(size), x_scale, y_scale)); 98 return ToRoundedSize(ScaleSize(gfx::SizeF(size), x_scale, y_scale));
96 } 99 }
97 100
98 Size ScaleToRoundedSize(const Size& size, float scale) { 101 Size ScaleToRoundedSize(const Size& size, float scale) {
99 if (scale == 1.f) 102 if (scale == 1.f)
100 return size; 103 return size;
101 return ToRoundedSize(ScaleSize(gfx::SizeF(size), scale, scale)); 104 return ToRoundedSize(ScaleSize(gfx::SizeF(size), scale, scale));
102 } 105 }
103 106
104 } // namespace gfx 107 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/geometry/size.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698