[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 06:52:42 PST 2026
https://github.com/hussam-alhassan updated https://github.com/llvm/llvm-project/pull/244197
>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 1/2] [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;
>From d4dd135344bee0829d310ff30a0e05d89eb469bf Mon Sep 17 00:00:00 2001
From: Hussam Alhassan <hussam.alhassan at arm.com>
Date: Thu, 19 Nov 2026 14:52:11 +0000
Subject: [PATCH 2/2] Add test for has_zeroes with a 64-bit Word type
---
libc/test/src/string/CMakeLists.txt | 10 ++++++++++
libc/test/src/string/string_length_test.cpp | 16 ++++++++++++++++
2 files changed, 26 insertions(+)
create mode 100644 libc/test/src/string/string_length_test.cpp
diff --git a/libc/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt
index 4421860cc16fcf..318f8733dbb950 100644
--- a/libc/test/src/string/CMakeLists.txt
+++ b/libc/test/src/string/CMakeLists.txt
@@ -384,6 +384,16 @@ add_libc_test(
libc.src.string.memset_explicit
)
+add_libc_test(
+ string_length_test
+ SUITE
+ libc-string-tests
+ SRCS
+ string_length_test.cpp
+ DEPENDS
+ libc.src.string.string_utils
+)
+
add_libc_multi_impl_test(memcmp libc-string-tests SRCS memcmp_test.cpp)
add_libc_multi_impl_test(memcpy libc-string-tests SRCS memcpy_test.cpp)
add_libc_multi_impl_test(memmove libc-string-tests SRCS memmove_test.cpp)
diff --git a/libc/test/src/string/string_length_test.cpp b/libc/test/src/string/string_length_test.cpp
new file mode 100644
index 00000000000000..0c0fa664f6e171
--- /dev/null
+++ b/libc/test/src/string/string_length_test.cpp
@@ -0,0 +1,16 @@
+//===-- Unittests for string_length ----------------------------------- ----===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/string/string_length.h"
+#include "hdr/stdint_proxy.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStringLengthTest, Size64BitsHasZeroes) {
+ bool result = LIBC_NAMESPACE::internal::word::has_zeroes<uint64_t>(0x0011223344556677);
+ ASSERT_TRUE(result);
+}
More information about the libc-commits
mailing list