git: e5f93d10782d - main - show_sysctl_all: reduce copying, please coverity

From: Doug Moore <dougm_at_FreeBSD.org>
Date: Sat, 01 Oct 2022 17:21:54 UTC
The branch main has been updated by dougm:

URL: https://cgit.FreeBSD.org/src/commit/?id=e5f93d10782dd804d0ee776c22b972ec582f5c99

commit e5f93d10782dd804d0ee776c22b972ec582f5c99
Author:     Doug Moore <dougm@FreeBSD.org>
AuthorDate: 2022-10-01 17:20:04 +0000
Commit:     Doug Moore <dougm@FreeBSD.org>
CommitDate: 2022-10-01 17:20:04 +0000

    show_sysctl_all: reduce copying, please coverity
    
    Modify db_show_sysctl_all so that it does not copy more than once the
    data of the input oid, and so that what it passes to db_show_oid does
    not alarm coverity.
    
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D36847
---
 sys/kern/kern_sysctl.c | 47 ++++++++++++++++++++++-------------------------
 1 file changed, 22 insertions(+), 25 deletions(-)

diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c
index 8e4b69bb6eee..6427ff0c7ecf 100644
--- a/sys/kern/kern_sysctl.c
+++ b/sys/kern/kern_sysctl.c
@@ -2765,12 +2765,12 @@ static void
 db_show_oid_name(int *oid, size_t nlen)
 {
 	struct sysctl_oid *oidp;
-	int qoid[CTL_MAXNAME+2];
+	int qoid[CTL_MAXNAME + 2];
 	int error;
 
-	qoid[0] = 0;
+	qoid[0] = CTL_SYSCTL;
+	qoid[1] = CTL_SYSCTL_NAME;
 	memcpy(qoid + 2, oid, nlen * sizeof(int));
-	qoid[1] = 1;
 
 	error = sysctl_find_oid(qoid, nlen + 2, &oidp, NULL, NULL);
 	if (error)
@@ -2856,25 +2856,24 @@ static int
 db_show_sysctl_all(int *oid, size_t len, int flags)
 {
 	struct sysctl_oid *oidp;
-	int name1[CTL_MAXNAME + 2], name2[CTL_MAXNAME + 2];
-	size_t l1, l2;
+	int qoid[CTL_MAXNAME + 2], next[CTL_MAXNAME];
+	size_t nlen;
 
-	name1[0] = CTL_SYSCTL;
-	name1[1] = CTL_SYSCTL_NEXT;
-	l1 = 2;
+	qoid[0] = CTL_SYSCTL;
+	qoid[1] = CTL_SYSCTL_NEXT;
 	if (len) {
-		memcpy(name1 + 2, oid, len * sizeof(int));
-		l1 += len;
+		nlen = len;
+		memcpy(&qoid[2], oid, nlen * sizeof(int));
 	} else {
-		name1[2] = CTL_KERN;
-		l1++;
+		nlen = 1;
+		qoid[2] = CTL_KERN;
 	}
 	for (;;) {
-		int i, error;
+		int error;
+		size_t nextsize = sizeof(next);
 
-		l2 = sizeof(name2);
-		error = kernel_sysctl(kdb_thread, name1, l1,
-		    name2, &l2, NULL, 0, &l2, 0);
+		error = kernel_sysctl(kdb_thread, qoid, nlen + 2,
+		    next, &nextsize, NULL, 0, &nlen, 0);
 		if (error != 0) {
 			if (error == ENOENT)
 				return (0);
@@ -2882,27 +2881,25 @@ db_show_sysctl_all(int *oid, size_t len, int flags)
 				db_error("sysctl(next)");
 		}
 
-		l2 /= sizeof(int);
+		nlen /= sizeof(int);
 
-		if (l2 < (unsigned int)len)
+		if (nlen < (unsigned int)len)
 			return (0);
 
-		for (i = 0; i < len; i++)
-			if (name2[i] != oid[i])
-				return (0);
+		if (memcmp(&oid[0], &next[0], len * sizeof(int)) != 0)
+			return (0);
 
 		/* Find the OID in question */
-		error = sysctl_find_oid(name2, l2, &oidp, NULL, NULL);
+		error = sysctl_find_oid(next, nlen, &oidp, NULL, NULL);
 		if (error)
 			return (error);
 
-		i = db_show_oid(oidp, name2, l2, flags | DB_SYSCTL_SAFE_ONLY);
+		(void)db_show_oid(oidp, next, nlen, flags | DB_SYSCTL_SAFE_ONLY);
 
 		if (db_pager_quit)
 			return (0);
 
-		memcpy(name1+2, name2, l2 * sizeof(int));
-		l1 = 2 + l2;
+		memcpy(&qoid[2 + len], &next[len], (nlen - len) * sizeof(int));
 	}
 }