scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit b643a0e6a56ac5af4e3f69106181ef0421f256ec
parent 52c9f69d5955ab73cd5bfa317d9a21edcf2299aa
Author: Mikhail Konovalov <m.konovalov@trustlab.center>
Date:   Sun,  3 Oct 2021 09:30:00 +0200

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.

Diffstat:
Msrc/libc/stdlib/realloc.c | 10+++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/libc/stdlib/realloc.c b/src/libc/stdlib/realloc.c @@ -10,14 +10,14 @@ realloc(void *ptr, size_t nbytes) Header *oh, *prev, *next, *new; size_t nunits, avail, onbytes, n; - if (!nbytes) + if (nbytes == 0) return NULL; if (!ptr) 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 @@ realloc(void *ptr, size_t nbytes) 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 @@ realloc(void *ptr, size_t nbytes) 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 @@ realloc(void *ptr, size_t nbytes) prev->h.next = new; new->h.next = next; new->h.size = avail - nunits; - return oh; + return oh + 1; } }