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

Unified Diff: core/include/fxcrt/fx_memory.h

Issue 1143663004: Add safe FX_Alloc2D() macro (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Drop one file, indent. Created 5 years, 7 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 | « core/include/fxcrt/fx_basic.h ('k') | core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: core/include/fxcrt/fx_memory.h
diff --git a/core/include/fxcrt/fx_memory.h b/core/include/fxcrt/fx_memory.h
index afce91169a9110cfc94e01677ee5476a3dad2580..a75ff2581870f8a08ea344a1edcf1b20aa61452d 100644
--- a/core/include/fxcrt/fx_memory.h
+++ b/core/include/fxcrt/fx_memory.h
@@ -41,6 +41,14 @@ inline void* FX_AllocOrDie(size_t num_members, size_t member_size) {
return nullptr; // Suppress compiler warning.
}
+inline void* FX_AllocOrDie2D(size_t w, size_t h, size_t member_size) {
+ if (w < std::numeric_limits<size_t>::max() / h) {
+ return FX_AllocOrDie(w * h, member_size);
+ }
+ FX_OutOfMemoryTerminate(); // Never returns.
+ return nullptr; // Suppress compiler warning.
+}
+
inline void* FX_ReallocOrDie(void* ptr, size_t num_members, size_t member_size) {
if (void* result = FX_SafeRealloc(ptr, num_members, member_size)) {
return result;
@@ -51,6 +59,7 @@ inline void* FX_ReallocOrDie(void* ptr, size_t num_members, size_t member_size)
// Never returns NULL.
#define FX_Alloc(type, size) (type*)FX_AllocOrDie(size, sizeof(type))
+#define FX_Alloc2D(type, w, h) (type*)FX_AllocOrDie2D(w, h, sizeof(type))
#define FX_Realloc(type, ptr, size) \
(type*)FX_ReallocOrDie(ptr, size, sizeof(type))
« no previous file with comments | « core/include/fxcrt/fx_basic.h ('k') | core/src/fpdfapi/fpdf_edit/fpdf_edit_image.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698