Skip to content

Commit c572f9e

Browse files
sxufacebook-github-bot
authored andcommitted
Check for 0 size allocation before erroring out in ET_TRY_ALLOCATE_* (#4097)
Summary: Pull Request resolved: #4097 malloc can return nullptr when the requested size is 0. When this is used with extension/memory_allocator/malloc_memory_allocator.h this line can fail: https://github.com/pytorch/executorch/blob/8f12da1a34008c645d294843b13142959c6dab97/runtime/executor/method.cpp#L144 Reviewed By: mohankumarkumar, cccclai Differential Revision: D59232805 fbshipit-source-id: d1fa025d8a3b99e7ce2063ca740e45980b82e23f
1 parent 8f12da1 commit c572f9e

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

runtime/core/memory_allocator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class MemoryAllocator {
217217
#define ET_TRY_ALLOCATE_OR(memory_allocator__, nbytes__, ...) \
218218
({ \
219219
void* et_try_allocate_result = memory_allocator__->allocate(nbytes__); \
220-
if (et_try_allocate_result == nullptr) { \
220+
if (et_try_allocate_result == nullptr && nbytes__ > 0) { \
221221
__VA_ARGS__ \
222222
/* The args must return. */ \
223223
__ET_UNREACHABLE(); \
@@ -272,7 +272,7 @@ class MemoryAllocator {
272272
({ \
273273
type__* et_try_allocate_result = \
274274
memory_allocator__->allocateList<type__>(nelem__); \
275-
if (et_try_allocate_result == nullptr) { \
275+
if (et_try_allocate_result == nullptr && nelem__ > 0) { \
276276
__VA_ARGS__ \
277277
/* The args must return. */ \
278278
__ET_UNREACHABLE(); \

0 commit comments

Comments
 (0)