Subject: lib/35403: Error code path optimization in libc's implementation of uname()
To: None <lib-bug-people@netbsd.org, gnats-admin@netbsd.org,>
From: Pierre Pronchery <khorben@defora.org>
List: netbsd-bugs
Date: 01/11/2007 01:50:00
>Number:         35403
>Category:       lib
>Synopsis:       Function uname() in libc can be slightly more efficient
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    lib-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Thu Jan 11 01:50:00 +0000 2007
>Originator:     Pierre Pronchery <khorben@defora.org>
>Release:        NetBSD 4.0_BETA2 (uname.c 1.9)
>Organization:
>Environment:
System: NetBSD syn 4.0_BETA2 NetBSD 4.0_BETA2 (GENERIC.MPACPI)
Architecture: i386
Machine: i386
>Description:
For what it matters, the rval variable in this function is not
necessary. It would be even more efficient to directly return in case of
errors in this function, since it can make up to four useless other
calls to sysctl() in this situation.
>How-To-Repeat:
In regular conditions this should never be triggered.
>Fix:
Apply attached patch, not tested though trivial.

--------------080106030601040508090805
Content-Type: text/plain;
 name="patch-libc-uname.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="patch-libc-uname.diff"

Index: uname.c
===================================================================
RCS file: /cvsroot/src/lib/libc/gen/uname.c,v
retrieving revision 1.9
diff -u -r1.9 uname.c
--- uname.c	7 Aug 2003 16:42:59 -0000	1.9
+++ uname.c	11 Jan 2007 01:34:05 -0000
@@ -54,7 +54,7 @@
 uname(name)
 	struct utsname *name;
 {
-	int mib[2], rval;
+	int mib[2];
 	size_t len;
 	char *p;
 
@@ -66,26 +66,26 @@
 	mib[1] = KERN_OSTYPE;
 	len = sizeof(name->sysname);
 	if (sysctl(mib, 2, &name->sysname, &len, NULL, 0) == -1)
-		rval = -1;
+		return -1;
 
 	mib[0] = CTL_KERN;
 	mib[1] = KERN_HOSTNAME;
 	len = sizeof(name->nodename);
 	if (sysctl(mib, 2, &name->nodename, &len, NULL, 0) == -1)
-		rval = -1;
+		return -1;
 
 	mib[0] = CTL_KERN;
 	mib[1] = KERN_OSRELEASE;
 	len = sizeof(name->release);
 	if (sysctl(mib, 2, &name->release, &len, NULL, 0) == -1)
-		rval = -1;
+		return -1;
 
 	/* The version may have newlines in it, turn them into spaces. */
 	mib[0] = CTL_KERN;
 	mib[1] = KERN_VERSION;
 	len = sizeof(name->version);
 	if (sysctl(mib, 2, &name->version, &len, NULL, 0) == -1)
-		rval = -1;
+		return -1;
 	else
 		for (p = name->version; len--; ++p) {
 			if (*p == '\n' || *p == '\t') {
@@ -100,6 +100,6 @@
 	mib[1] = HW_MACHINE;
 	len = sizeof(name->machine);
 	if (sysctl(mib, 2, &name->machine, &len, NULL, 0) == -1)
-		rval = -1;
-	return (rval);
+		return -1;
+	return 0;
 }

--------------080106030601040508090805--

>Unformatted:
 This is a multi-part message in MIME format.
 --------------080106030601040508090805
 Content-Type: text/plain; charset=ISO-8859-15
 Content-Transfer-Encoding: 7bit