Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Tue, 27 Feb 2024 09:51:24 -0500
From: Rich Felker <dalias@...c.org>
To: James Tirta Halim <tirtajames45@...il.com>
Cc: musl@...ts.openwall.com
Subject: Re: [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 <stddef.h>
> > +
> > +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

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.