From owner-freebsd-net@FreeBSD.ORG Thu Oct 12 07:20:35 2006 Return-Path: X-Original-To: freebsd-net@FreeBSD.org Delivered-To: freebsd-net@FreeBSD.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 9CCC316A416; Thu, 12 Oct 2006 07:20:35 +0000 (UTC) (envelope-from ru@rambler-co.ru) Received: from relay0.rambler.ru (relay0.rambler.ru [81.19.66.187]) by mx1.FreeBSD.org (Postfix) with ESMTP id 256B943D60; Thu, 12 Oct 2006 07:20:34 +0000 (GMT) (envelope-from ru@rambler-co.ru) Received: from relay0.rambler.ru (localhost [127.0.0.1]) by relay0.rambler.ru (Postfix) with ESMTP id B054B5FBB; Thu, 12 Oct 2006 11:20:32 +0400 (MSD) Received: from edoofus.park.rambler.ru (unknown [81.19.65.108]) by relay0.rambler.ru (Postfix) with ESMTP id A8DE35F50; Thu, 12 Oct 2006 11:20:32 +0400 (MSD) Received: (from ru@localhost) by edoofus.park.rambler.ru (8.13.8/8.13.8) id k9C7KaJj061341; Thu, 12 Oct 2006 11:20:36 +0400 (MSD) (envelope-from ru) Date: Thu, 12 Oct 2006 11:20:36 +0400 From: Ruslan Ermilov To: Bruce Evans Message-ID: <20061012072036.GA60767@rambler-co.ru> References: <20061011090241.GA2831@FreeBSD.czest.pl> <20061011094049.GA24964@rambler-co.ru> <20061012052101.A814@epsplex.bde.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="XsQoSWH+UP9D9v3l" Content-Disposition: inline In-Reply-To: <20061012052101.A814@epsplex.bde.org> User-Agent: Mutt/1.5.13 (2006-08-11) X-Virus-Scanned: No virus found Cc: freebsd-net@FreeBSD.org, andre@FreeBSD.org Subject: Re: [PATCH] Make hash.h usable in the kernel X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 12 Oct 2006 07:20:35 -0000 --XsQoSWH+UP9D9v3l Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Oct 12, 2006 at 05:21:09AM +1000, Bruce Evans wrote: > On Wed, 11 Oct 2006, Ruslan Ermilov wrote: > >%%% > >Index: sys/sys/hash.h > >=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > >RCS file: /home/ncvs/src/sys/sys/hash.h,v > >retrieving revision 1.2 > >diff -u -p -r1.2 hash.h > >--- sys/sys/hash.h 12 Mar 2006 15:34:33 -0000 1.2 > >+++ sys/sys/hash.h 11 Oct 2006 09:38:50 -0000 > >@@ -86,7 +86,7 @@ hash32_strn(const void *buf, size_t len, > > * namei() hashing of path name parts. > > */ > >static __inline uint32_t > >-hash32_stre(const void *buf, int end, char **ep, uint32_t hash) > >+hash32_stre(const void *buf, int end, const char **ep, uint32_t hash) > >{ > > const unsigned char *p =3D buf; > > >=20 > I think this would break passing ep in almost all callers, >=20 There are no callers of these functions yet, at least not in the current FreeBSD kernel. There are only 2 callers in OpenBSD, both in sys/kern/vfs_lookup.c > in the same > way that "fixing" the corresponding arg in the strtol(3) family would > break almost all callers. >=20 Yes, but strtol(3) has seen more life in sin. ;) > Callers want and need to pass char **, but > char ** is not compatible with const char **. >=20 Not compatible, but "char **" can safely be casted to "const char **". > Callers want to do this > because it's easier to write "char *end; ... &end", and they often > need to do this so that they can modify the resulting *end. >=20 But this is bad practice; if string is really const, writing to *end will SIGBUS, and the fact that interface has it spelled as "char **" doesn't mitigate it: : #include :=20 : static const char *s =3D "123a"; :=20 : int : main(void) : { : long v; : char *endptr; :=20 : endptr =3D NULL; : v =3D strtol(s, &endptr, 0); : if (endptr !=3D NULL) : *endptr =3D '\0'; : return (0); : } OTOH, if string is really modifiable, then simple casting when calling a function works: : #include :=20 : void foo(const char *, char *); : void bar(const char *, const char **); :=20 : void : foo(const char *s1, char *s2) : { : const char *end1 =3D NULL; : char *end2 =3D NULL; :=20 : bar(s1, &end1); : bar(s2, (const char **)&end2); : } Or differently: it's safe (and possible) to do "end1 =3D end2", but not the opposite. > Changing > the prototype forces all callers to use "const char **end; ... &end", ^ extra `*' > and then if they want to modify *end, to convert `end' to plain char *. >=20 Not necessarily, see above. And from the function's POV (whose prototype we're considering), "end" will be made to point to a substring of a const string, so obviously it will also point to a const string. > Modifying *end is only valid if the original string is modifyable, and > this case ends up needing lots of ugly casting away of const, which > leads to compiler warnings, which lead to even uglier things like the > __DECONST() mistake to "fix" the warnings. >=20 Not *lots* actually. Passing "char *" where "const char *" is required is safe and allowed, passing "char **" as "const char **" is allowed but requires a (safe) cast. > >@@ -94,7 +94,7 @@ hash32_stre(const void *buf, int end, ch > > hash =3D HASHSTEP(hash, *p++); > > > > if (ep) > >- *ep =3D (char *)p; > >+ *ep =3D (const char *)p; > > > > return hash; > >} >=20 > Doesn't this cause a cast-qual warning in the kernel? >=20 Why? None of qualifiers are lost as a result of cast; both "p" and "ep" are pointers to const-qualified base types. (No, it doesn't cause a warning.) Cheers, --=20 Ruslan Ermilov ru@FreeBSD.org FreeBSD committer --XsQoSWH+UP9D9v3l Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (FreeBSD) iD8DBQFFLezEqRfpzJluFF4RArz+AJwPTP7EDDuEcZDi4aF49yIw8CBoLgCdEFLI +uGMOqytUx8bpaxFFqX/3xw= =r2jR -----END PGP SIGNATURE----- --XsQoSWH+UP9D9v3l--