Skip to content

Commit

Permalink
ui/gfx: Make gfx::Size::GetArea use checked math.
Browse files Browse the repository at this point in the history
This patch ensures that if the area calculation would overflow, we
instead fail a CHECK.

R=danakj, pkasting@chromium.org
BUG=352761

Review URL: https://codereview.chromium.org/1371753004

Cr-Commit-Position: refs/heads/master@{#353594}
  • Loading branch information
vmpstr authored and Commit bot committed Oct 12, 2015
1 parent a09957f commit 35f226a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ui/gfx/geometry/size.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <ApplicationServices/ApplicationServices.h>
#endif

#include "base/numerics/safe_math.h"
#include "base/strings/stringprintf.h"
#include "ui/gfx/geometry/size_conversions.h"

Expand Down Expand Up @@ -44,7 +45,9 @@ CGSize Size::ToCGSize() const {
#endif

int Size::GetArea() const {
return width() * height();
base::CheckedNumeric<int> checked_area = width();
checked_area *= height();
return checked_area.ValueOrDie();
}

void Size::Enlarge(int grow_width, int grow_height) {
Expand Down
1 change: 1 addition & 0 deletions ui/gfx/geometry/size.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class GFX_EXPORT Size {
void set_width(int width) { width_ = width < 0 ? 0 : width; }
void set_height(int height) { height_ = height < 0 ? 0 : height; }

// This call will CHECK if the area of this size would overflow int.
int GetArea() const;

void SetSize(int width, int height) {
Expand Down

0 comments on commit 35f226a

Please sign in to comment.