Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix][Support] Fix copy constructor for support::OrderedSet #17044

Merged
merged 1 commit into from
May 30, 2024

Conversation

Lunderberg
Copy link
Contributor

Prior to this commit, the support::OrderedSet<T> utility used the default copy constructor and copy assignment, which would copy both the OrderedSet::elements_ and OrderedSet::elem_to_iter_ members. While this is the correct behavior for elements_, the copy of elem_to_iter_ would contain references to the original's element_, rather than to its own.

While elem_to_iter_ is used in both OrderedSet::push_back and OrderedSet::erase, the implementation of OrderedSet::push_back only depends on the keys used in elem_to_iter_, and does not depend on the values stored. As a result, this bug could go undetected for append-only usage, which is the most frequent use of OrderedSet.

This commit updates support::OrderedSet to have an explicit copy constructor and copy assignment. Only the std::list<T> elements_ member may be copied, while the elem_to_iter_ must instead be rebuilt.

Prior to this commit, the `support::OrderedSet<T>` utility used the
default copy constructor and copy assignment, which would copy both
the `OrderedSet::elements_` and `OrderedSet::elem_to_iter_` members.
While this is the correct behavior for `elements_`, the copy of
`elem_to_iter_` would contain references to the original's `element_`,
rather than to its own.

While `elem_to_iter_` is used in both `OrderedSet::push_back` and
`OrderedSet::erase`, the implementation of `OrderedSet::push_back`
only depends on the keys used in `elem_to_iter_`, and does not depend
on the values stored.  As a result, this bug could go undetected for
append-only usage, which is the most frequent use of `OrderedSet`.

This commit updates `support::OrderedSet` to have an explicit copy
constructor and copy assignment.  Only the `std::list<T> elements_`
member may be copied, while the `elem_to_iter_` must instead be
rebuilt.
@Lunderberg Lunderberg requested a review from vinx13 May 29, 2024 16:28
@Lunderberg
Copy link
Contributor Author

While I'm unaware of any way to trigger this bug from an externally-visible API, I ran across this during implementation of a debug feature. This bug can result in segfaults when accessing elements of a support::OrderedSet.

@tqchen tqchen merged commit f6aab98 into apache:main May 30, 2024
19 checks passed
@Lunderberg Lunderberg deleted the bugfix_ordered_set_copy_constructor branch May 30, 2024 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants