Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions runtime/executor/platform_memory_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <cinttypes>
#include <cstdint>

#include <c10/util/safe_numerics.h>
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<c10/util/safe_numerics.h> is already included by executorch/runtime/core/memory_allocator.h, so this direct include is redundant here. Consider removing it to keep header dependencies minimal and reduce rebuild churn.

Suggested change
#include <c10/util/safe_numerics.h>

Copilot uses AI. Check for mistakes.
#include <executorch/runtime/core/memory_allocator.h>
#include <executorch/runtime/platform/log.h>
#include <executorch/runtime/platform/platform.h>
Expand Down Expand Up @@ -46,8 +47,20 @@ class PlatformMemoryAllocator final : public MemoryAllocator {
return nullptr;
}

// Allocate enough memory for the node, the data and the alignment bump.
size_t alloc_size = sizeof(AllocationNode) + size + alignment;
// Check for overflow before computing total allocation size.
// Allocate enough for the node, data, and alignment bump (at most
// alignment - 1 extra bytes to align the data pointer).
size_t alloc_size = 0;
if (c10::add_overflows(sizeof(AllocationNode), size, &alloc_size) ||
c10::add_overflows(alloc_size, alignment - 1, &alloc_size)) {
ET_LOG(
Error,
"Allocation size overflow: size %zu, alignment %zu",
size,
alignment);
return nullptr;
}

void* node_memory = runtime::pal_allocate(alloc_size);

// If allocation failed, log message and return nullptr.
Expand Down
Loading