[hackers] [ubase][PATCH] sysctl: fix non-supported byte-by-byte partial reads

From: runitclean <runitclean_AT_disroot.org>
Date: Thu, 30 Jul 2026 16:44:25 +0100

Before this patch, trying to query a value using ubase's sysctl returned trimmed one-byte-lengthed result, apparently, `read(fd, buf, 1)` was successfully fetching the first byte, but when trying to read the remaining string byte-by-byte, it failed since custom entry handler ignores `ppos`, to fix this, read the full payload in one call.

---
 sysctl.c | 40 +++++++++++-----------------------------
 1 file changed, 11 insertions(+), 29 deletions(-)
diff --git a/sysctl.c b/sysctl.c
index a98a637..ff9323a 100644
--- a/sysctl.c
+++ b/sysctl.c
_AT_@ -21,11 +21,10 @@ static int
 getsysctl(char *variable, char **value)
 {
 	char path[PATH_MAX];
+	char buf[BUFSIZ];
 	char *p;
-	char *buf, *tmp, c;
 	int fd;
 	ssize_t n;
-	size_t sz, i;
 
 	replacestr(variable, '.', '/');
 
_AT_@ -41,39 +40,22 @@ getsysctl(char *variable, char **value)
 	if (fd < 0)
 		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;
-			tmp = realloc(buf, sz);
-			if (!tmp) {
-				close(fd);
-				free(buf);
-				return -1;
-			}
-			buf = tmp;
-		}
-		buf[i++] = c;
+	n = read(fd, buf, sizeof(buf) - 1);
+	close(fd);
+
+	if (n < 0) {
+		return -1;
 	}
-	buf[i] = '\0';
+	buf[n] = '\0';
 
 	p = strrchr(buf, '\n');
 	if (p)
 		*p = '\0';
 
-	*value = buf;
-
-	close(fd);
+	*value = estrdup(buf);
+	if (!*value) {
+		return -1;
+	}
 
 	return 0;
 }
-- 
2.53.0
Received on Thu Jul 30 2026 - 17:44:25 CEST

This archive was generated by hypermail 2.3.0 : Thu Jul 30 2026 - 18:00:40 CEST