Closed Lunderberg closed 1 month ago
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
.
Prior to this commit, the
support::OrderedSet<T>
utility used the default copy constructor and copy assignment, which would copy both theOrderedSet::elements_
andOrderedSet::elem_to_iter_
members. While this is the correct behavior forelements_
, the copy ofelem_to_iter_
would contain references to the original'selement_
, rather than to its own.While
elem_to_iter_
is used in bothOrderedSet::push_back
andOrderedSet::erase
, the implementation ofOrderedSet::push_back
only depends on the keys used inelem_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 ofOrderedSet
.This commit updates
support::OrderedSet
to have an explicit copy constructor and copy assignment. Only thestd::list<T> elements_
member may be copied, while theelem_to_iter_
must instead be rebuilt.