From nobody Sat Sep 23 16:26:52 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RtF053ypYz4ttK5; Sat, 23 Sep 2023 16:27:01 +0000 (UTC) (envelope-from zlei@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [IPv6:2610:1c1:1:606c::24b:4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RtF053Q55z4S3r; Sat, 23 Sep 2023 16:27:01 +0000 (UTC) (envelope-from zlei@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695486421; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EGfq+URLOXZNoAVV5/CUfHf1PKQcntNEWgLMSWFLJ4s=; b=VY7q+yYbk67HG64cbdjaDFf9duyr639YdbKvq8eWCqFh5QCLd7R504M5Ttem1wXi36ayj6 HnYp1y8EYcOOVgaGZ4lDy3UntgEZtmpif0gjZ3Yym3RdvKu8fUyFmo2zTBF5rbt+1L2t1R M14d7Gz2gPYaqxj50fPW99LeUAy3VBSYRSrDnXieJ2nvTzzfm16nT9PNCx1amfqk8TloKv Xqm1drvJ+SClEf5aGAKQgmmXfY9dixamRqYsZtZce4wHMj1SvDvJloeeamojUTBwuKnBtA IgOxS/AdS4yBOnB3HHDyNIx2r8m15jgBRA14szRquaa3IEo8uUMolSS9PkYwGw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1695486421; a=rsa-sha256; cv=none; b=tyc5ttHp4sXcTreROps/qqNg9KgOTWxc8o54oZySQJ8CkUhEgrgDdeMz9zrV5938bIbDss c3Rr+6BqhnfiHVP1P24vwGbIhXq/67N1uyY8IP1jlb9TUHxpTwDClr31AeMJlx7SBwVoYc 5jqYnL+P3wNRaFvCpWzeh3sIwQKCuOWwP1A7LmujYEYUol38x6fE6U+/MEozKa/DNYyuSf JG3YGk8OuS913AfW8wTX5mmpPM1pibbOtOA+IGrJv2c7WrFzDlRZkLPuAgQeWjkcnV6y/D G94w3IJKUpFb2SHacKbm4En56L70zboh1NJI6B+L6FwqchZU9qV48PXK8OTr4A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695486421; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EGfq+URLOXZNoAVV5/CUfHf1PKQcntNEWgLMSWFLJ4s=; b=qp7f/PP8irpP9yT4rUsoMA0Msmj+TdLhFj/hE3RyyrKaqMPYJm/jcVAy8DxVy6pPobWbAB hy0e234JbyBTqSCdGz7e2Jnp9rlmgY9sScrsIR1kJMi9NxowaODGoNtGKyOWgDj6b7mZUE 2s8KU2iHS17QDxUZQflEBZxd2nc2qRMyglKl3a5paWoc3kdsGrLpghPDo5j9Z5xYjj+v/0 TvRKLErqO6kEP4U8TwCUn0sqP+3hL3bNres0U/c761VHLY95FNNQ+rX1WMkg22QWOcg7CL BSGooP6GsITLRJGPdnW1GLkqeU06hEM+uNfejilmI9jh6Z6v334+CvRHU56HSA== Received: from smtpclient.apple (ns1.oxydns.net [45.32.91.63]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) (Authenticated sender: zlei/mail) by smtp.freebsd.org (Postfix) with ESMTPSA id 4RtF036kf8z1MpW; Sat, 23 Sep 2023 16:26:59 +0000 (UTC) (envelope-from zlei@FreeBSD.org) Content-Type: text/plain; charset=us-ascii List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.4\)) Subject: Re: git: f80babf906b7 - main - kern_sysctl: Make name2oid() non-destructive to the name From: Zhenlei Huang In-Reply-To: <202309231623.38NGNrmQ003926@gitrepo.freebsd.org> Date: Sun, 24 Sep 2023 00:26:52 +0800 Cc: "src-committers@freebsd.org" , "dev-commits-src-all@freebsd.org" , "dev-commits-src-main@freebsd.org" Content-Transfer-Encoding: quoted-printable Message-Id: References: <202309231623.38NGNrmQ003926@gitrepo.freebsd.org> To: Alexander Motin X-Mailer: Apple Mail (2.3696.120.41.1.4) Good job ! > On Sep 24, 2023, at 12:23 AM, Alexander Motin wrote: >=20 > The branch main has been updated by mav: >=20 > URL: = https://cgit.FreeBSD.org/src/commit/?id=3Df80babf906b7be51b2a031ef26525893= c7bf4e31 >=20 > commit f80babf906b7be51b2a031ef26525893c7bf4e31 > Author: Alexander Motin > AuthorDate: 2023-09-23 16:13:46 +0000 > Commit: Alexander Motin > CommitDate: 2023-09-23 16:13:46 +0000 >=20 > kern_sysctl: Make name2oid() non-destructive to the name >=20 > It is not the first time I see it panicking while trying to modify > const memory. Lets make it safer and easier to use. While there, > mark few functions using it also const. >=20 > MFC after: 10 days > --- > sys/kern/kern_sysctl.c | 34 +++++++++++++++++++++++++++------- > 1 file changed, 27 insertions(+), 7 deletions(-) >=20 > diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c > index 780eb6099b07..a4bfe8e21aed 100644 > --- a/sys/kern/kern_sysctl.c > +++ b/sys/kern/kern_sysctl.c > @@ -127,7 +127,7 @@ static int sysctl_remove_oid_locked(struct = sysctl_oid *oidp, int del, > int recurse); > static int sysctl_old_kernel(struct sysctl_req *, const void *, = size_t); > static int sysctl_new_kernel(struct sysctl_req *, void *, size_t); > -static int name2oid(char *, int *, int *, struct sysctl_oid **); > +static int name2oid(const char *, int *, int *, struct sysctl_oid = **); >=20 > static struct sysctl_oid * > sysctl_find_oidname(const char *name, struct sysctl_oid_list *list) > @@ -143,6 +143,21 @@ sysctl_find_oidname(const char *name, struct = sysctl_oid_list *list) > return (NULL); > } >=20 > +static struct sysctl_oid * > +sysctl_find_oidnamelen(const char *name, size_t len, > + struct sysctl_oid_list *list) > +{ > + struct sysctl_oid *oidp; > + > + SYSCTL_ASSERT_LOCKED(); > + SYSCTL_FOREACH(oidp, list) { > + if (strncmp(oidp->oid_name, name, len) =3D=3D 0 && > + oidp->oid_name[len] =3D=3D '\0') > + return (oidp); > + } > + return (NULL); > +} > + > /* > * Initialization of the MIB tree. > * > @@ -978,7 +993,7 @@ SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, = sysctl_register_all, NULL); >=20 > #ifdef VIMAGE > static void > -sysctl_setenv_vnet(void *arg __unused, char *name) > +sysctl_setenv_vnet(void *arg __unused, const char *name) > { > struct sysctl_oid *oidp; > int oid[CTL_MAXNAME]; > @@ -1001,7 +1016,7 @@ out: > } >=20 > static void > -sysctl_unsetenv_vnet(void *arg __unused, char *name) > +sysctl_unsetenv_vnet(void *arg __unused, const char *name) > { > struct sysctl_oid *oidp; > int oid[CTL_MAXNAME]; > @@ -1419,21 +1434,26 @@ static SYSCTL_NODE(_sysctl, = CTL_SYSCTL_NEXTNOSKIP, nextnoskip, CTLFLAG_RD | > CTLFLAG_MPSAFE | CTLFLAG_CAPRD, sysctl_sysctl_next, ""); >=20 > static int > -name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp) > +name2oid(const char *name, int *oid, int *len, struct sysctl_oid = **oidpp) > { > struct sysctl_oid *oidp; > struct sysctl_oid_list *lsp =3D &sysctl__children; > + const char *n; >=20 > SYSCTL_ASSERT_LOCKED(); >=20 > for (*len =3D 0; *len < CTL_MAXNAME;) { > - oidp =3D sysctl_find_oidname(strsep(&name, "."), lsp); > + n =3D strchrnul(name, '.'); > + oidp =3D sysctl_find_oidnamelen(name, n - name, lsp); > if (oidp =3D=3D NULL) > return (ENOENT); > *oid++ =3D oidp->oid_number; > (*len)++; >=20 > - if (name =3D=3D NULL || *name =3D=3D '\0') { > + name =3D n; > + if (*name =3D=3D '.') > + name++; > + if (*name =3D=3D '\0') { > if (oidpp) > *oidpp =3D oidp; > return (0); > @@ -2999,7 +3019,7 @@ db_show_sysctl_all(int *oid, size_t len, int = flags) > * Show a sysctl by its user facing string > */ > static int > -db_sysctlbyname(char *name, int flags) > +db_sysctlbyname(const char *name, int flags) > { > struct sysctl_oid *oidp; > int oid[CTL_MAXNAME];