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

AddressSanitizer: container-overflow false positive #1752

Open
tttapa opened this issue Apr 30, 2024 · 0 comments
Open

AddressSanitizer: container-overflow false positive #1752

tttapa opened this issue Apr 30, 2024 · 0 comments

Comments

@tttapa
Copy link

tttapa commented Apr 30, 2024

The following code using a polymorphic memory resource causes a false positive for the container-overflow check in the address sanitizer:

https://godbolt.org/z/9q596WG1d

#include <array>
#include <memory_resource>
#include <numeric>
#include <vector>

int main() {
    std::array<unsigned char, 1024> buffer;
    std::pmr::monotonic_buffer_resource mbr {buffer.data(), buffer.size()};
    std::pmr::vector<int> vec {&mbr};
    vec.reserve(16);
    return std::accumulate(buffer.begin(), buffer.end(), int {});
}
clang++-18 -fsanitize=address -O0 -stdlib=libc++

The code uses an array as a buffer, and allocates a vector into that buffer. Then it accesses all bytes of the buffer.
AFAICT, accessing the bytes of the buffer is not UB, but the reason why this fails is that there also happens to be a reserved vector in that storage, and Asan thinks we're accessing beyond the initialized memory of the vector.

This may be challenging to work around, though. I guess Asan just marks those bytes as belonging to the vector, and it may be hard to tell whether the bytes are accessed directly through the vector's iterators, or through other (possibly valid) means.

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