summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKeith Packard via Newlib <newlib@sourceware.org>2020-08-13 17:19:01 -0700
committerCorinna Vinschen <corinna@vinschen.de>2020-08-17 11:43:55 +0200
commitce4044adeebfdc60714d3a35f67ba536edb55612 (patch)
treeb0234cb8b655503266b585344586235125b977a9
parent70d02aaca6ca4aa8990c673b2b0d2220ae813ee4 (diff)
downloadcygnal-ce4044adeebfdc60714d3a35f67ba536edb55612.tar.gz
cygnal-ce4044adeebfdc60714d3a35f67ba536edb55612.tar.bz2
cygnal-ce4044adeebfdc60714d3a35f67ba536edb55612.zip
libm/stdlib: don't read past source in nano_realloc
Save the computed block size and use it to avoid reading past the end of the source block. Signed-off-by: Keith Packard <keithp@keithp.com>
-rw-r--r--newlib/libc/stdlib/nano-mallocr.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/newlib/libc/stdlib/nano-mallocr.c b/newlib/libc/stdlib/nano-mallocr.c
index 9c59cff73..3970753a9 100644
--- a/newlib/libc/stdlib/nano-mallocr.c
+++ b/newlib/libc/stdlib/nano-mallocr.c
@@ -466,6 +466,7 @@ void * nano_realloc(RARG void * ptr, malloc_size_t size)
{
void * mem;
chunk * p_to_realloc;
+ malloc_size_t old_size;
if (ptr == NULL) return nano_malloc(RCALL size);
@@ -477,13 +478,14 @@ void * nano_realloc(RARG void * ptr, malloc_size_t size)
/* TODO: There is chance to shrink the chunk if newly requested
* size is much small */
- if (nano_malloc_usable_size(RCALL ptr) >= size)
+ old_size = nano_malloc_usable_size(RCALL ptr);
+ if (old_size >= size)
return ptr;
mem = nano_malloc(RCALL size);
if (mem != NULL)
{
- memcpy(mem, ptr, size);
+ memcpy(mem, ptr, old_size);
nano_free(RCALL ptr);
}
return mem;