commit be15c6c152e56359ee52e0bc507d0328f8c0e981
parent 2996bee1a65156f2714982a5f3d588aaaed0edc4
Author: Eyal Sawady <ecs@d2evs.net>
Date: Wed, 3 Feb 2021 12:25:36 -0500
Fix rt::realloc
We can return the original pointer if the allocated size is greater than
the requested size, not if it's less than the requested size.
In addition, free the original pointer if the allocation succeeds.
Diffstat:
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/rt/malloc.ha b/rt/malloc.ha
@@ -148,7 +148,7 @@ export fn realloc(_p: nullable *void, n: size) nullable *void = {
let p = _p: *void;
let bsize = (p: uintptr - size(size): uintptr): *size;
let s = *bsize;
- if (s < n) {
+ if (s > n) {
return p;
};
@@ -159,6 +159,7 @@ export fn realloc(_p: nullable *void, n: size) nullable *void = {
let new = malloc(n);
if (new != null: nullable *void) {
memcpy(new: *void, p, s);
+ free(p);
};
return new;