ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

commit e4fa3f5c591c62aec29efa19fcea0b10ce577996
parent 3803adfd7eb6b85852e89ae186a1e68834ee72ca
Author: sin <sin@2f30.org>
Date:   Thu, 17 Apr 2014 16:22:58 +0100

Don't leak `buf' if realloc fails

Not an issue in ubase but someone might want to re-use this
function elsewhere.

Diffstat:
Msysctl.c | 8+++++---
1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/sysctl.c b/sysctl.c @@ -12,7 +12,7 @@ getsysctl(char *variable, char **value) { char path[PATH_MAX]; char *p; - char *buf, c; + char *buf, *tmp, c; int fd; ssize_t n; size_t sz, i; @@ -43,11 +43,13 @@ getsysctl(char *variable, char **value) break; if (i == sz - 1) { sz *= 2; - buf = realloc(buf, sz); - if (!buf) { + tmp = realloc(buf, sz); + if (!tmp) { close(fd); + free(buf); return -1; } + buf = tmp; } buf[i++] = c; }