* [PATCH 0/2] Realloc fixes version 2
@ 2020-08-14 0:19 Keith Packard
2020-08-14 0:19 ` [PATCH 1/2] libm/stdlib: don't read past source in nano_realloc Keith Packard
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Keith Packard @ 2020-08-14 0:19 UTC (permalink / raw)
To: newlib
Here's an updated pair which fixes a bug where realloc was reading off
the source, and then addresses a TODO item to shrink the buffer when
the new size is much smaller.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] libm/stdlib: don't read past source in nano_realloc
2020-08-14 0:19 [PATCH 0/2] Realloc fixes version 2 Keith Packard
@ 2020-08-14 0:19 ` Keith Packard
2020-08-14 0:19 ` [PATCH 2/2] libm/stdlib: Realloc when shrinking by 2* or more Keith Packard
2020-08-17 9:45 ` [PATCH 0/2] Realloc fixes version 2 Corinna Vinschen
2 siblings, 0 replies; 4+ messages in thread
From: Keith Packard @ 2020-08-14 0:19 UTC (permalink / raw)
To: newlib
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>
---
newlib/libc/stdlib/nano-mallocr.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/newlib/libc/stdlib/nano-mallocr.c b/newlib/libc/stdlib/nano-mallocr.c
index 04465eb9e..5028431ec 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;
--
2.28.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 2/2] libm/stdlib: Realloc when shrinking by 2* or more
2020-08-14 0:19 [PATCH 0/2] Realloc fixes version 2 Keith Packard
2020-08-14 0:19 ` [PATCH 1/2] libm/stdlib: don't read past source in nano_realloc Keith Packard
@ 2020-08-14 0:19 ` Keith Packard
2020-08-17 9:45 ` [PATCH 0/2] Realloc fixes version 2 Corinna Vinschen
2 siblings, 0 replies; 4+ messages in thread
From: Keith Packard @ 2020-08-14 0:19 UTC (permalink / raw)
To: newlib
This reduces memory usage when reallocating objects much smaller.
Signed-off-by: Keith Packard <keithp@keithp.com>
---
newlib/libc/stdlib/nano-mallocr.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/newlib/libc/stdlib/nano-mallocr.c b/newlib/libc/stdlib/nano-mallocr.c
index 5028431ec..d178c34ab 100644
--- a/newlib/libc/stdlib/nano-mallocr.c
+++ b/newlib/libc/stdlib/nano-mallocr.c
@@ -476,15 +476,15 @@ void * nano_realloc(RARG void * ptr, malloc_size_t size)
return NULL;
}
- /* TODO: There is chance to shrink the chunk if newly requested
- * size is much small */
old_size = nano_malloc_usable_size(RCALL ptr);
- if (old_size >= size)
+ if (size <= old_size && (old_size >> 1) < size)
return ptr;
mem = nano_malloc(RCALL size);
if (mem != NULL)
{
+ if (old_size > size)
+ old_size = size;
memcpy(mem, ptr, old_size);
nano_free(RCALL ptr);
}
--
2.28.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 0/2] Realloc fixes version 2
2020-08-14 0:19 [PATCH 0/2] Realloc fixes version 2 Keith Packard
2020-08-14 0:19 ` [PATCH 1/2] libm/stdlib: don't read past source in nano_realloc Keith Packard
2020-08-14 0:19 ` [PATCH 2/2] libm/stdlib: Realloc when shrinking by 2* or more Keith Packard
@ 2020-08-17 9:45 ` Corinna Vinschen
2 siblings, 0 replies; 4+ messages in thread
From: Corinna Vinschen @ 2020-08-17 9:45 UTC (permalink / raw)
To: Keith Packard; +Cc: newlib
On Aug 13 17:19, Keith Packard via Newlib wrote:
> Here's an updated pair which fixes a bug where realloc was reading off
> the source, and then addresses a TODO item to shrink the buffer when
> the new size is much smaller.
>
Pushed.
Thanks,
Corinna
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2020-08-17 9:45 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-14 0:19 [PATCH 0/2] Realloc fixes version 2 Keith Packard
2020-08-14 0:19 ` [PATCH 1/2] libm/stdlib: don't read past source in nano_realloc Keith Packard
2020-08-14 0:19 ` [PATCH 2/2] libm/stdlib: Realloc when shrinking by 2* or more Keith Packard
2020-08-17 9:45 ` [PATCH 0/2] Realloc fixes version 2 Corinna Vinschen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).