Constantine A. Murenin (cnst) wrote,
Constantine A. Murenin
cnst

/sys/kern/kern_sysctl.c in FreeBSD

I was looking at this code for a few minutes thinking how it could possibly continue working as opposed to generating oid_number clashes after a second OID_AUTO element is registered onto any given subtree, until it finally hit me. If you're not too intimate with C, trust me, this patch does not break the code flow in the simplest test case, although its effects on one's mind may vary greatly! :)

http://moduli.net/grok/diff/freebsd/src/sys/kern/kern_sysctl.c?r2=1.103&r1=1.102
--- src/sys/kern/kern_sysctl.c	2001/01/05 07:00:44	1.102
+++ src/sys/kern/kern_sysctl.c	2001/01/24 04:35:13	1.103
@@ -37,7 +37,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
- * $FreeBSD: src/sys/kern/kern_sysctl.c,v 1.102 2001/01/05 07:00:44 jhb Exp $
+ * $FreeBSD: src/sys/kern/kern_sysctl.c,v 1.103 2001/01/24 04:35:13 mckusick Exp $
  */
 
 #include "opt_compat.h"
@@ -114,13 +114,11 @@ void sysctl_register_oid(struct sysctl_o
 	 * 100 to leave space for pre-assigned oid numbers.
 	 */
 	if (oidp->oid_number == OID_AUTO) {
-		/* First, find the highest oid in the parent list >99 */
-		n = 99;
-		SLIST_FOREACH(p, parent, oid_link) {
-			if (p->oid_number > n)
-				n = p->oid_number;
-		}
-		oidp->oid_number = n + 1;
+		static int newoid = 100;
+
+		oidp->oid_number = newoid++;
+		if (newoid == 0x7fffffff)
+			panic("out of oids");
 	}
 
 	/*

P.S. For the record, this patch did seem to generate some other problems, because it is based on certain assumptions that don't neccessarily hold, thus being less robust than what it has replaced. For example, see kern_sysctl.c#rev1.112.


2007-08-25 update: P.P.S. Whilst looking at some other issue, I've discovered that dillon@ actually backed out this mckusick's code from DragonFly, for the above reasons:
http://www.dragonflybsd.org/cvsweb/src/sys/kern/kern_sysctl.c#rev1.14
Tags: brainfuck, c, gsoc2007, gsoc2007.en, static
Subscribe
  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments