[libc-commits] [libc] adf76c5 - [libc] Fix has_zeroes() function not working with long Word type (#244197)

via libc-commits libc-commits at lists.llvm.org
Fri Nov 27 02:58:53 PST 2026


Author: Hussam Alhassan
Date: 2026-11-27T10:58:48Z
New Revision: adf76c5b16ff5b2bb26b8977fc9f382c4d8cff18

URL: https://github.com/llvm/llvm-project/commit/adf76c5b16ff5b2bb26b8977fc9f382c4d8cff18
DIFF: https://github.com/llvm/llvm-project/commit/adf76c5b16ff5b2bb26b8977fc9f382c4d8cff18.diff

LOG: [libc] Fix has_zeroes() function not working with long Word type (#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.

Added: 
    libc/test/src/string/string_length_test.cpp

Modified: 
    libc/src/string/CMakeLists.txt
    libc/src/string/string_length.h
    libc/test/src/string/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index 4427e59961039d..4ccc5f5da9ed8d 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -28,6 +28,17 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  string_length
+  HDRS
+    string_length.h
+  DEPENDS
+    libc.hdr.limits_macros
+    libc.hdr.stdint_proxy
+    libc.hdr.types.size_t
+    libc.src.__support.CPP.type_traits
+)
+
 add_entrypoint_object(
   memccpy
   SRCS

diff  --git a/libc/src/string/string_length.h b/libc/src/string/string_length.h
index 21376e04b439c9..54bc7943a74545 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;

diff  --git a/libc/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt
index 4421860cc16fcf..a6b4249e370c44 100644
--- a/libc/test/src/string/CMakeLists.txt
+++ b/libc/test/src/string/CMakeLists.txt
@@ -384,6 +384,17 @@ 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_length
+    libc.hdr.stdint_proxy
+)
+
 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..d944bb6dba8258
--- /dev/null
+++ b/libc/test/src/string/string_length_test.cpp
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// \file
+// This file contains unit tests for string_length
+//
+//===----------------------------------------------------------------------===//
+
+#include "hdr/stdint_proxy.h"
+#include "src/string/string_length.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStringLengthTest, HasZeroes64BitWordBlockTrue) {
+  bool result =
+      LIBC_NAMESPACE::internal::word::has_zeroes<uint64_t>(0x0011223344556677);
+  ASSERT_TRUE(result);
+}
+
+TEST(LlvmLibcStringLengthTest, HasZeroes64BitWordFalse) {
+  bool result =
+      LIBC_NAMESPACE::internal::word::has_zeroes<uint64_t>(0x1122334455667788);
+  ASSERT_FALSE(result);
+}
+
+TEST(LlvmLibcStringLengthTest, HasZeroes32BitWordBlockTrue) {
+  bool result =
+      LIBC_NAMESPACE::internal::word::has_zeroes<uint32_t>(0x00112233);
+  ASSERT_TRUE(result);
+}
+
+TEST(LlvmLibcStringLengthTest, HasZeroes32BitWordBlockFalse) {
+  bool result =
+      LIBC_NAMESPACE::internal::word::has_zeroes<uint32_t>(0x11223344);
+  ASSERT_FALSE(result);
+}


        


More information about the libc-commits mailing list