[libc-commits] [libc] [Libc] Fix has_zeroes() function not working with long Word type (PR #244197)
Hussam Alhassan via libc-commits
libc-commits at lists.llvm.org
Thu Nov 19 05:00:07 PST 2026
https://github.com/hussam-alhassan created https://github.com/llvm/llvm-project/pull/244197
The LOW_BITS constant in the has_zeroes() function is an unsigned int, which means if Word is a type larger than the platform's int e.g. a long, then the block parameter will be truncated during the subtraction operation. This results in zero bytes in the higher-order half not being detected.
>From 32f1f3aa081d3a1b145013b8f4834a1aeaf4649f Mon Sep 17 00:00:00 2001
From: Hussam Alhassan <hussam.alhassan at arm.com>
Date: Thu, 19 Nov 2026 12:50:54 +0000
Subject: [PATCH] [Libc] Fix has_zeroes() function not working with long Word
type
The LOW_BITS constant in the has_zeroes() function is an unsigned int,
which means if Word is a type larger than the platform's int e.g. a
long, then the block parameter will be truncated during the subtraction
operation. This results in zero bytes in the higher-order half not being
detected.
---
libc/src/string/string_length.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/string/string_length.h b/libc/src/string/string_length.h
index 8e9aa6bb5bc6d2..153dace1a8bb4a 100644
--- a/libc/src/string/string_length.h
+++ b/libc/src/string/string_length.h
@@ -106,7 +106,7 @@ template <typename Word> LIBC_INLINE constexpr Word repeat_byte(Word byte) {
// high bit set will no longer have it set, narrowing the list of bytes which
// result in non-zero values to just the zero byte.
template <typename Word> LIBC_INLINE constexpr bool has_zeroes(Word block) {
- constexpr unsigned int LOW_BITS = repeat_byte<Word>(0x01);
+ constexpr Word LOW_BITS = repeat_byte<Word>(0x01);
constexpr Word HIGH_BITS = repeat_byte<Word>(0x80);
Word subtracted = block - LOW_BITS;
Word inverted = ~block;
More information about the libc-commits
mailing list