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=-3.1 required=5.0 tests=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 91FD620267 for ; Tue, 27 Feb 2024 15:51:19 +0100 (CET) Received: (qmail 20464 invoked by uid 550); 27 Feb 2024 14:47:42 -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 20420 invoked from network); 27 Feb 2024 14:47:42 -0000 Date: Tue, 27 Feb 2024 09:51:24 -0500 From: Rich Felker To: James Tirta Halim Cc: musl@lists.openwall.com Message-ID: <20240227145123.GL4163@brightrain.aerifal.cx> References: <20240227140756.216904-1-tirtajames45@gmail.com> <20240227144926.GK4163@brightrain.aerifal.cx> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240227144926.GK4163@brightrain.aerifal.cx> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] [PATCH] add memcmpeq: memcmp that returns length of first mismatch 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 = n; > > +#ifdef __GNUC__ > > + typedef size_t __attribute__((__may_alias__)) word; > > + const unsigned char *p1 = (const unsigned char *)s1; > > + const unsigned char *p2 = (const unsigned char *)s2; > > + for (; n >= sizeof(word) && *(word *)p1 == *(word *)p2; p1+=sizeof(word), p2+=sizeof(word), n-=sizeof(word)); > > +#endif > > + for (; n; --n) > > + if (*p1++ != *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 = 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