From b09445cbfe1e8105b244a9e1819c2826d1be14e9 Mon Sep 17 00:00:00 2001 From: liang yongxiang Date: Wed, 28 Mar 2018 10:17:24 +0800 Subject: [PATCH] [Kernel][memheap] fix rt_realloc bugs - missing free old memory after malloc new - free memory when new size is zero --- src/memheap.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/memheap.c b/src/memheap.c index dcd9114620..7317ebdbab 100644 --- a/src/memheap.c +++ b/src/memheap.c @@ -659,6 +659,12 @@ void *rt_realloc(void *rmem, rt_size_t newsize) if (rmem == RT_NULL) return rt_malloc(newsize); + if (newsize == 0) + { + rt_free(rmem); + return RT_NULL; + } + /* get old memory item */ header_ptr = (struct rt_memheap_item *) ((rt_uint8_t *)rmem - RT_MEMHEAP_SIZE); @@ -678,6 +684,8 @@ void *rt_realloc(void *rmem, rt_size_t newsize) rt_memcpy(new_ptr, rmem, oldsize); else rt_memcpy(new_ptr, rmem, newsize); + + rt_free(rmem); } }