Re: [PATCH] lib/string: fix memchr_inv() for large ranges

From: Andy Shevchenko

Date: Tue Jun 23 2026 - 16:55:10 EST


On Sun, Jun 21, 2026 at 12:11:33PM +0000, Bradley Morgan wrote:
> memchr_inv() takes a size_t length but counts 8 byte words in an
> unsigned int. At 32GiB that count wraps, so the scan can quietly miss
> most of the range.

Wow! Is it real life use case?

> Use size_t for the word count.

...

> @@ -837,7 +837,8 @@ void *memchr_inv(const void *start, int c, size_t bytes)
> {
> u8 value = c;
> u64 value64;
> - unsigned int words, prefix;
> + size_t words;
> + unsigned int prefix;

Perhaps move towards reversed xmas tree while at it?

unsigned int prefix;
size_t words;
u8 value = c;
u64 value64;

(same lines touched, no extra).

> if (bytes <= 16)
> return check_bytes8(start, value, bytes);

--
With Best Regards,
Andy Shevchenko