Skip to content

Commit 8f062d3

Browse files
Check overflow in allocation
Differential Revision: D78678540 Pull Request resolved: #12683
1 parent 29c52b5 commit 8f062d3

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

runtime/core/memory_allocator.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
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.

0 commit comments

Comments
 (0)