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

Potential footgun in ptr_map #37

Open
Noxitu opened this issue Mar 6, 2023 · 0 comments
Open

Potential footgun in ptr_map #37

Noxitu opened this issue Mar 6, 2023 · 0 comments

Comments

@Noxitu
Copy link

Noxitu commented Mar 6, 2023

There is an easy risk of creating use-after-free errors with current implementation of iterators. This simple code:

auto it = map.find(key);
const auto &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.

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 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.

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

No branches or pull requests

1 participant