public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.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 [thread overview] Message-ID: <bug-103501-4@http.gcc.gnu.org/bugzilla/> (raw) 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.
next reply other threads:[~2021-11-30 16:45 UTC|newest] Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-11-30 16:45 redi at gcc dot gnu.org [this message] 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
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-103501-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: linkBe 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).