Index: cc/resources/resource.h |
diff --git a/cc/resources/resource.h b/cc/resources/resource.h |
index 0e3c3df688387b9ee927f150737bc8dd37a7a6b7..f9138fbb876ef405c8afada390cf4031f92f4b92 100644 |
--- a/cc/resources/resource.h |
+++ b/cc/resources/resource.h |
@@ -5,6 +5,7 @@ |
#ifndef CC_RESOURCES_RESOURCE_H_ |
#define CC_RESOURCES_RESOURCE_H_ |
+#include "base/numerics/safe_math.h" |
#include "cc/base/cc_export.h" |
#include "cc/resources/resource_provider.h" |
#include "ui/gfx/geometry/size.h" |
@@ -13,7 +14,7 @@ namespace cc { |
class CC_EXPORT Resource { |
public: |
- Resource() : id_(0) {} |
+ Resource() : id_(0), format_(RGBA_8888) {} |
Resource(unsigned id, const gfx::Size& size, ResourceFormat format) |
: id_(id), |
size_(size), |
@@ -22,14 +23,36 @@ class CC_EXPORT Resource { |
ResourceId id() const { return id_; } |
gfx::Size size() const { return size_; } |
ResourceFormat format() const { return format_; } |
- size_t bytes() const; |
- inline static size_t MemorySizeBytes(const gfx::Size& size, |
+ // Return true if the call to UncheckedMemorySizeBytes would return a value |
+ // that fits in a size_t. |
+ static bool VerifySizeInBytes(const gfx::Size& size, ResourceFormat format) { |
+ base::CheckedNumeric<size_t> checked_value = BitsPerPixel(format); |
+ checked_value *= size.width(); |
+ checked_value *= size.height(); |
+ if (!checked_value.IsValid()) |
+ return false; |
+ size_t value = checked_value.ValueOrDie(); |
+ if ((value % 8) != 0) |
+ return false; |
+ return true; |
+ } |
+ |
+ static size_t CheckedMemorySizeBytes(const gfx::Size& size, |
ResourceFormat format) { |
- DCHECK_EQ(0, (BitsPerPixel(format) * size.width() * size.height()) % 8); |
- // TODO(vmpstr): Make this function overflow safe. crbug.com/495867 |
- return static_cast<size_t>( |
- (BitsPerPixel(format) * size.width() * size.height()) / 8); |
+ DCHECK(VerifySizeInBytes(size, format)); |
+ base::CheckedNumeric<size_t> checked_value = BitsPerPixel(format); |
+ checked_value *= size.width(); |
+ checked_value *= size.height(); |
+ checked_value /= 8; |
+ return checked_value.ValueOrDie(); |
+ } |
+ |
+ inline static size_t UncheckedMemorySizeBytes(const gfx::Size& size, |
+ ResourceFormat format) { |
+ DCHECK(VerifySizeInBytes(size, format)); |
+ return static_cast<size_t>(BitsPerPixel(format)) * size.width() * |
+ size.height() / 8; |
} |
protected: |