From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: from second.openwall.net (second.openwall.net [193.110.157.125]) by inbox.vuxu.org (Postfix) with SMTP id 52D0125BDB for ; Wed, 28 Feb 2024 05:40:33 +0100 (CET) Received: (qmail 15673 invoked by uid 550); 28 Feb 2024 04:36:53 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 15637 invoked from network); 28 Feb 2024 04:36:53 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=s31663417; t=1709095218; x=1709700018; i=nullplan@gmx.net; bh=upxbH8DIZjF8cIUcUTNsDpCjC1aeegBTWPvZ77wU8/4=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:References: In-Reply-To; b=Hg47aKHlwadObq1jEMDtYHAvGe1mpqja/mwM+9vedgwKOhZslO8hi2olMayPk5hf UZKs2UJSqqlGngVdR6JQTkOj0QDqahklPeUVgMVzf1scWSOMUgUxFfAdPiliRajaX QhvUd/C5VBFUp5qIYqYkZz8gLg6+D7YoUkMEI6wY5Tgh6kvl0LNiHJ8Qr4PruKMFh sU5Bf4mccOEN/9W5/52UFxh2nwgTnD+3Jdb/+IH5tO444cK+Pqtzo+shrC28vkmbE tlgi+JBDyU1l1ISbzk8gRn/HTJwTn+R6iRtXFtQ0h9RKvHAk/DFeT2NjoGaypDxPl dI+Zg49H+eYw2rmikA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Date: Wed, 28 Feb 2024 05:40:17 +0100 From: Markus Wichmann To: musl@lists.openwall.com Cc: James Tirta Halim Message-ID: References: <20240227140756.216904-1-tirtajames45@gmail.com> <20240227144926.GK4163@brightrain.aerifal.cx> <20240227145123.GL4163@brightrain.aerifal.cx> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240227145123.GL4163@brightrain.aerifal.cx> X-Provags-ID: V03:K1:qB4sD2aoZ8st/2CwFbpQzzypjmpN0SjLgY5RxrZVlZ38Aq/9PBr 2GKWEAy8PUwfMyum38o01ybrvUr0xbVNtoMGaEQGNimg7IxpM9vIG1zMS6rKbS/HO07mfta 1luoPMfoCeeda7Jlyz4+g88FIs5OCFdrxoaBL3Ug61rmapMKuVoux/hP58FAVCiQjZwm7KM nD8QdkgqoZkHXuXNTEXpw== UI-OutboundReport: notjunk:1;M01:P0:bTcHP8ODuvg=;uVEez4eRQF2BBGz7rxwkCTQuMND +wY4Zv5Mct2A5IKf0kH1Om209w/3ygQ8eS9FUYqhIS/yUdDJFycytmTLGqDGlWcZWwghbsyN5 pOZlo9I29GhJk7TGUX2icRNznk2h5OZD1EltHXhlDzB9czAOy/6JKUdL63hl/xUrZjgnKPWK1 BJ9u+Hl3Z9o+l2Xd3VOlcvIv953zWWD7FsgJzuXzvKBdkqF2d0Dh5HWMJmT9b+KKWJWtPXZW1 hdnbi4n9BrZGgx2+uu4bT+hvHqmiBqchZTKUB0fzke6BSBDfT8SmODzSxRfTP5jVwzS3+JFgq NF4Zk2pIXgFqfaXvyweQ7/HL4chnJ6PgBwXM+tBmLBklIjhjrj5qmyKTGgIQvZm3V3uQnUdzI 6eid0lvb+RmYhkFcEnMld5XZYoyswV3D7hpDtBAZdlgxWOMivXgC6znuiMvr/6lAVr5U+6t2V yy5ONnxJ4W+8KeoryjZ8Zh9G+lzyZ89FQbJMbnp66TUdlFAAZeXYarQlxIO1VUOjh5+MT6pPu oh7XNhRdCeRnE3YqOnZgGMCjECdZJS/53I4bRWwGL5P4RSJfDbs4s5HLGjW1zWfgTthUaputa l/aPD4y0ijgu4jMQQS/GSEvTvQaFkoOYh2vI08VR6omC2sfs0f5ceC1zUwrEikkT+nTm6g5wB W71//+5qWm7tWFR5Toc/zlil/hC7Vn3uq29mhQvAVqr/BgQHUXhIiFWhp7cpaIzXvaEgTT2qH 3Nl0JfU0wTW/h3i7KR+HJgwaAg5ejG1gQ0U96isK8OIQGM3KlKhGSn/K4thSthfs0WNbt2JQc vPq+8Wr+SZ8RuUEkmcQk99h0A+htWG2RczSWTkgxY28ss= Content-Transfer-Encoding: quoted-printable Subject: Re: [musl] [PATCH] add memcmpeq: memcmp that returns length of first mismatch Am Tue, Feb 27, 2024 at 09:51:24AM -0500 schrieb Rich Felker: > On Tue, Feb 27, 2024 at 09:49:27AM -0500, Rich Felker wrote: > > On Tue, Feb 27, 2024 at 09:07:56PM +0700, James Tirta Halim wrote: > > > --- > > > src/string/mempcmpeq.c | 19 +++++++++++++++++++ > > > 1 file changed, 19 insertions(+) > > > create mode 100644 src/string/mempcmpeq.c > > > > > > diff --git a/src/string/mempcmpeq.c b/src/string/mempcmpeq.c > > > new file mode 100644 > > > index 00000000..bc17bc58 > > > --- /dev/null > > > +++ b/src/string/mempcmpeq.c > > > @@ -0,0 +1,19 @@ > > > +#include > > > + > > > +size_t > > > +mempcmpeq(const void *s1, > > > + const void *s2, > > > + size_t n) > > > +{ > > > + const size_t length =3D n; > > > +#ifdef __GNUC__ > > > + typedef size_t __attribute__((__may_alias__)) word; > > > + const unsigned char *p1 =3D (const unsigned char *)s1; > > > + const unsigned char *p2 =3D (const unsigned char *)s2; > > > + for (; n >=3D sizeof(word) && *(word *)p1 =3D=3D *(word *)p2; p1+= =3Dsizeof(word), p2+=3Dsizeof(word), n-=3Dsizeof(word)); > > > +#endif > > > + for (; n; --n) > > > + if (*p1++ !=3D *p2++) > > > + return (size_t)((p1 - 1 - (const unsigned char *)s1)); > > > + return length; > > > +} > > > -- > > > 2.44.0 > > > > This code does not do what you claim it does and does not seem to > > match your test results. As written, it should return immediately, > > because the entire body except > > > > > + const size_t length =3D n; > > > + return length; > > > > is dead code. > > Sorry, this part is wrong -- I missed the return above, so it appears > it actually does behave as described. Dunno how I overlooked that. > EMORECOFFEE. > > Rich And I puzzled over it a solid five minutes wondering how you got to your conclusion. But there are a few more issues here: 1. The declarations of p1 and p2 need to be moved outside the #ifdef, since they are used outside the #ifdef. 2. The explicit conversions in the initializations are unnecessary and should be elided. 3. If this function is to be used by strstr, it needs to get a double underscore name, since strstr is an ISO-C function and must only call ISO-C functions or double underscore functions (wonky counterexamples like system() notwithstanding. And even that one doesn't call extension functions). If the function should also be an externally accessible extension, it can get a weak alias. 4. All the other musl C code avoids misaligned word access. I don't know which architecture/ABI doesn't allow it, but it is nevertheless the case. And I don't know if this function is still worth it if it has to take all possible misalignments into account. Ciao, Markus