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

Message ID 20200820221449.686006-2-keithp@keithp.com
State New
Headers show
Series
  • Fixes inspired by building with clang
Related show

Commit Message

R. Diez via Newlib Aug. 20, 2020, 10:14 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 6ba0eb74b..0b032d833 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 (old_size > size)
 	    old_size = size;