Don't leak `buf' if realloc fails
Not an issue in ubase but someone might want to re-use this function elsewhere.
This commit is contained in:
parent
3803adfd7e
commit
e4fa3f5c59
8
sysctl.c
8
sysctl.c
@ -12,7 +12,7 @@ getsysctl(char *variable, char **value)
|
|||||||
{
|
{
|
||||||
char path[PATH_MAX];
|
char path[PATH_MAX];
|
||||||
char *p;
|
char *p;
|
||||||
char *buf, c;
|
char *buf, *tmp, c;
|
||||||
int fd;
|
int fd;
|
||||||
ssize_t n;
|
ssize_t n;
|
||||||
size_t sz, i;
|
size_t sz, i;
|
||||||
@ -43,11 +43,13 @@ getsysctl(char *variable, char **value)
|
|||||||
break;
|
break;
|
||||||
if (i == sz - 1) {
|
if (i == sz - 1) {
|
||||||
sz *= 2;
|
sz *= 2;
|
||||||
buf = realloc(buf, sz);
|
tmp = realloc(buf, sz);
|
||||||
if (!buf) {
|
if (!tmp) {
|
||||||
close(fd);
|
close(fd);
|
||||||
|
free(buf);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
buf = tmp;
|
||||||
}
|
}
|
||||||
buf[i++] = c;
|
buf[i++] = c;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user