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

Unified Diff: cc/resources/resource.h

Issue 1154393003: cc: Use CheckedNumeric for resource size calculations. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update Created 5 years, 6 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
Index: cc/resources/resource.h
diff --git a/cc/resources/resource.h b/cc/resources/resource.h
index 0e3c3df688387b9ee927f150737bc8dd37a7a6b7..ce566709a7d7bd480e9aa75978bd4390217a568f 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"
@@ -24,12 +25,23 @@ class CC_EXPORT Resource {
ResourceFormat format() const { return format_; }
size_t bytes() const;
danakj 2015/06/04 20:58:44 we should remove this function.
- inline static size_t MemorySizeBytes(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);
+ // 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) {
+ if (((BitsPerPixel(format) * size.width() * size.height()) % 8) != 0)
danakj 2015/06/04 20:58:43 can you do this only if result.IsValid() and conve
+ return false;
+ base::CheckedNumeric<size_t> result = BitsPerPixel(format);
+ result *= size.width();
+ result *= size.height();
+ result /= 8;
+ return result.IsValid();
+ }
+
+ 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:

Powered by Google App Engine
This is Rietveld 408576698