shithub: scc

Download patch

ref: 2fc84f35db1880e0557e1e5f1f008370ee7ee545
parent: 815c89528181877874213fded0f31a83ba1123db
author: Mikhail Konovalov <m.konovalov@trustlab.center>
date: Sun Oct 3 05:30:00 EDT 2021

libc: Fix realloc() +1 error

There were several cases in realloc() were it was returning
a pointer to the Header structure instead of returning the
pointer to the actual allocated buffer.

--- a/src/libc/stdlib/realloc.c
+++ b/src/libc/stdlib/realloc.c
@@ -10,7 +10,7 @@
 	Header *oh, *prev, *next, *new;
 	size_t nunits, avail, onbytes, n;
 
-	if (!nbytes)
+	if (nbytes == 0)
 		return NULL;
 
 	if (!ptr)
@@ -17,7 +17,7 @@
 		return malloc(nbytes);
 
 	nunits = (nbytes + sizeof(Header) - 1) / sizeof(Header) + 1;
-	oh = (Header*)ptr - 1;
+	oh = (Header*) ptr - 1;
 
 	if (oh->h.size == nunits)
 		return ptr;
@@ -28,7 +28,7 @@
 		new->h.size = oh->h.size - nunits;
 		oh->h.size = nunits;
 		free(new + 1);
-		return oh;
+		return oh + 1;
 	}
 
 	prev = _prevchunk(oh);
@@ -44,7 +44,7 @@
 		if (avail == nunits) {
 			oh->h.size = nunits;
 			prev->h.next = next->h.next;
-			return oh;
+			return oh + 1;
 		}
 
 		if (avail > nunits) {
@@ -52,7 +52,7 @@
 			prev->h.next = new;
 			new->h.next = next;
 			new->h.size = avail - nunits;
-			return oh;
+			return oh + 1;
 		}
 	}