From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id A94923858D33; Tue, 8 Aug 2023 21:58:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A94923858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691531918; bh=kFw9HjNFqL52xH3pC47oEnPzi4jiBEgKQZfsBqqiZso=; h=From:To:Subject:Date:From; b=mHLJs4+Xm8wDAkW0cOETeWgtw1LdUrsVCgpfCKGzTA0JsJgOlcCJVaI2sSc9hzZbr jaDeQ56H9mZFFptMdWSOY4pZ3uDmXsTX8nhKb8SLyXHBnLyaJdLtT1NbmS/wk/jtwO 00LIhpsSAe400+cWOFQD1mZXRCbTQVgAn3tPJwyM= From: "kamkaz at windowslive dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/110952] New: Allocator::pointer is required to be implicitly convertible from and into a native pointer Date: Tue, 08 Aug 2023 21:58:38 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 11.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: kamkaz at windowslive dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D110952 Bug ID: 110952 Summary: Allocator::pointer is required to be implicitly convertible from and into a native pointer Product: gcc Version: 11.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: kamkaz at windowslive dot com Target Milestone: --- For std::list and containers based on _Rb_tree (std::(multi_)set, std::(multi_)map) there are some non-standard requirements currently impose= d on `std::allocator_traits::pointer` type. In the implementation of these containers, this pointer type (which might b= e a fancy_pointer) is required to be implicitly convertible from and into native pointers, which in this case are equivalent to `std::pointer_traits::element_type *`. This bug is present in all the GCC versions I managed to test, from 6.2 unt= il 13.2. The proper way to convert from/into these custom pointer types is to use: - std::__to_address(__ptr) to obtain the native pointer (which either calls `__ptr.operator->()` or `std::pointer_traits::to_address(__ptr)`) - std::pointer_traits::pointer_to(*__ptr) to get back the potentia= lly "fancy" pointer. This proper way of handling allocator pointer is already implemented in std::forward_list. To fix this bug, the following changes must be made: In bits/stl_tree.h: Current: protected: _Link_type _M_get_node() { return _Alloc_traits::allocate(_M_get_Node_allocator(), 1); } void _M_put_node(_Link_type __p) _GLIBCXX_NOEXCEPT { _Alloc_traits::deallocate(_M_get_Node_allocator(), __p, 1); } Fixed: protected: _Link_type _M_get_node() {=20 auto __ptr =3D _Alloc_traits::allocate(_M_get_Node_allocator(), 1); return std::__to_address(__ptr); } void _M_put_node(_Link_type __p) _GLIBCXX_NOEXCEPT { typedef typename _Alloc_traits::pointer _Ptr; auto __ptr =3D std::pointer_traits<_Ptr>::pointer_to(*__p); _Alloc_traits::deallocate(_M_get_Node_allocator(), __ptr, 1); } In bits/stl_list.h: Current: typename _Node_alloc_traits::pointer _M_get_node() { return _Node_alloc_traits::allocate(_M_impl, 1); } void _M_put_node(typename _Node_alloc_traits::pointer __p) _GLIBCXX_NOEXCE= PT { _Node_alloc_traits::deallocate(_M_impl, __p, 1); } Fixed: typename _Node_alloc_traits::value_type* _M_get_node() { auto __ptr =3D _Node_alloc_traits::allocate(_M_impl, 1); return std::__to_address(__ptr); } void _M_put_node(typename _Node_alloc_traits::value_type* __p) _GLIBCXX_NOEXCEPT { typedef typename _Node_alloc_traits::pointer _Ptr; auto __ptr =3D std::pointer_traits<_Ptr>::pointer_to(*__p); _Node_alloc_traits::deallocate(_M_impl, __ptr, 1); } This fix does not goes along with the coding style (81 characters in a line= ), it might require some extra typedefs. It is NOT a duplicate of Bug 57272 - it's not about the internal representa= tion of the nodes, just handling and requirements imposed on the allocator point= er. There are no ABI issues here that I can think of. There is a minuscule possibility it might be a breaking change for someone = - if their Fancy Pointer's implicit conversions behaved differently than its `pointer_to` and `.operator->()` (or if they didn't provide them and relied= on implicit conversions, which are not part of the standard). Here there is a small example reproducing the issue: https://godbolt.org/z/fnno3jGYs Note, that if implicit construction from `T*` and `operator T*()` are added= to the fancy pointer type, the example compiles. (Yes, ppointer there doesn't meet the requirement of RandomAccessIterator t= hat is required for Allocator::pointer. However, since these functionalities are not used by the mentioned containers, it doesn't matter here).=