[PATCH 4/4] libm/stdlib: Recover from realloc failure when shrinking

Keith Packard keithp@keithp.com
Tue Aug 11 23:05:43 GMT 2020


When shrinking and the new allocation fails, just leave things as they
were and keep going. Applications may well expect shrinking
reallocations to always succeed.

Signed-off-by: Keith Packard <keithp@keithp.com>
---
 newlib/libc/stdlib/nano-mallocr.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/newlib/libc/stdlib/nano-mallocr.c b/newlib/libc/stdlib/nano-mallocr.c
index 83234c618..452d1448a 100644
--- a/newlib/libc/stdlib/nano-mallocr.c
+++ b/newlib/libc/stdlib/nano-mallocr.c
@@ -481,7 +481,12 @@ void * nano_realloc(RARG void * ptr, malloc_size_t size)
       return ptr;
 
     mem = nano_malloc(RCALL size);
-    if (mem != NULL)
+    if (mem == NULL)
+    {
+	if (size <= old_size)
+	    return ptr;
+    }
+    else
     {
 	if (size > old_size)
 	    size = old_size;
-- 
2.28.0



More information about the Newlib mailing list