From e3fe16a22ce1ef78827b85217d3baa023c3a9b90 Mon Sep 17 00:00:00 2001 From: Adrian Grigore Date: Tue, 9 Jul 2024 08:09:16 +0300 Subject: [PATCH] libc/vsmprint.c: don't use pointer after realloc --- libc/vsmprint.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/libc/vsmprint.c b/libc/vsmprint.c index df5abe4..27d08ac 100644 --- a/libc/vsmprint.c +++ b/libc/vsmprint.c @@ -6,23 +6,24 @@ static int fmtStrFlush(Fmt *f) { char *s; - int n; + int n, d; if(f->start == nil) return 0; n = (uintptr)f->farg; n *= 2; - s = (char*)f->start; - f->start = realloc(s, n); - if(f->start == nil){ + d = (char*)f->to - (char*)f->start; + s = realloc(f->start, n); + if(s == nil){ f->farg = nil; f->to = nil; f->stop = nil; - free(s); + free(f->start); return 0; } + f->start = s; f->farg = (void*)(uintptr)n; - f->to = (char*)f->start + ((char*)f->to - s); + f->to = (char*)f->start + d; f->stop = (char*)f->start + n - 1; return 1; }