From f1194ec0c51a3911510c9cbd19a18f7564224c7a Mon Sep 17 00:00:00 2001
From: kib <kib@FreeBSD.org>
Date: Wed, 22 Oct 2008 19:55:12 +0000
Subject: Remove doubtful structure definition with variable array members. I
 believe this is not a valid C99 construct.  Use directly calculated offsets
 into the supplied buffer, using specified members length, to fill appropriate
 structure.

Either use sysctl, or copy the value of the UNAME_x environment
variable, instead of unconditionally doing sysctl, and then
overriding a returned value with user-specified one.

Noted and tested by:	rdivacky
---
 lib/libc/gen/__xuname.c | 142 +++++++++++++++++++++++++-----------------------
 1 file changed, 75 insertions(+), 67 deletions(-)

(limited to 'lib')

diff --git a/lib/libc/gen/__xuname.c b/lib/libc/gen/__xuname.c
index cf8b21f..f385eb2 100644
--- a/lib/libc/gen/__xuname.c
+++ b/lib/libc/gen/__xuname.c
@@ -45,94 +45,102 @@ __xuname(int namesize, void *namebuf)
 {
 	int mib[2], rval;
 	size_t len;
-	char *p;
+	char *p, *q;
 	int oerrno;
-	struct xutsname {
-		char	sysname[namesize];	/* Name of this OS. */
-		char	nodename[namesize];	/* Name of this network node. */
-		char	release[namesize];	/* Release level. */
-		char	version[namesize];	/* Version level. */
-		char	machine[namesize];	/* Hardware type. */
-	} *name;
 
-	name = (struct xutsname *)namebuf;
 	rval = 0;
+	q = (char *)namebuf;
 
 	mib[0] = CTL_KERN;
-	mib[1] = KERN_OSTYPE;
-	len = sizeof(name->sysname);
-	oerrno = errno;
-	if (sysctl(mib, 2, &name->sysname, &len, NULL, 0) == -1) {
-		if(errno == ENOMEM)
-			errno = oerrno;
-		else
-			rval = -1;
-	}
-	name->sysname[sizeof(name->sysname) - 1] = '\0';
+
 	if ((p = getenv("UNAME_s")))
-		strlcpy(name->sysname, p, sizeof(name->sysname));
+		strlcpy(q, p, namesize);
+	else {
+		mib[1] = KERN_OSTYPE;
+		len = namesize;
+		oerrno = errno;
+		if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
+			if (errno == ENOMEM)
+				errno = oerrno;
+			else
+				rval = -1;
+		}
+		q[namesize - 1] = '\0';
+	}
+	q += namesize;
 
-	mib[0] = CTL_KERN;
 	mib[1] = KERN_HOSTNAME;
-	len = sizeof(name->nodename);
+	len = namesize;
 	oerrno = errno;
-	if (sysctl(mib, 2, &name->nodename, &len, NULL, 0) == -1) {
-		if(errno == ENOMEM)
+	if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
+		if (errno == ENOMEM)
 			errno = oerrno;
 		else
 			rval = -1;
 	}
-	name->nodename[sizeof(name->nodename) - 1] = '\0';
+	q[namesize - 1] = '\0';
+	q += namesize;
 
-	mib[0] = CTL_KERN;
-	mib[1] = KERN_OSRELEASE;
-	len = sizeof(name->release);
-	oerrno = errno;
-	if (sysctl(mib, 2, &name->release, &len, NULL, 0) == -1) {
-		if(errno == ENOMEM)
-			errno = oerrno;
-		else
-			rval = -1;
-	}
-	name->release[sizeof(name->release) - 1] = '\0';
 	if ((p = getenv("UNAME_r")))
-		strlcpy(name->release, p, sizeof(name->release));
-
-	/* The version may have newlines in it, turn them into spaces. */
-	mib[0] = CTL_KERN;
-	mib[1] = KERN_VERSION;
-	len = sizeof(name->version);
-	oerrno = errno;
-	if (sysctl(mib, 2, &name->version, &len, NULL, 0) == -1) {
-		if (errno == ENOMEM)
-			errno = oerrno;
-		else
-			rval = -1;
-	}
-	name->version[sizeof(name->version) - 1] = '\0';
-	for (p = name->version; len--; ++p) {
-		if (*p == '\n' || *p == '\t') {
-			if (len > 1)
-				*p = ' ';
+		strlcpy(q, p, namesize);
+	else {
+		mib[1] = KERN_OSRELEASE;
+		len = namesize;
+		oerrno = errno;
+		if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
+			if (errno == ENOMEM)
+				errno = oerrno;
 			else
-				*p = '\0';
+				rval = -1;
 		}
+		q[namesize - 1] = '\0';
 	}
+	q += namesize;
+
 	if ((p = getenv("UNAME_v")))
-		strlcpy(name->version, p, sizeof(name->version));
+		strlcpy(q, p, namesize);
+	else {
 
-	mib[0] = CTL_HW;
-	mib[1] = HW_MACHINE;
-	len = sizeof(name->machine);
-	oerrno = errno;
-	if (sysctl(mib, 2, &name->machine, &len, NULL, 0) == -1) {
-		if (errno == ENOMEM)
-			errno = oerrno;
-		else
-			rval = -1;
+		/*
+		 * The version may have newlines in it, turn them into
+		 * spaces.
+		 */
+		mib[1] = KERN_VERSION;
+		len = namesize;
+		oerrno = errno;
+		if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
+			if (errno == ENOMEM)
+				errno = oerrno;
+			else
+				rval = -1;
+		}
+		q[namesize - 1] = '\0';
+		for (p = q; len--; ++p) {
+			if (*p == '\n' || *p == '\t') {
+				if (len > 1)
+					*p = ' ';
+				else
+					*p = '\0';
+			}
+		}
 	}
-	name->machine[sizeof(name->machine) - 1] = '\0';
+	q += namesize;
+
 	if ((p = getenv("UNAME_m")))
-		strlcpy(name->machine, p, sizeof(name->machine));
+		strlcpy(q, p, namesize);
+	else {
+		mib[0] = CTL_HW;
+		mib[1] = HW_MACHINE;
+		len = namesize;
+		oerrno = errno;
+		if (sysctl(mib, 2, &q, &len, NULL, 0) == -1) {
+			if (errno == ENOMEM)
+				errno = oerrno;
+			else
+				rval = -1;
+		}
+		q[namesize - 1] = '\0';
+	}
+
 	return (rval);
 }
-- 
cgit v1.1