Skip to content

Commit

Permalink
kmm_realloc: fix crash issue case of oldmem is NULL
Browse files Browse the repository at this point in the history
if oldmem argument is NULL, we should call the malloc(). It is posix rule.
But now, if oldmem is NULL, mm_get_heap returns NULL. So we get crash.

So, Add checking NULL.
And Change to so that, if oldmem is out of heap address, we can get a assertion.

Signed-off-by: eunwoo.nam <[email protected]>
  • Loading branch information
ewoodev authored and sunghan-chang committed Jul 15, 2024
1 parent 00fbfac commit 2e284ac
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions os/mm/kmm_heap/kmm_realloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,30 @@ FAR void *kmm_realloc(FAR void *oldmem, size_t newsize)
mmaddress_t caller_retaddr = 0;
ARCH_GET_RET_ADDRESS(caller_retaddr)
#endif
struct mm_heap_s *kheap_origin = mm_get_heap(oldmem);
struct mm_heap_s *kheap_origin;
struct mm_heap_s *kheap_new;

if (newsize == 0) {
mm_free(kheap_origin, oldmem);
return NULL;
}
if (oldmem) {
kheap_origin = mm_get_heap(oldmem);

/* The oldmem given by first argument is not a dynamically
* allocated address. This will cause ASSERT like Linux.
*/
ASSERT(kheap_origin);

if (newsize == 0) {
mm_free(kheap_origin, oldmem);
return NULL;
}

#ifdef CONFIG_DEBUG_MM_HEAPINFO
ret = mm_realloc(kheap_origin, oldmem, newsize, caller_retaddr);
ret = mm_realloc(kheap_origin, oldmem, newsize, caller_retaddr);
#else
ret = mm_realloc(kheap_origin, oldmem, newsize);
ret = mm_realloc(kheap_origin, oldmem, newsize);
#endif
if (ret != NULL) {
return ret;
if (ret != NULL) {
return ret;
}
}

/* Try to mm_malloc to another heap. */
Expand Down

0 comments on commit 2e284ac

Please sign in to comment.