Skip to content

ASIO_ENABLE_BUFFER_DEBUGGING dereferences invalidated iterator #1751

Description

@arvidn

When ASIO_ENABLE_BUFFER_DEBUGGING is enabled, buffer captures a std::function for validation.

here:
https://github.com/chriskohlhoff/asio/blob/master/include/asio/buffer.hpp#L206

the buffer_check is initialized in the std::string and std::vector constructors, here:
https://github.com/chriskohlhoff/asio/blob/master/include/asio/buffer.hpp#L1356
https://github.com/chriskohlhoff/asio/blob/master/include/asio/buffer.hpp#L1290

By capturing the begin() iterator in a detail::buffer_debug_check. The call operator of which dereferences the iterator. This is relying on the standard library's debug iterator to then flag an invalidated iterator.

This approach works well for types where iterators and pointers are treated the same. e.g. std::string invalidates both pointers and iterators on move-construction or move-assignment (since it supports SSO). But std::vector is different. It guarantees stability of pointers but invalidates iterators, on move-assignment and move-construction.

This is a quirk raised in LWG 2321, but is still not resolved. libc++'s debug iterators assert in this case.

I encountered this problem because of SSL sockets holding buffers, which means move-constructing such socket will also move construct the buffers, which will invalidate the iterator member.

https://github.com/chriskohlhoff/asio/blob/master/include/asio/ssl/detail/stream_core.hpp#L42

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions