[hackers] [ubase] Don't leak `buf' if realloc fails || sin

From: <git_AT_suckless.org>
Date: Thu, 17 Apr 2014 17:27:13 +0200

commit e4fa3f5c591c62aec29efa19fcea0b10ce577996
Author: sin <sin_AT_2f30.org>
Date: Thu Apr 17 16:22:58 2014 +0100

    Don't leak `buf' if realloc fails
    
    Not an issue in ubase but someone might want to re-use this
    function elsewhere.

diff --git a/sysctl.c b/sysctl.c
index 43ab5b1..b7f2223 100644
--- a/sysctl.c
+++ b/sysctl.c
_AT_@ -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;
_AT_@ -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;
         }
Received on Thu Apr 17 2014 - 17:27:13 CEST

This archive was generated by hypermail 2.3.0 : Thu Apr 17 2014 - 17:36:29 CEST