[libcxx-commits] [libcxx] [libc++] Remove potential 0-sized array in __compressed_pair_padding (PR #109028)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 30 00:03:56 PDT 2024


https://github.com/serge-sans-paille updated https://github.com/llvm/llvm-project/pull/109028

>From 1df4ef53007dd69e859cb3b9412a60ed3337c708 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Tue, 17 Sep 2024 20:39:05 +0200
Subject: [PATCH 1/4] [libc++] Remove potential 0-sized array in
 __compressed_pair_padding

---
 libcxx/include/__memory/compressed_pair.h | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/libcxx/include/__memory/compressed_pair.h b/libcxx/include/__memory/compressed_pair.h
index 629e3ad8848ffa..77928af434f1f4 100644
--- a/libcxx/include/__memory/compressed_pair.h
+++ b/libcxx/include/__memory/compressed_pair.h
@@ -52,13 +52,16 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #ifndef _LIBCPP_ABI_NO_COMPRESSED_PAIR_PADDING
 
-template <class _ToPad>
+template <class _ToPad,
+          bool _Empty = ((is_empty<_ToPad>::value && !__libcpp_is_final<_ToPad>::value) ||
+                         is_reference<_ToPad>::value || sizeof(_ToPad) - __datasizeof_v<_ToPad> == 0)>
 class __compressed_pair_padding {
-  char __padding_[((is_empty<_ToPad>::value && !__libcpp_is_final<_ToPad>::value) || is_reference<_ToPad>::value)
-                      ? 0
-                      : sizeof(_ToPad) - __datasizeof_v<_ToPad>];
+  char __padding_[sizeof(_ToPad) - __datasizeof_v<_ToPad>];
 };
 
+template <class _ToPad>
+class __compressed_pair_padding<_ToPad, true> {};
+
 #  define _LIBCPP_COMPRESSED_PAIR(T1, Initializer1, T2, Initializer2)                                                  \
     _LIBCPP_NO_UNIQUE_ADDRESS __attribute__((__aligned__(_LIBCPP_ALIGNOF(T2)))) T1 Initializer1;                       \
     _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _);          \

>From 55765d09969455128446310a1042c14bde95e227 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Wed, 18 Sep 2024 11:25:18 +0200
Subject: [PATCH 2/4] [libc++] Remove potential 0-sized array in
 __packed_format_arg_store

---
 libcxx/include/__format/format_arg_store.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/libcxx/include/__format/format_arg_store.h b/libcxx/include/__format/format_arg_store.h
index 68e936acecade7..9a4a41df6d4e68 100644
--- a/libcxx/include/__format/format_arg_store.h
+++ b/libcxx/include/__format/format_arg_store.h
@@ -234,6 +234,11 @@ struct __packed_format_arg_store {
   uint64_t __types_ = 0;
 };
 
+template <class _Context>
+struct __packed_format_arg_store<_Context, 0> {
+  uint64_t __types_ = 0;
+};
+
 template <class _Context, size_t _Np>
 struct __unpacked_format_arg_store {
   basic_format_arg<_Context> __args_[_Np];

>From 1112cfd95b9840fcf44f04cb73aedcf480146e4f Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Wed, 18 Sep 2024 16:51:50 +0200
Subject: [PATCH 3/4] fixup! [libc++] Remove potential 0-sized array in
 __compressed_pair_padding

---
 libcxx/include/__memory/compressed_pair.h | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/libcxx/include/__memory/compressed_pair.h b/libcxx/include/__memory/compressed_pair.h
index 77928af434f1f4..d955aebad63e31 100644
--- a/libcxx/include/__memory/compressed_pair.h
+++ b/libcxx/include/__memory/compressed_pair.h
@@ -52,15 +52,11 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #ifndef _LIBCPP_ABI_NO_COMPRESSED_PAIR_PADDING
 
-template <class _ToPad,
-          bool _Empty = ((is_empty<_ToPad>::value && !__libcpp_is_final<_ToPad>::value) ||
-                         is_reference<_ToPad>::value || sizeof(_ToPad) - __datasizeof_v<_ToPad> == 0)>
-class __compressed_pair_padding {
-  char __padding_[sizeof(_ToPad) - __datasizeof_v<_ToPad>];
-};
-
 template <class _ToPad>
-class __compressed_pair_padding<_ToPad, true> {};
+using __compressed_pair_padding =
+    __padding<((is_empty<_ToPad>::value && !__libcpp_is_final<_ToPad>::value) || is_reference<_ToPad>::value)
+                  ? 0
+                  : (sizeof(_ToPad) - __datasizeof_v<_ToPad>)>;
 
 #  define _LIBCPP_COMPRESSED_PAIR(T1, Initializer1, T2, Initializer2)                                                  \
     _LIBCPP_NO_UNIQUE_ADDRESS __attribute__((__aligned__(_LIBCPP_ALIGNOF(T2)))) T1 Initializer1;                       \

>From 2269e8de5ff77462696284ca3d9572a056956901 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Mon, 30 Sep 2024 09:03:21 +0200
Subject: [PATCH 4/4] fixup! fixup! [libc++] Remove potential 0-sized array in
 __compressed_pair_padding

---
 libcxx/include/CMakeLists.txt             |  1 +
 libcxx/include/__memory/compressed_pair.h |  1 +
 libcxx/include/__memory/padding.h         | 32 +++++++++++++++++++++++
 libcxx/include/string                     |  9 +------
 4 files changed, 35 insertions(+), 8 deletions(-)
 create mode 100644 libcxx/include/__memory/padding.h

diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 8c61009167ddce..d1f4e2acc705db 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -546,6 +546,7 @@ set(files
   __memory/inout_ptr.h
   __memory/noexcept_move_assign_container.h
   __memory/out_ptr.h
+  __memory/padding.h
   __memory/pointer_traits.h
   __memory/ranges_construct_at.h
   __memory/ranges_uninitialized_algorithms.h
diff --git a/libcxx/include/__memory/compressed_pair.h b/libcxx/include/__memory/compressed_pair.h
index d955aebad63e31..6717da0208e1c2 100644
--- a/libcxx/include/__memory/compressed_pair.h
+++ b/libcxx/include/__memory/compressed_pair.h
@@ -11,6 +11,7 @@
 #define _LIBCPP___MEMORY_COMPRESSED_PAIR_H
 
 #include <__config>
+#include <__memory/padding.h>
 #include <__type_traits/datasizeof.h>
 #include <__type_traits/is_empty.h>
 #include <__type_traits/is_final.h>
diff --git a/libcxx/include/__memory/padding.h b/libcxx/include/__memory/padding.h
new file mode 100644
index 00000000000000..081bb4c6c7c281
--- /dev/null
+++ b/libcxx/include/__memory/padding.h
@@ -0,0 +1,32 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___MEMORY_PADDING_H
+#define _LIBCPP___MEMORY_PADDING_H
+
+#include <__config>
+#include <cstddef>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <size_t _PaddingSize>
+struct __padding {
+  char __padding_[_PaddingSize];
+};
+
+template <>
+struct __padding<0> {};
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___MEMORY_PADDING_H
diff --git a/libcxx/include/string b/libcxx/include/string
index fdb189016bfbac..781af5f1f27f48 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -610,6 +610,7 @@ basic_string<char32_t> operator""s( const char32_t *str, size_t len );
 #include <__memory/compressed_pair.h>
 #include <__memory/construct_at.h>
 #include <__memory/noexcept_move_assign_container.h>
+#include <__memory/padding.h>
 #include <__memory/pointer_traits.h>
 #include <__memory/swap_allocator.h>
 #include <__memory_resource/polymorphic_allocator.h>
@@ -749,14 +750,6 @@ struct __can_be_converted_to_string_view
 struct __uninitialized_size_tag {};
 struct __init_with_sentinel_tag {};
 
-template <size_t _PaddingSize>
-struct __padding {
-  char __padding_[_PaddingSize];
-};
-
-template <>
-struct __padding<0> {};
-
 template <class _CharT, class _Traits, class _Allocator>
 class basic_string {
 private:



More information about the libcxx-commits mailing list