public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "kamkaz at windowslive dot com" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-110952-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110952

            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 imposed on
`std::allocator_traits<Allocator>::pointer` type.

In the implementation of these containers, this pointer type (which might be a
fancy_pointer) is required to be implicitly convertible from and into native
pointers, which in this case are equivalent to
`std::pointer_traits<pointer>::element_type *`.

This bug is present in all the GCC versions I managed to test, from 6.2 until
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<pointer>::to_address(__ptr)`)
- std::pointer_traits<pointer>::pointer_to(*__ptr) to get back the potentially
"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()
      { 
        auto __ptr = _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 = 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_NOEXCEPT
      { _Node_alloc_traits::deallocate(_M_impl, __p, 1); }

Fixed:
      typename _Node_alloc_traits::value_type*
      _M_get_node()
      {
        auto __ptr = _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 = 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 representation
of the nodes, just handling and requirements imposed on the allocator pointer.

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 that
is required for Allocator::pointer. However, since these functionalities are
not used by the mentioned containers, it doesn't matter here).

             reply	other threads:[~2023-08-08 21:58 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-08 21:58 kamkaz at windowslive dot com [this message]
2023-08-09 12:42 ` [Bug libstdc++/110952] " redi at gcc dot gnu.org

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=bug-110952-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).