Index | Thread | Search

From:
Tim van der Molen <tim@kariliq.nl>
Subject:
Re: Sync memmem.c and strstr.c with upstream musl
To:
Theo Buehler <tb@theobuehler.org>
Cc:
tech@openbsd.org
Date:
Tue, 23 Jun 2026 15:02:01 +0200

Download raw body.

Thread
Theo Buehler (2026-06-23 14:25 +0200):
> On Tue, Jun 23, 2026 at 02:20:41PM +0200, Tim van der Molen wrote:
> > This diff merges two fixes from upstream musl. The first fixes a shift
> > overflow, the second fixes a comment. See:
> > 
> > https://git.musl-libc.org/cgit/musl/commit?id=593caa456309714402ca4cb77c3770f4c24da9da
> > https://git.musl-libc.org/cgit/musl/commit?id=c53e9b239418eb3e0e8be256abd0f6ad7608bbcf
> > 
> > OK?
> 
> ok tb (I'd commit the comment fix separately, but your call)

Yes, you're probably right.

> > 
> > Index: memmem.c
> > ===================================================================
> > RCS file: /cvs/src/lib/libc/string/memmem.c,v
> > diff -p -u -r1.5 memmem.c
> > --- memmem.c	16 Apr 2020 12:39:28 -0000	1.5
> > +++ memmem.c	23 Jun 2026 12:11:21 -0000
> > @@ -38,8 +38,8 @@ twobyte_memmem(const unsigned char *h, s
> >  static char *
> >  threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
> >  {
> > -	uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
> > -	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
> > +	uint32_t nw = (uint32_t)n[0]<<24 | n[1]<<16 | n[2]<<8;
> > +	uint32_t hw = (uint32_t)h[0]<<24 | h[1]<<16 | h[2]<<8;
> >  	for (h+=3, k-=3; k; k--, hw = (hw|*h++)<<8)
> >  		if (hw == nw) return (char *)h-3;
> >  	return hw == nw ? (char *)h-3 : 0;
> > @@ -48,8 +48,8 @@ threebyte_memmem(const unsigned char *h,
> >  static char *
> >  fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
> >  {
> > -	uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
> > -	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
> > +	uint32_t nw = (uint32_t)n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
> > +	uint32_t hw = (uint32_t)h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
> >  	for (h+=4, k-=4; k; k--, hw = hw<<8 | *h++)
> >  		if (hw == nw) return (char *)h-4;
> >  	return hw == nw ? (char *)h-4 : 0;
> > Index: strstr.c
> > ===================================================================
> > RCS file: /cvs/src/lib/libc/string/strstr.c,v
> > diff -p -u -r1.9 strstr.c
> > --- strstr.c	16 Apr 2020 12:37:52 -0000	1.9
> > +++ strstr.c	23 Jun 2026 12:11:21 -0000
> > @@ -37,8 +37,8 @@ twobyte_strstr(const unsigned char *h, c
> >  static char *
> >  threebyte_strstr(const unsigned char *h, const unsigned char *n)
> >  {
> > -	uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
> > -	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
> > +	uint32_t nw = (uint32_t)n[0]<<24 | n[1]<<16 | n[2]<<8;
> > +	uint32_t hw = (uint32_t)h[0]<<24 | h[1]<<16 | h[2]<<8;
> >  	for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8);
> >  	return *h ? (char *)h-2 : 0;
> >  }
> > @@ -46,8 +46,8 @@ threebyte_strstr(const unsigned char *h,
> >  static char *
> >  fourbyte_strstr(const unsigned char *h, const unsigned char *n)
> >  {
> > -	uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
> > -	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
> > +	uint32_t nw = (uint32_t)n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
> > +	uint32_t hw = (uint32_t)h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
> >  	for (h+=3; *h && hw != nw; hw = hw<<8 | *++h);
> >  	return *h ? (char *)h-3 : 0;
> >  }
> > @@ -129,7 +129,7 @@ twoway_strstr(const unsigned char *h, co
> >  	for (;;) {
> >  		/* Update incremental end-of-haystack pointer */
> >  		if (z-h < l) {
> > -			/* Fast estimate for MIN(l,63) */
> > +			/* Fast estimate for MAX(l,63) */
> >  			size_t grow = l | 63;
> >  			const unsigned char *z2 = memchr(z, 0, grow);
> >  			if (z2) {
> >