[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 26 01:54:02 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/9] [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/9] 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);
+}

>From 13f148b9988ebdfa13f4beae9b51828d77167725 Mon Sep 17 00:00:00 2001
From: Hussam Alhassan <hussam.alhassan at arm.com>
Date: Thu, 19 Nov 2026 15:04:02 +0000
Subject: [PATCH 3/9] Fix test formatting

---
 libc/test/src/string/string_length_test.cpp | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/libc/test/src/string/string_length_test.cpp b/libc/test/src/string/string_length_test.cpp
index 0c0fa664f6e171..a5738f66aa3d41 100644
--- a/libc/test/src/string/string_length_test.cpp
+++ b/libc/test/src/string/string_length_test.cpp
@@ -1,4 +1,5 @@
-//===-- Unittests for string_length ----------------------------------- ----===//
+//===-- 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.
@@ -6,11 +7,12 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/string/string_length.h"
 #include "hdr/stdint_proxy.h"
+#include "src/string/string_length.h"
 #include "test/UnitTest/Test.h"
 
 TEST(LlvmLibcStringLengthTest, Size64BitsHasZeroes) {
-  bool result = LIBC_NAMESPACE::internal::word::has_zeroes<uint64_t>(0x0011223344556677);
+  bool result =
+      LIBC_NAMESPACE::internal::word::has_zeroes<uint64_t>(0x0011223344556677);
   ASSERT_TRUE(result);
 }

>From fbecf533ffe9aa8ea9367935e043b3f5177add05 Mon Sep 17 00:00:00 2001
From: Hussam Alhassan <hussam.alhassan at arm.com>
Date: Wed, 25 Nov 2026 15:37:12 +0000
Subject: [PATCH 4/9] Fix header for string_length_test.cpp file

---
 libc/test/src/string/string_length_test.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libc/test/src/string/string_length_test.cpp b/libc/test/src/string/string_length_test.cpp
index a5738f66aa3d41..6e30e5c5144267 100644
--- a/libc/test/src/string/string_length_test.cpp
+++ b/libc/test/src/string/string_length_test.cpp
@@ -1,5 +1,4 @@
-//===-- Unittests for string_length -----------------------------------
-//----===//
+//===-- 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.

>From 3e6481003536fefd9f7a2888b43122ba385cbaed Mon Sep 17 00:00:00 2001
From: Hussam Alhassan <hussam.alhassan at arm.com>
Date: Wed, 25 Nov 2026 15:42:41 +0000
Subject: [PATCH 5/9] Add more tests for the has_zeroes function

---
 libc/test/src/string/string_length_test.cpp | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/libc/test/src/string/string_length_test.cpp b/libc/test/src/string/string_length_test.cpp
index 6e30e5c5144267..2b888afd08cf2f 100644
--- a/libc/test/src/string/string_length_test.cpp
+++ b/libc/test/src/string/string_length_test.cpp
@@ -10,8 +10,26 @@
 #include "src/string/string_length.h"
 #include "test/UnitTest/Test.h"
 
-TEST(LlvmLibcStringLengthTest, Size64BitsHasZeroes) {
+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);
+}

>From 4f418329d611178cda7c70d433df53c4942b2aa7 Mon Sep 17 00:00:00 2001
From: Hussam Alhassan <hussam.alhassan at arm.com>
Date: Wed, 25 Nov 2026 15:56:49 +0000
Subject: [PATCH 6/9] Update relevant CMakeList files with new entry for the
 string_length.h file

---
 libc/src/string/CMakeLists.txt      | 11 +++++++++++
 libc/test/src/string/CMakeLists.txt |  2 +-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index dad6ad21da15b6..29c0650ea07acb 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/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt
index 318f8733dbb950..28a8cfd08cd796 100644
--- a/libc/test/src/string/CMakeLists.txt
+++ b/libc/test/src/string/CMakeLists.txt
@@ -391,7 +391,7 @@ add_libc_test(
   SRCS
     string_length_test.cpp
   DEPENDS
-    libc.src.string.string_utils
+    libc.src.string.string_length
 )
 
 add_libc_multi_impl_test(memcmp libc-string-tests SRCS memcmp_test.cpp)

>From cbc91c9296f11dedb8cb4eb21fb48eb68daf6291 Mon Sep 17 00:00:00 2001
From: Hussam Alhassan <hussam.alhassan at arm.com>
Date: Wed, 25 Nov 2026 15:57:19 +0000
Subject: [PATCH 7/9] Updated header for string_length_test to conform to the
 latest header standard

---
 libc/test/src/string/string_length_test.cpp | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/libc/test/src/string/string_length_test.cpp b/libc/test/src/string/string_length_test.cpp
index 2b888afd08cf2f..b679272efb9756 100644
--- a/libc/test/src/string/string_length_test.cpp
+++ b/libc/test/src/string/string_length_test.cpp
@@ -1,10 +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
 //
 //===----------------------------------------------------------------------===//
+//
+// string_length_test.cpp
+// This file contains unit tests for string_length
+// 
+//===----------------------------------------------------------------------===//
+
 
 #include "hdr/stdint_proxy.h"
 #include "src/string/string_length.h"

>From 6dadba6b669fd548398e8454dc983ff2e1c1b5a6 Mon Sep 17 00:00:00 2001
From: Hussam Alhassan <hussam.alhassan at arm.com>
Date: Wed, 25 Nov 2026 16:11:00 +0000
Subject: [PATCH 8/9] Improve header for string_length_test

---
 libc/test/src/string/string_length_test.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libc/test/src/string/string_length_test.cpp b/libc/test/src/string/string_length_test.cpp
index b679272efb9756..c89329d7411efa 100644
--- a/libc/test/src/string/string_length_test.cpp
+++ b/libc/test/src/string/string_length_test.cpp
@@ -1,4 +1,4 @@
-//===--  ---------------------------------------===//
+//===-- String Length Test --------------------------------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,7 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// string_length_test.cpp
 // This file contains unit tests for string_length
 // 
 //===----------------------------------------------------------------------===//

>From 2413904ec1adf0a3225887283327b0a0e99f64b1 Mon Sep 17 00:00:00 2001
From: Hussam Alhassan <hussam.alhassan at arm.com>
Date: Thu, 26 Nov 2026 09:53:43 +0000
Subject: [PATCH 9/9] Fix file header for string_length__test

---
 libc/test/src/string/string_length_test.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/test/src/string/string_length_test.cpp b/libc/test/src/string/string_length_test.cpp
index c89329d7411efa..26df56becfdb08 100644
--- a/libc/test/src/string/string_length_test.cpp
+++ b/libc/test/src/string/string_length_test.cpp
@@ -1,4 +1,4 @@
-//===-- String Length Test --------------------------------------*- C++ -*-===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 //
+// \file
 // This file contains unit tests for string_length
 // 
 //===----------------------------------------------------------------------===//



More information about the libc-commits mailing list