public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r12-6573] libstdc++: Ignore cv-quals when std::allocator<void> constructs
Date: Fri, 14 Jan 2022 10:14:49 +0000 (GMT)	[thread overview]
Message-ID: <20220114101449.8D12D3857C4F@sourceware.org> (raw)

https://gcc.gnu.org/g:fc6f1128ae603164aea6303ce2b3ed0b57e6a378

commit r12-6573-gfc6f1128ae603164aea6303ce2b3ed0b57e6a378
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Jan 13 21:59:13 2022 +0000

    libstdc++: Ignore cv-quals when std::allocator<void> constructs
    
    When I added the std::allocator_traits<std::allocator<void>>
    specialization it broke code like this:
    
      std::allocate_shared<const int>(std::allocator<void>());
    
    The problem is that allocator_traits<allocator<void>>::construct(a, p)
    now uses std::_Construct(p), which only does a static_cast<void*>(p) and
    so fails if the pointer has cv-quals.
    
    This changes std::_Construct (and the related std::_Construct_novalue)
    to use a C-style cast to (void*) which matches the effects of the
    "voidify" helper in the C++20 standard.
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/stl_construct.h (_Construct, _Construct_novalue):
            Also cast away cv-qualifiers when converting pointer to void.
            * testsuite/20_util/allocator/void.cc: Test construct function
            with cv-qualified types.

Diff:
---
 libstdc++-v3/include/bits/stl_construct.h        |  4 ++--
 libstdc++-v3/testsuite/20_util/allocator/void.cc | 15 +++++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/include/bits/stl_construct.h b/libstdc++-v3/include/bits/stl_construct.h
index 7c5fd4c9cf7..9531222809c 100644
--- a/libstdc++-v3/include/bits/stl_construct.h
+++ b/libstdc++-v3/include/bits/stl_construct.h
@@ -116,7 +116,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  return;
 	}
 #endif
-      ::new(static_cast<void*>(__p)) _Tp(std::forward<_Args>(__args)...);
+      ::new((void*)__p) _Tp(std::forward<_Args>(__args)...);
     }
 #else
   template<typename _T1, typename _T2>
@@ -132,7 +132,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _T1>
     inline void
     _Construct_novalue(_T1* __p)
-    { ::new(static_cast<void*>(__p)) _T1; }
+    { ::new((void*)__p) _T1; }
 
   template<typename _ForwardIterator>
     _GLIBCXX20_CONSTEXPR void
diff --git a/libstdc++-v3/testsuite/20_util/allocator/void.cc b/libstdc++-v3/testsuite/20_util/allocator/void.cc
index 52e1fef3700..5cdf0be012c 100644
--- a/libstdc++-v3/testsuite/20_util/allocator/void.cc
+++ b/libstdc++-v3/testsuite/20_util/allocator/void.cc
@@ -87,8 +87,23 @@ static_assert( std::is_same<std::allocator<void>::const_pointer, const void*>(),
     "const_pointer is const void*" );
 #endif // C++20
 
+void
+test02()
+{
+  std::allocator<void> av;
+  int* p = std::allocator<int>().allocate(1);
+  const int* c = p;
+  std::allocator_traits<std::allocator<void>>::construct(av, c, 0);
+  volatile int* v = p;
+  std::allocator_traits<std::allocator<void>>::construct(av, v, 0);
+  const volatile int* cv = p;
+  std::allocator_traits<std::allocator<void>>::construct(av, cv, 0);
+  std::allocator<int>().deallocate(p, 1);
+}
+
 int
 main()
 {
   test01();
+  test02();
 }


                 reply	other threads:[~2022-01-14 10:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220114101449.8D12D3857C4F@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).