Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 22 Oct 2008 19:55:12 +0000 (UTC)
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r184177 - head/lib/libc/gen
Message-ID:  <200810221955.m9MJtCT9037516@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kib
Date: Wed Oct 22 19:55:12 2008
New Revision: 184177
URL: http://svn.freebsd.org/changeset/base/184177

Log:
  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

Modified:
  head/lib/libc/gen/__xuname.c

Modified: head/lib/libc/gen/__xuname.c
==============================================================================
--- head/lib/libc/gen/__xuname.c	Wed Oct 22 19:39:16 2008	(r184176)
+++ head/lib/libc/gen/__xuname.c	Wed Oct 22 19:55:12 2008	(r184177)
@@ -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);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200810221955.m9MJtCT9037516>