From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id DEC4F3858D28; Tue, 30 Nov 2021 16:45:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DEC4F3858D28 From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/103501] New: associative containers left invalid after allocator-extended move construction Date: Tue, 30 Nov 2021 16:45:00 +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: 12.0 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org 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 keywords 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 30 Nov 2021 16:45:01 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D103501 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 #include template struct Alloc : std::allocator { template struct rebind { using other =3D Alloc; }; Alloc(int i) : id(i) { } template Alloc(const Alloc& a) : id(a.id) { } int id; using is_always_equal =3D std::false_type; bool operator=3D=3D(const Alloc& a) const { return id =3D=3D a.id; } bool operator!=3D(const Alloc& a) const { return id !=3D 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 =3D -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 =3D -y.i; } bool operator<(const Y& rhs) const { return i < rhs.i; } }; int main() { std::set, Alloc> s1{ {1, 2, 3}, Alloc(1)}; std::set, Alloc> s2{ std::move(s1), Alloc(2) }; for (auto& x : s1) std::cout << x.i << '\n'; std::cout << '\n'; std::multiset, Alloc> m1{ {1, 2, 3}, Alloc(1)}; std::multiset, Alloc> m2{ std::move(m1), Alloc(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.=