File tree Expand file tree Collapse file tree 1 file changed +12
-1
lines changed
Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change 1212#include < cinttypes>
1313#include < cstdint>
1414
15+ #include < c10/util/safe_numerics.h>
16+
1517#include < executorch/runtime/core/error.h>
1618#include < executorch/runtime/platform/assert.h>
1719#include < executorch/runtime/platform/compiler.h>
@@ -137,7 +139,16 @@ class MemoryAllocator {
137139 // Some users of this method allocate lists of pointers, causing the next
138140 // line to expand to `sizeof(type *)`, which triggers a clang-tidy warning.
139141 // NOLINTNEXTLINE(bugprone-sizeof-expression)
140- return static_cast <T*>(this ->allocate (size * sizeof (T), alignment));
142+ size_t bytes_size = 0 ;
143+ bool overflow = c10::mul_overflows (size, sizeof (T), &bytes_size);
144+ if (overflow) {
145+ ET_LOG (
146+ Error,
147+ " Failed to allocate list of type %zu: size * sizeof(T) overflowed" ,
148+ size);
149+ return nullptr ;
150+ }
151+ return static_cast <T*>(this ->allocate (bytes_size, alignment));
141152 }
142153
143154 // Returns the allocator memory's base address.
You can’t perform that action at this time.
0 commit comments