From christoph.mallon@gmx.de  Sat Feb 23 15:44:20 2013
Return-Path: <christoph.mallon@gmx.de>
Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115])
	by hub.freebsd.org (Postfix) with ESMTP id 4B7BA4A2
	for <freebsd-gnats-submit@freebsd.org>; Sat, 23 Feb 2013 15:44:20 +0000 (UTC)
	(envelope-from christoph.mallon@gmx.de)
Received: from mout.gmx.net (mout.gmx.net [212.227.17.20])
	by mx1.freebsd.org (Postfix) with ESMTP id E4E06237
	for <freebsd-gnats-submit@freebsd.org>; Sat, 23 Feb 2013 15:44:19 +0000 (UTC)
Received: from mailout-de.gmx.net ([10.1.76.30]) by mrigmx.server.lan
 (mrigmx001) with ESMTP (Nemesis) id 0M8LTo-1V4pdJ0eDC-00vyoa for
 <FreeBSD-gnats-submit@freebsd.org>; Sat, 23 Feb 2013 16:44:19 +0100
Received: (qmail invoked by alias); 23 Feb 2013 15:44:18 -0000
Received: from p5B132CB8.dip.t-dialin.net (EHLO rotluchs.lokal) [91.19.44.184]
  by mail.gmx.net (mp030) with SMTP; 23 Feb 2013 16:44:18 +0100
Received: from tron by rotluchs.lokal with local (Exim 4.80.1 (FreeBSD))
	(envelope-from <christoph.mallon@gmx.de>)
	id 1U9HGr-000Dez-I3
	for FreeBSD-gnats-submit@freebsd.org; Sat, 23 Feb 2013 16:44:17 +0100
Message-Id: <E1U9HGr-000Dez-I3@rotluchs.lokal>
Date: Sat, 23 Feb 2013 16:44:17 +0100
From: Christoph Mallon <christoph.mallon@gmx.de>
Reply-To: Christoph Mallon <christoph.mallon@gmx.de>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [PATCH] Simplify and clean up kern_sysctl
X-Send-Pr-Version: 3.114
X-GNATS-Notify: Marius Strobl <marius@FreeBSD.org>

>Number:         176373
>Category:       kern
>Synopsis:       [kernel] [patch] Simplify and clean up kern_sysctl
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          update
>Submitter-Id:   current-users
>Arrival-Date:   Sat Feb 23 15:50:00 UTC 2013
>Closed-Date:    Fri Mar 08 11:28:04 UTC 2013
>Last-Modified:  Fri Mar  8 11:30:01 UTC 2013
>Originator:     Christoph Mallon
>Release:        
>Organization:
>Environment:


	
>Description:
This series of patches performs some simplifications and clean ups in kern_sysctl:
- Use strdup() instead of reimplementing it.
- Use __DECONST instead of strange casts.
- Remove pointless #ifndef _SYS_SYSPROTO_H_.
- Reduce code duplication and simplify name2oid().
>How-To-Repeat:
	
>Fix:
Please apply these patches.

--- 0001-kern_sysctl-Use-strdup-instead-of-reimplementing-it.patch begins here ---
From c2614b9cffabd2e796e979dfefaaa28931506c13 Mon Sep 17 00:00:00 2001
From: Christoph Mallon <christoph.mallon@gmx.de>
Date: Sat, 23 Feb 2013 12:46:06 +0100
Subject: [PATCH 1/4] kern_sysctl: Use strdup() instead of reimplementing it.

---
 sys/kern/kern_sysctl.c | 22 ++++------------------
 1 file changed, 4 insertions(+), 18 deletions(-)

diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c
index 33296d3..6a7b58c 100644
--- a/sys/kern/kern_sysctl.c
+++ b/sys/kern/kern_sysctl.c
@@ -462,8 +462,6 @@ sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
 	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
 {
 	struct sysctl_oid *oidp;
-	ssize_t len;
-	char *newname;
 
 	/* You have to hook up somewhere.. */
 	if (parent == NULL)
@@ -490,11 +488,7 @@ sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
 	SLIST_NEXT(oidp, oid_link) = NULL;
 	oidp->oid_number = number;
 	oidp->oid_refcnt = 1;
-	len = strlen(name);
-	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
-	bcopy(name, newname, len + 1);
-	newname[len] = '\0';
-	oidp->oid_name = newname;
+	oidp->oid_name = strdup(name, M_SYSCTLOID);
 	oidp->oid_handler = handler;
 	oidp->oid_kind = CTLFLAG_DYN | kind;
 	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
@@ -508,12 +502,8 @@ sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
 		oidp->oid_arg2 = arg2;
 	}
 	oidp->oid_fmt = fmt;
-	if (descr) {
-		int len = strlen(descr) + 1;
-		oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
-		if (oidp->oid_descr)
-			strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
-	}
+	if (descr)
+		oidp->oid_descr = strdup(descr, M_SYSCTLOID);
 	/* Update the context, if used */
 	if (clist != NULL)
 		sysctl_ctx_entry_add(clist, oidp);
@@ -529,14 +519,10 @@ sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
 void
 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
 {
-	ssize_t len;
 	char *newname;
 	void *oldname;
 
-	len = strlen(name);
-	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
-	bcopy(name, newname, len + 1);
-	newname[len] = '\0';
+	newname = strdup(name, M_SYSCTLOID);
 	SYSCTL_XLOCK();
 	oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
 	oidp->oid_name = newname;
-- 
1.8.1.3
--- 0001-kern_sysctl-Use-strdup-instead-of-reimplementing-it.patch ends here ---

--- dummy1 begins here ---
dummy file, because GNATS damages every other file
--- dummy1 ends here ---

--- 0002-kern_sysctl-Use-__DECONST-instead-of-strange-casts.patch begins here ---
From 1dfbd4970ca555fe1eb7a291dd04c3863e50297b Mon Sep 17 00:00:00 2001
From: Christoph Mallon <christoph.mallon@gmx.de>
Date: Sat, 23 Feb 2013 12:48:43 +0100
Subject: [PATCH 2/4] kern_sysctl: Use __DECONST instead of strange casts.

---
 sys/kern/kern_sysctl.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c
index 6a7b58c..b1d33cb 100644
--- a/sys/kern/kern_sysctl.c
+++ b/sys/kern/kern_sysctl.c
@@ -444,9 +444,8 @@ sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
 				SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
 			}
 			if (oidp->oid_descr)
-				free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
-			free((void *)(uintptr_t)(const void *)oidp->oid_name,
-			     M_SYSCTLOID);
+				free(__DECONST(char *, oidp->oid_descr), M_SYSCTLOID);
+			free(__DECONST(char *, oidp->oid_name, M_SYSCTLOID));
 			free(oidp, M_SYSCTLOID);
 		}
 	}
@@ -520,11 +519,11 @@ void
 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
 {
 	char *newname;
-	void *oldname;
+	char *oldname;
 
 	newname = strdup(name, M_SYSCTLOID);
 	SYSCTL_XLOCK();
-	oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
+	oldname = __DECONST(char *, oidp->oid_name);
 	oidp->oid_name = newname;
 	SYSCTL_XUNLOCK();
 	free(oldname, M_SYSCTLOID);
-- 
1.8.1.3
--- 0002-kern_sysctl-Use-__DECONST-instead-of-strange-casts.patch ends here ---

--- dummy2 begins here ---
dummy file, because GNATS damages every other file
--- dummy2 ends here ---

--- 0003-kern_sysctl-Remove-pointless-ifndef-_SYS_SYSPROTO_H_.patch begins here ---
From e8423de48e8864ea8db1e6cb8655288804a985bc Mon Sep 17 00:00:00 2001
From: Christoph Mallon <christoph.mallon@gmx.de>
Date: Sat, 23 Feb 2013 12:53:57 +0100
Subject: [PATCH 3/4] kern_sysctl: Remove pointless #ifndef _SYS_SYSPROTO_H_.

The first thing, which the included file sys/sysproto.h does, is setting _SYS_SYSPROTO_H_.
---
 sys/kern/kern_sysctl.c | 10 ----------
 1 file changed, 10 deletions(-)

diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c
index b1d33cb..74e4137 100644
--- a/sys/kern/kern_sysctl.c
+++ b/sys/kern/kern_sysctl.c
@@ -1522,16 +1522,6 @@ sysctl_root(SYSCTL_HANDLER_ARGS)
 	return (error);
 }
 
-#ifndef _SYS_SYSPROTO_H_
-struct sysctl_args {
-	int	*name;
-	u_int	namelen;
-	void	*old;
-	size_t	*oldlenp;
-	void	*new;
-	size_t	newlen;
-};
-#endif
 int
 sys___sysctl(struct thread *td, struct sysctl_args *uap)
 {
-- 
1.8.1.3
--- 0003-kern_sysctl-Remove-pointless-ifndef-_SYS_SYSPROTO_H_.patch ends here ---

--- dummy3 begins here ---
dummy file, because GNATS damages every other file
--- dummy3 ends here ---

--- 0004-kern_sysctl-Reduce-code-duplication-and-simplify-nam.patch begins here ---
From 1123715a295de35502afc6e59ae280c16d598236 Mon Sep 17 00:00:00 2001
From: Christoph Mallon <christoph.mallon@gmx.de>
Date: Sat, 23 Feb 2013 16:04:53 +0100
Subject: [PATCH 4/4] kern_sysctl: Reduce code duplication and simplify
 name2oid().

---
 sys/kern/kern_sysctl.c | 36 ++++++++----------------------------
 1 file changed, 8 insertions(+), 28 deletions(-)

diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c
index 74e4137..687d07a 100644
--- a/sys/kern/kern_sysctl.c
+++ b/sys/kern/kern_sysctl.c
@@ -808,39 +808,26 @@ static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_CAPRD,
 static int
 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
 {
-	int i;
 	struct sysctl_oid *oidp;
 	struct sysctl_oid_list *lsp = &sysctl__children;
 	char *p;
 
 	SYSCTL_ASSERT_XLOCKED();
 
-	if (!*name)
-		return (ENOENT);
-
-	p = name + strlen(name) - 1 ;
-	if (*p == '.')
-		*p = '\0';
-
-	*len = 0;
-
-	for (p = name; *p && *p != '.'; p++) 
-		;
-	i = *p;
-	if (i == '.')
-		*p = '\0';
+	for (*len = 0; *len < CTL_MAXNAME;) {
+		p = strsep(&name, ".");
 
-	oidp = SLIST_FIRST(lsp);
-
-	while (oidp && *len < CTL_MAXNAME) {
-		if (strcmp(name, oidp->oid_name)) {
-			oidp = SLIST_NEXT(oidp, oid_link);
-			continue;
+		oidp = SLIST_FIRST(lsp);
+		for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
+			if (oidp == NULL)
+				return (ENOENT);
+			if (strcmp(p, oidp->oid_name) == 0)
+				break;
 		}
 		*oid++ = oidp->oid_number;
 		(*len)++;
 
-		if (!i) {
+		if (name == NULL || *name == '\0') {
 			if (oidpp)
 				*oidpp = oidp;
 			return (0);
@@ -853,13 +840,6 @@ name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
 			break;
 
 		lsp = SYSCTL_CHILDREN(oidp);
-		oidp = SLIST_FIRST(lsp);
-		name = p+1;
-		for (p = name; *p && *p != '.'; p++) 
-				;
-		i = *p;
-		if (i == '.')
-			*p = '\0';
 	}
 	return (ENOENT);
 }
-- 
1.8.1.3
--- 0004-kern_sysctl-Reduce-code-duplication-and-simplify-nam.patch ends here ---


>Release-Note:
>Audit-Trail:

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/176373: commit references a PR
Date: Fri,  1 Mar 2013 18:49:23 +0000 (UTC)

 Author: marius
 Date: Fri Mar  1 18:49:14 2013
 New Revision: 247561
 URL: http://svnweb.freebsd.org/changeset/base/247561
 
 Log:
   - Use strdup(9) instead of reimplementing it.
   - Use __DECONST instead of strange casts.
   - Reduce code duplication and simplify name2oid().
   
   PR:		176373
   Submitted by:	Christoph Mallon
   MFC after:	1 week
 
 Modified:
   head/sys/kern/kern_sysctl.c
 
 Modified: head/sys/kern/kern_sysctl.c
 ==============================================================================
 --- head/sys/kern/kern_sysctl.c	Fri Mar  1 18:40:14 2013	(r247560)
 +++ head/sys/kern/kern_sysctl.c	Fri Mar  1 18:49:14 2013	(r247561)
 @@ -444,9 +444,9 @@ sysctl_remove_oid_locked(struct sysctl_o
  				SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
  			}
  			if (oidp->oid_descr)
 -				free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
 -			free((void *)(uintptr_t)(const void *)oidp->oid_name,
 -			     M_SYSCTLOID);
 +				free(__DECONST(char *, oidp->oid_descr),
 +				    M_SYSCTLOID);
 +			free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
  			free(oidp, M_SYSCTLOID);
  		}
  	}
 @@ -462,8 +462,6 @@ sysctl_add_oid(struct sysctl_ctx_list *c
  	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
  {
  	struct sysctl_oid *oidp;
 -	ssize_t len;
 -	char *newname;
  
  	/* You have to hook up somewhere.. */
  	if (parent == NULL)
 @@ -490,11 +488,7 @@ sysctl_add_oid(struct sysctl_ctx_list *c
  	SLIST_NEXT(oidp, oid_link) = NULL;
  	oidp->oid_number = number;
  	oidp->oid_refcnt = 1;
 -	len = strlen(name);
 -	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
 -	bcopy(name, newname, len + 1);
 -	newname[len] = '\0';
 -	oidp->oid_name = newname;
 +	oidp->oid_name = strdup(name, M_SYSCTLOID);
  	oidp->oid_handler = handler;
  	oidp->oid_kind = CTLFLAG_DYN | kind;
  	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
 @@ -508,12 +502,8 @@ sysctl_add_oid(struct sysctl_ctx_list *c
  		oidp->oid_arg2 = arg2;
  	}
  	oidp->oid_fmt = fmt;
 -	if (descr) {
 -		int len = strlen(descr) + 1;
 -		oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
 -		if (oidp->oid_descr)
 -			strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
 -	}
 +	if (descr)
 +		oidp->oid_descr = strdup(descr, M_SYSCTLOID);
  	/* Update the context, if used */
  	if (clist != NULL)
  		sysctl_ctx_entry_add(clist, oidp);
 @@ -529,16 +519,12 @@ sysctl_add_oid(struct sysctl_ctx_list *c
  void
  sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
  {
 -	ssize_t len;
  	char *newname;
 -	void *oldname;
 +	char *oldname;
  
 -	len = strlen(name);
 -	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
 -	bcopy(name, newname, len + 1);
 -	newname[len] = '\0';
 +	newname = strdup(name, M_SYSCTLOID);
  	SYSCTL_XLOCK();
 -	oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
 +	oldname = __DECONST(char *, oidp->oid_name);
  	oidp->oid_name = newname;
  	SYSCTL_XUNLOCK();
  	free(oldname, M_SYSCTLOID);
 @@ -823,39 +809,26 @@ static SYSCTL_NODE(_sysctl, 2, next, CTL
  static int
  name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
  {
 -	int i;
  	struct sysctl_oid *oidp;
  	struct sysctl_oid_list *lsp = &sysctl__children;
  	char *p;
  
  	SYSCTL_ASSERT_XLOCKED();
  
 -	if (!*name)
 -		return (ENOENT);
 +	for (*len = 0; *len < CTL_MAXNAME;) {
 +		p = strsep(&name, ".");
  
 -	p = name + strlen(name) - 1 ;
 -	if (*p == '.')
 -		*p = '\0';
 -
 -	*len = 0;
 -
 -	for (p = name; *p && *p != '.'; p++) 
 -		;
 -	i = *p;
 -	if (i == '.')
 -		*p = '\0';
 -
 -	oidp = SLIST_FIRST(lsp);
 -
 -	while (oidp && *len < CTL_MAXNAME) {
 -		if (strcmp(name, oidp->oid_name)) {
 -			oidp = SLIST_NEXT(oidp, oid_link);
 -			continue;
 +		oidp = SLIST_FIRST(lsp);
 +		for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
 +			if (oidp == NULL)
 +				return (ENOENT);
 +			if (strcmp(p, oidp->oid_name) == 0)
 +				break;
  		}
  		*oid++ = oidp->oid_number;
  		(*len)++;
  
 -		if (!i) {
 +		if (name == NULL || *name == '\0') {
  			if (oidpp)
  				*oidpp = oidp;
  			return (0);
 @@ -868,13 +841,6 @@ name2oid(char *name, int *oid, int *len,
  			break;
  
  		lsp = SYSCTL_CHILDREN(oidp);
 -		oidp = SLIST_FIRST(lsp);
 -		name = p+1;
 -		for (p = name; *p && *p != '.'; p++) 
 -				;
 -		i = *p;
 -		if (i == '.')
 -			*p = '\0';
  	}
  	return (ENOENT);
  }
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->closed 
State-Changed-By: marius 
State-Changed-When: Fri Mar 8 11:27:41 UTC 2013 
State-Changed-Why:  
Committed, thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=176373 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/176373: commit references a PR
Date: Fri,  8 Mar 2013 11:26:29 +0000 (UTC)

 Author: marius
 Date: Fri Mar  8 11:26:00 2013
 New Revision: 248035
 URL: http://svnweb.freebsd.org/changeset/base/248035
 
 Log:
   MFC: r247561
   
   - Use strdup(9) instead of reimplementing it.
   - Use __DECONST instead of strange casts.
   - Reduce code duplication and simplify name2oid().
   
   PR:		176373
   Submitted by:	Christoph Mallon
 
 Modified:
   stable/8/sys/kern/kern_sysctl.c
 Directory Properties:
   stable/8/sys/   (props changed)
   stable/8/sys/kern/   (props changed)
 
 Modified: stable/8/sys/kern/kern_sysctl.c
 ==============================================================================
 --- stable/8/sys/kern/kern_sysctl.c	Fri Mar  8 11:25:52 2013	(r248034)
 +++ stable/8/sys/kern/kern_sysctl.c	Fri Mar  8 11:26:00 2013	(r248035)
 @@ -420,9 +420,9 @@ sysctl_remove_oid_locked(struct sysctl_o
  				SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
  			}
  			if (oidp->oid_descr)
 -				free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
 -			free((void *)(uintptr_t)(const void *)oidp->oid_name,
 -			     M_SYSCTLOID);
 +				free(__DECONST(char *, oidp->oid_descr),
 +				    M_SYSCTLOID);
 +			free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
  			free(oidp, M_SYSCTLOID);
  		}
  	}
 @@ -439,8 +439,6 @@ sysctl_add_oid(struct sysctl_ctx_list *c
  	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
  {
  	struct sysctl_oid *oidp;
 -	ssize_t len;
 -	char *newname;
  
  	/* You have to hook up somewhere.. */
  	if (parent == NULL)
 @@ -467,11 +465,7 @@ sysctl_add_oid(struct sysctl_ctx_list *c
  	SLIST_NEXT(oidp, oid_link) = NULL;
  	oidp->oid_number = number;
  	oidp->oid_refcnt = 1;
 -	len = strlen(name);
 -	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
 -	bcopy(name, newname, len + 1);
 -	newname[len] = '\0';
 -	oidp->oid_name = newname;
 +	oidp->oid_name = strdup(name, M_SYSCTLOID);
  	oidp->oid_handler = handler;
  	oidp->oid_kind = CTLFLAG_DYN | kind;
  	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
 @@ -484,12 +478,8 @@ sysctl_add_oid(struct sysctl_ctx_list *c
  		oidp->oid_arg2 = arg2;
  	}
  	oidp->oid_fmt = fmt;
 -	if (descr) {
 -		int len = strlen(descr) + 1;
 -		oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
 -		if (oidp->oid_descr)
 -			strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
 -	}
 +	if (descr)
 +		oidp->oid_descr = strdup(descr, M_SYSCTLOID);
  	/* Update the context, if used */
  	if (clist != NULL)
  		sysctl_ctx_entry_add(clist, oidp);
 @@ -505,16 +495,12 @@ sysctl_add_oid(struct sysctl_ctx_list *c
  void
  sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
  {
 -	ssize_t len;
  	char *newname;
 -	void *oldname;
 +	char *oldname;
  
 -	len = strlen(name);
 -	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
 -	bcopy(name, newname, len + 1);
 -	newname[len] = '\0';
 +	newname = strdup(name, M_SYSCTLOID);
  	SYSCTL_XLOCK();
 -	oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
 +	oldname = __DECONST(char *, oidp->oid_name);
  	oidp->oid_name = newname;
  	SYSCTL_XUNLOCK();
  	free(oldname, M_SYSCTLOID);
 @@ -785,39 +771,26 @@ static SYSCTL_NODE(_sysctl, 2, next, CTL
  static int
  name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
  {
 -	int i;
  	struct sysctl_oid *oidp;
  	struct sysctl_oid_list *lsp = &sysctl__children;
  	char *p;
  
  	SYSCTL_ASSERT_XLOCKED();
  
 -	if (!*name)
 -		return (ENOENT);
 +	for (*len = 0; *len < CTL_MAXNAME;) {
 +		p = strsep(&name, ".");
  
 -	p = name + strlen(name) - 1 ;
 -	if (*p == '.')
 -		*p = '\0';
 -
 -	*len = 0;
 -
 -	for (p = name; *p && *p != '.'; p++) 
 -		;
 -	i = *p;
 -	if (i == '.')
 -		*p = '\0';
 -
 -	oidp = SLIST_FIRST(lsp);
 -
 -	while (oidp && *len < CTL_MAXNAME) {
 -		if (strcmp(name, oidp->oid_name)) {
 -			oidp = SLIST_NEXT(oidp, oid_link);
 -			continue;
 +		oidp = SLIST_FIRST(lsp);
 +		for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
 +			if (oidp == NULL)
 +				return (ENOENT);
 +			if (strcmp(p, oidp->oid_name) == 0)
 +				break;
  		}
  		*oid++ = oidp->oid_number;
  		(*len)++;
  
 -		if (!i) {
 +		if (name == NULL || *name == '\0') {
  			if (oidpp)
  				*oidpp = oidp;
  			return (0);
 @@ -830,13 +803,6 @@ name2oid(char *name, int *oid, int *len,
  			break;
  
  		lsp = SYSCTL_CHILDREN(oidp);
 -		oidp = SLIST_FIRST(lsp);
 -		name = p+1;
 -		for (p = name; *p && *p != '.'; p++) 
 -				;
 -		i = *p;
 -		if (i == '.')
 -			*p = '\0';
  	}
  	return (ENOENT);
  }
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/176373: commit references a PR
Date: Fri,  8 Mar 2013 11:26:00 +0000 (UTC)

 Author: marius
 Date: Fri Mar  8 11:25:52 2013
 New Revision: 248034
 URL: http://svnweb.freebsd.org/changeset/base/248034
 
 Log:
   MFC: r247561
   
   - Use strdup(9) instead of reimplementing it.
   - Use __DECONST instead of strange casts.
   - Reduce code duplication and simplify name2oid().
   
   PR:		176373
   Submitted by:	Christoph Mallon
 
 Modified:
   stable/9/sys/kern/kern_sysctl.c
 Directory Properties:
   stable/9/sys/   (props changed)
 
 Modified: stable/9/sys/kern/kern_sysctl.c
 ==============================================================================
 --- stable/9/sys/kern/kern_sysctl.c	Fri Mar  8 10:43:03 2013	(r248033)
 +++ stable/9/sys/kern/kern_sysctl.c	Fri Mar  8 11:25:52 2013	(r248034)
 @@ -444,9 +444,9 @@ sysctl_remove_oid_locked(struct sysctl_o
  				SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
  			}
  			if (oidp->oid_descr)
 -				free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
 -			free((void *)(uintptr_t)(const void *)oidp->oid_name,
 -			     M_SYSCTLOID);
 +				free(__DECONST(char *, oidp->oid_descr),
 +				    M_SYSCTLOID);
 +			free(__DECONST(char *, oidp->oid_name), M_SYSCTLOID);
  			free(oidp, M_SYSCTLOID);
  		}
  	}
 @@ -462,8 +462,6 @@ sysctl_add_oid(struct sysctl_ctx_list *c
  	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
  {
  	struct sysctl_oid *oidp;
 -	ssize_t len;
 -	char *newname;
  
  	/* You have to hook up somewhere.. */
  	if (parent == NULL)
 @@ -490,11 +488,7 @@ sysctl_add_oid(struct sysctl_ctx_list *c
  	SLIST_NEXT(oidp, oid_link) = NULL;
  	oidp->oid_number = number;
  	oidp->oid_refcnt = 1;
 -	len = strlen(name);
 -	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
 -	bcopy(name, newname, len + 1);
 -	newname[len] = '\0';
 -	oidp->oid_name = newname;
 +	oidp->oid_name = strdup(name, M_SYSCTLOID);
  	oidp->oid_handler = handler;
  	oidp->oid_kind = CTLFLAG_DYN | kind;
  	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
 @@ -508,12 +502,8 @@ sysctl_add_oid(struct sysctl_ctx_list *c
  		oidp->oid_arg2 = arg2;
  	}
  	oidp->oid_fmt = fmt;
 -	if (descr) {
 -		int len = strlen(descr) + 1;
 -		oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
 -		if (oidp->oid_descr)
 -			strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
 -	}
 +	if (descr)
 +		oidp->oid_descr = strdup(descr, M_SYSCTLOID);
  	/* Update the context, if used */
  	if (clist != NULL)
  		sysctl_ctx_entry_add(clist, oidp);
 @@ -529,16 +519,12 @@ sysctl_add_oid(struct sysctl_ctx_list *c
  void
  sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
  {
 -	ssize_t len;
  	char *newname;
 -	void *oldname;
 +	char *oldname;
  
 -	len = strlen(name);
 -	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
 -	bcopy(name, newname, len + 1);
 -	newname[len] = '\0';
 +	newname = strdup(name, M_SYSCTLOID);
  	SYSCTL_XLOCK();
 -	oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
 +	oldname = __DECONST(char *, oidp->oid_name);
  	oidp->oid_name = newname;
  	SYSCTL_XUNLOCK();
  	free(oldname, M_SYSCTLOID);
 @@ -823,39 +809,26 @@ static SYSCTL_NODE(_sysctl, 2, next, CTL
  static int
  name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
  {
 -	int i;
  	struct sysctl_oid *oidp;
  	struct sysctl_oid_list *lsp = &sysctl__children;
  	char *p;
  
  	SYSCTL_ASSERT_XLOCKED();
  
 -	if (!*name)
 -		return (ENOENT);
 +	for (*len = 0; *len < CTL_MAXNAME;) {
 +		p = strsep(&name, ".");
  
 -	p = name + strlen(name) - 1 ;
 -	if (*p == '.')
 -		*p = '\0';
 -
 -	*len = 0;
 -
 -	for (p = name; *p && *p != '.'; p++) 
 -		;
 -	i = *p;
 -	if (i == '.')
 -		*p = '\0';
 -
 -	oidp = SLIST_FIRST(lsp);
 -
 -	while (oidp && *len < CTL_MAXNAME) {
 -		if (strcmp(name, oidp->oid_name)) {
 -			oidp = SLIST_NEXT(oidp, oid_link);
 -			continue;
 +		oidp = SLIST_FIRST(lsp);
 +		for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
 +			if (oidp == NULL)
 +				return (ENOENT);
 +			if (strcmp(p, oidp->oid_name) == 0)
 +				break;
  		}
  		*oid++ = oidp->oid_number;
  		(*len)++;
  
 -		if (!i) {
 +		if (name == NULL || *name == '\0') {
  			if (oidpp)
  				*oidpp = oidp;
  			return (0);
 @@ -868,13 +841,6 @@ name2oid(char *name, int *oid, int *len,
  			break;
  
  		lsp = SYSCTL_CHILDREN(oidp);
 -		oidp = SLIST_FIRST(lsp);
 -		name = p+1;
 -		for (p = name; *p && *p != '.'; p++) 
 -				;
 -		i = *p;
 -		if (i == '.')
 -			*p = '\0';
  	}
  	return (ENOENT);
  }
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
>Unformatted:
