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

Message ID 20200811230543.2169774-5-keithp@keithp.com
State Superseded
Headers show
Series
  • Fixes for memory allocation bugs
Related show

Commit Message

Eshan dhawan via Newlib Aug. 11, 2020, 11:05 p.m.
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(-)

-- 
2.28.0

Patch

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;