[hackers] [ubase] Don't hardcode the buffer size in sysctl || sin
commit 3803adfd7eb6b85852e89ae186a1e68834ee72ca
Author: sin <sin_AT_2f30.org>
Date: Thu Apr 17 16:13:34 2014 +0100
Don't hardcode the buffer size in sysctl
diff --git a/sysctl.c b/sysctl.c
index c6e42ce..43ab5b1 100644
--- a/sysctl.c
+++ b/sysctl.c
_AT_@ -12,9 +12,10 @@ getsysctl(char *variable, char **value)
{
char path[PATH_MAX];
char *p;
- char *buf;
+ char *buf, c;
int fd;
ssize_t n;
+ size_t sz, i;
for (p = variable; *p; p++)
if (*p == '.')
_AT_@ -28,19 +29,29 @@ getsysctl(char *variable, char **value)
if (fd < 0)
return -1;
- buf = malloc(1024);
- if (!buf) {
- close(fd);
- return -1;
- }
-
- n = read(fd, buf, 1023);
- if (n <= 0) {
- close(fd);
- free(buf);
- return -1;
+ i = 0;
+ sz = 1;
+ buf = NULL;
+ while (1) {
+ n = read(fd, &c, 1);
+ if (n < 0) {
+ close(fd);
+ free(buf);
+ return -1;
+ }
+ if (n == 0)
+ break;
+ if (i == sz - 1) {
+ sz *= 2;
+ buf = realloc(buf, sz);
+ if (!buf) {
+ close(fd);
+ return -1;
+ }
+ }
+ buf[i++] = c;
}
- buf[n] = '
Received on Thu Apr 17 2014 - 17:22:49 CEST
This archive was generated by hypermail 2.3.0
: Thu Apr 17 2014 - 17:24:20 CEST