lib/string: fix memchr_inv() for large ranges
Subsystems (2)
PATCH 1/1 lib/string: fix memchr_inv() for large ranges
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.
Use size_t for the word count.
Fixes 798248206b59 ("lib/string.c: introduce memchr_inv()")
Signed-off-by Bradley Morgan <[email protected]>
--- lib/string.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/string.c +2 -1
@@ -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;if (bytes <=16 )return check_bytes8 (start, value, bytes);
Signature · 1 line
2.53.0
Quoted 3 lines · Bradley Morgan
memchr_inv()takes asize_tlength 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?
Quoted 1 line
Use
size_tfor the word count.
...
lib/string.c void *memchr_inv(const void *start, int c, size_t bytes) · line 841
Quoted 7 lines
lib/string.c +2
-1@@ -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).
Quoted 2 lines
lib/string.c +0
-0if (bytes <=16 )return check_bytes8 (start, value, bytes);
Signature · 2 lines
With Best Regards, Andy Shevchenko