public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/103501] New: associative containers left invalid after allocator-extended move construction
@ 2021-11-30 16:45 redi at gcc dot gnu.org
  2021-11-30 22:19 ` [Bug libstdc++/103501] " redi at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2021-11-30 16:45 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103501
           Summary: associative containers left invalid after
                    allocator-extended move construction
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

#include <set>
#include <iostream>

template<typename T>
struct Alloc : std::allocator<T>
{
  template<typename U> struct rebind { using other = Alloc<U>; };

  Alloc(int i) : id(i) { }

  template<typename U>
    Alloc(const Alloc<U>& a) : id(a.id) { }

  int id;

  using is_always_equal = std::false_type;

  bool operator==(const Alloc& a) const { return id == a.id; }
  bool operator!=(const Alloc& a) const { return id != a.id; }
};


struct X
{
  int i;

  X(int i) : i(i) { }
  X(const X& x) noexcept : i(x.i) { }
  X(X&& x) noexcept : i(x.i) { x.i = -1; }

  bool operator<(const X& rhs) const { return i < rhs.i; }
};

struct Y
{
  int i;

  Y(int i) : i(i) { }
  Y(const Y& y) noexcept : i(y.i) { }
  Y(Y&& y) noexcept : i(y.i) { y.i = -y.i; }

  bool operator<(const Y& rhs) const { return i < rhs.i; }
};


int main()
{
  std::set<X, std::less<>, Alloc<X>> s1{ {1, 2, 3}, Alloc<X>(1)};
  std::set<X, std::less<>, Alloc<X>> s2{ std::move(s1), Alloc<X>(2) };
  for (auto& x : s1)
    std::cout << x.i << '\n';

  std::cout << '\n';

  std::multiset<Y, std::less<>, Alloc<Y>> m1{ {1, 2, 3}, Alloc<Y>(1)};
  std::multiset<Y, std::less<>, Alloc<Y>> m2{ std::move(m1), Alloc<Y>(2) };
  for (auto& y : m1)
    std::cout << y.i << '\n';
}

The sets are left with broken invariants, printing:

-1
-1
-1

-1
-2
-3

(the std::set has non-unique elements, the std::multiset has elements in
non-increasing order).

The problem is that we move each individual element when the allocators are not
equal, and that can leave non-unique elements behind.

I think we need to clear the moved-from sets in this case.

I haven't checked the unordered containers yet to see if they have the same
issue.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-07-07  9:47 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-30 16:45 [Bug libstdc++/103501] New: associative containers left invalid after allocator-extended move construction redi at gcc dot gnu.org
2021-11-30 22:19 ` [Bug libstdc++/103501] " redi at gcc dot gnu.org
2021-12-01 15:08 ` cvs-commit at gcc dot gnu.org
2021-12-01 17:48 ` redi at gcc dot gnu.org
2022-01-05 22:06 ` cvs-commit at gcc dot gnu.org
2022-06-15  8:14 ` cvs-commit at gcc dot gnu.org
2022-06-15  8:21 ` redi at gcc dot gnu.org
2022-06-28 10:47 ` jakub at gcc dot gnu.org
2022-06-28 12:00 ` redi at gcc dot gnu.org
2023-07-07  9:47 ` rguenth at gcc dot gnu.org

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).