You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is an easy risk of creating use-after-free errors with current implementation of iterators. This simple code:
auto it = map.find(key);
constauto &data = it->second;
The problem comes from iterator::operator-> returning temporary (proxy), where second is a value, and not reference. This means that as soon as the assignment to data ends, temporary is freed and our reference points to destroyed object. This will be also true for directly doing map.find(key)->second.
This problem is dangerous, because it doesn't generate any warning even with decently aggressive flags (on clang-15, -Wall -Wextra -Wpedantic). Moreover, same code for std::map works correctly.
I have no idea if this can be solved on the code level (i.e. if there is a value that could be referenced in that proxy instead of making a copy), or if only solution is just making sure documentation warns about this.
The text was updated successfully, but these errors were encountered:
There is an easy risk of creating use-after-free errors with current implementation of iterators. This simple code:
The problem comes from
iterator::operator->
returning temporary (proxy
), wheresecond
is a value, and not reference. This means that as soon as the assignment todata
ends, temporary is freed and our reference points to destroyed object. This will be also true for directly doingmap.find(key)->second
.A full example on godbolt with enabled address sanitizer: https://godbolt.org/z/M9h43Pdd1
This problem is dangerous, because it doesn't generate any warning even with decently aggressive flags (on clang-15,
-Wall -Wextra -Wpedantic
). Moreover, same code forstd::map
works correctly.I have no idea if this can be solved on the code level (i.e. if there is a value that could be referenced in that proxy instead of making a copy), or if only solution is just making sure documentation warns about this.
The text was updated successfully, but these errors were encountered: