Date: Sun, 24 Sep 2023 00:26:52 +0800 From: Zhenlei Huang <zlei@FreeBSD.org> To: Alexander Motin <mav@FreeBSD.org> Cc: "src-committers@freebsd.org" <src-committers@FreeBSD.org>, "dev-commits-src-all@freebsd.org" <dev-commits-src-all@FreeBSD.org>, "dev-commits-src-main@freebsd.org" <dev-commits-src-main@FreeBSD.org> Subject: Re: git: f80babf906b7 - main - kern_sysctl: Make name2oid() non-destructive to the name Message-ID: <FD6C2834-3E33-4FB9-8482-0B05DC3B0B64@FreeBSD.org> In-Reply-To: <202309231623.38NGNrmQ003926@gitrepo.freebsd.org> References: <202309231623.38NGNrmQ003926@gitrepo.freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
Good job ! > On Sep 24, 2023, at 12:23 AM, Alexander Motin <mav@FreeBSD.org> 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 <mav@FreeBSD.org> > AuthorDate: 2023-09-23 16:13:46 +0000 > Commit: Alexander Motin <mav@FreeBSD.org> > 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];
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?FD6C2834-3E33-4FB9-8482-0B05DC3B0B64>