Skip to contentSkip to conversation

lib/string: fix memchr_inv() for large ranges

PATCH Applied Bradley Morgan 2 participants 2 messages Jun 21 – Jun 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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()")
---
 lib/string.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/string.c +2 -1
index 1f9297e9776a..8ea7d4b9f0c0 100644
@@ -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
#1 lore raw
Quoted 3 lines · Bradley Morgan

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?

Quoted 1 line

Use size_t for 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 -0
 	if (bytes <= 16)
 		return check_bytes8(start, value, bytes);
Signature · 2 lines
With Best Regards,
Andy Shevchenko