[libcxx-commits] [libcxx] [libc++][string] Remove potential non-trailing 0-length array (PR #105865)
via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Aug 23 11:07:31 PDT 2024
https://github.com/serge-sans-paille updated https://github.com/llvm/llvm-project/pull/105865
>From 8e06f53d3ed04d87dc932ce70c2730e098110a7d Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Fri, 23 Aug 2024 19:52:56 +0200
Subject: [PATCH] [libc++][string] Remove potential non-trailing 0-length array
It is a violation of the standard to use 0 length arrays, especially
when not at the end of a structure (not a FAM GNU extension). Compiler
generally accept it, but it's probably better to have a conforming
implementation.
---
libcxx/include/string | 52 ++++++++++++++++++++++++++++++-------------
1 file changed, 36 insertions(+), 16 deletions(-)
diff --git a/libcxx/include/string b/libcxx/include/string
index 6e93a6230cc2c0..251b8632bc1df1 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -748,6 +748,41 @@ struct __can_be_converted_to_string_view
struct __uninitialized_size_tag {};
struct __init_with_sentinel_tag {};
+#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
+template <class _T, size_t __min_cap, size_t _Padding = sizeof(_T) - 1>
+struct __short_impl {
+ _T __data_[__min_cap];
+ unsigned char __padding_[_Padding];
+ unsigned char __size_ : 7;
+ unsigned char __is_long_ : 1;
+};
+
+template <class _T, size_t __min_cap>
+struct __short_impl<_T, __min_cap, 0> {
+ value_type __data_[__min_cap];
+ unsigned char __size_ : 7;
+ unsigned char __is_long_ : 1;
+};
+#else
+template <class _T, size_t __min_cap, size_t _Padding = sizeof(_T) - 1>
+struct __short_impl {
+ struct _LIBCPP_PACKED {
+ unsigned char __is_long_ : 1;
+ unsigned char __size_ : 7;
+ };
+ char __padding_[_Padding];
+ _T __data_[__min_cap];
+};
+template <class _T, size_t __min_cap>
+struct __short_impl<_T, __min_cap, 0> {
+ struct _LIBCPP_PACKED {
+ unsigned char __is_long_ : 1;
+ unsigned char __size_ : 7;
+ };
+ _T __data_[__min_cap];
+};
+#endif
+
template <class _CharT, class _Traits, class _Allocator>
class basic_string {
private:
@@ -850,13 +885,6 @@ private:
enum { __min_cap = (sizeof(__long) - 1) / sizeof(value_type) > 2 ? (sizeof(__long) - 1) / sizeof(value_type) : 2 };
- struct __short {
- value_type __data_[__min_cap];
- unsigned char __padding_[sizeof(value_type) - 1];
- unsigned char __size_ : 7;
- unsigned char __is_long_ : 1;
- };
-
// The __endian_factor is required because the field we use to store the size
// has one fewer bit than it would if it were not a bitfield.
//
@@ -899,17 +927,9 @@ private:
enum { __min_cap = (sizeof(__long) - 1) / sizeof(value_type) > 2 ? (sizeof(__long) - 1) / sizeof(value_type) : 2 };
- struct __short {
- struct _LIBCPP_PACKED {
- unsigned char __is_long_ : 1;
- unsigned char __size_ : 7;
- };
- char __padding_[sizeof(value_type) - 1];
- value_type __data_[__min_cap];
- };
-
#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
+ using __short = __short_impl<value_type, __min_cap>;
static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
union __rep {
More information about the libcxx-commits
mailing list