[libcxx-commits] [libcxx] [libc++] Fix the long/short flag position of basic_string for a narrow size_type (PR #236867)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Oct 29 20:57:05 PDT 2026


https://github.com/Himadhith updated https://github.com/llvm/llvm-project/pull/236867

>From 13c0bc3cf9b4ce54ada19186c74982cc26934d26 Mon Sep 17 00:00:00 2001
From: himadhith <himadhith.v at ibm.com>
Date: Thu, 29 Oct 2026 23:08:24 +0530
Subject: [PATCH 1/2] [libc++] Fix the long/short flag position of basic_string
 for a narrow size_type

---
 libcxx/include/string | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/string b/libcxx/include/string
index 33175c115fd10c..12438d29968851 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -797,12 +797,19 @@ private:
     __long() = default;
 
     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __long(__alloc_result __alloc, size_type __size)
-        : __data_(__alloc.ptr), __size_(__size), __cap_(__alloc.count / __endian_factor), __is_long_(true) {
+        : __data_(__alloc.ptr),
+          __size_(__size),
+          __padding_(),
+          __cap_(__alloc.count / __endian_factor),
+          __is_long_(true) {
       _LIBCPP_ASSERT_INTERNAL(!__fits_in_sso(__alloc.count), "Long capacity should always be larger than the SSO");
     }
 
     pointer __data_;
     size_type __size_;
+    _LIBCPP_NO_UNIQUE_ADDRESS
+    __padding<((2 * sizeof(size_type) + alignof(pointer) - 1) & ~(alignof(pointer) - 1)) - 2 * sizeof(size_type)>
+        __padding_;
     size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
     size_type __is_long_ : 1;
   };

>From b7c0a2aaba3e7c4db9a303df02cfcb737e27d4fd Mon Sep 17 00:00:00 2001
From: himadhith <himadhith.v at ibm.com>
Date: Fri, 30 Oct 2026 09:26:46 +0530
Subject: [PATCH 2/2] use _LIBCPP_ALIGNOF instead of alignof

---
 libcxx/include/string | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/string b/libcxx/include/string
index 12438d29968851..25bf9e158f5d6b 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -808,7 +808,8 @@ private:
     pointer __data_;
     size_type __size_;
     _LIBCPP_NO_UNIQUE_ADDRESS
-    __padding<((2 * sizeof(size_type) + alignof(pointer) - 1) & ~(alignof(pointer) - 1)) - 2 * sizeof(size_type)>
+    __padding<((2 * sizeof(size_type) + _LIBCPP_ALIGNOF(pointer) - 1) & ~(_LIBCPP_ALIGNOF(pointer) - 1)) -
+              2 * sizeof(size_type)>
         __padding_;
     size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
     size_type __is_long_ : 1;



More information about the libcxx-commits mailing list