[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 01:40:39 PDT 2024


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

>From 312cb4e31473fca528f9ad797aa52b6576e39388 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 1/2] [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 cb61492e21846a8590db15f8bd1f4e8587a44c06 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 2/2] [libc++] Remove potential 0-sized array in
 __compressed_pair_padding

---
 libcxx/include/CMakeLists.txt             |  1 +
 libcxx/include/__memory/compressed_pair.h | 10 +++----
 libcxx/include/__memory/padding.h         | 32 +++++++++++++++++++++++
 libcxx/include/string                     | 13 +++------
 4 files changed, 41 insertions(+), 15 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 629e3ad8848ffa..18a1a3a07edc58 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>
@@ -53,11 +54,10 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 #ifndef _LIBCPP_ABI_NO_COMPRESSED_PAIR_PADDING
 
 template <class _ToPad>
-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>];
-};
+using __compressed_pair_padding =
+    std::__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;                       \
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..192d2bf1e0b8c9 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:
@@ -861,7 +854,7 @@ private:
 
   struct __short {
     value_type __data_[__min_cap];
-    _LIBCPP_NO_UNIQUE_ADDRESS __padding<sizeof(value_type) - 1> __padding_;
+    _LIBCPP_NO_UNIQUE_ADDRESS std::__padding<sizeof(value_type) - 1> __padding_;
     unsigned char __size_    : 7;
     unsigned char __is_long_ : 1;
   };
@@ -913,7 +906,7 @@ private:
       unsigned char __is_long_ : 1;
       unsigned char __size_    : 7;
     };
-    _LIBCPP_NO_UNIQUE_ADDRESS __padding<sizeof(value_type) - 1> __padding_;
+    _LIBCPP_NO_UNIQUE_ADDRESS std::__padding<sizeof(value_type) - 1> __padding_;
     value_type __data_[__min_cap];
   };
 



More information about the libcxx-commits mailing list