diff --git a/vanetza/common/byte_view.cpp b/vanetza/common/byte_view.cpp index eeb702a6c..a739386e5 100644 --- a/vanetza/common/byte_view.cpp +++ b/vanetza/common/byte_view.cpp @@ -6,10 +6,26 @@ namespace vanetza { -byte_view_range::byte_view_range( - const ByteBuffer::const_iterator& begin, - const ByteBuffer::const_iterator& end) : - iterator_range(begin, end) +namespace +{ + +boost::iterator_range +make_safe_range(const ByteBuffer::const_iterator& begin, const ByteBuffer::const_iterator& end) +{ + if (begin < end) { + byte_view_iterator vbegin { begin }; + byte_view_iterator vend { std::next(vbegin, std::distance(begin, end)) }; + return boost::iterator_range { vbegin, vend }; + } else { + byte_view_iterator empty; + return boost::iterator_range { empty, empty }; + } +} + +} + +byte_view_range::byte_view_range(const ByteBuffer::const_iterator& begin, const ByteBuffer::const_iterator& end) : + iterator_range(make_safe_range(begin, end)) { } @@ -19,7 +35,7 @@ byte_view_range::byte_view_range(const byte_view_iterator& begin, const byte_vie } byte_view_range::byte_view_range(ByteBuffer&& _buffer) : - iterator_range(_buffer.begin(), _buffer.end()), buffer(std::move(_buffer)) + iterator_range(make_safe_range(_buffer.begin(), _buffer.end())), buffer(std::move(_buffer)) { } diff --git a/vanetza/common/byte_view.hpp b/vanetza/common/byte_view.hpp index 4fd3884ad..5fb240178 100644 --- a/vanetza/common/byte_view.hpp +++ b/vanetza/common/byte_view.hpp @@ -2,6 +2,7 @@ #define BYTE_VIEW_HPP_TXN2ISMB #include +#include #include #include #include @@ -23,7 +24,7 @@ class byte_view_iterator byte_view_iterator() = default; explicit byte_view_iterator(pointer p) : m_iterator(p) {} - explicit byte_view_iterator(const ByteBuffer::const_iterator& it) : m_iterator(&*it) {} + explicit byte_view_iterator(const ByteBuffer::const_iterator& it) : m_iterator(boost::to_address(it)) {} constexpr value_type operator*() const { @@ -153,7 +154,7 @@ class byte_view_range : private boost::iterator_range * \param buffer pass buffer via rvalue * \note View is valid without limitation */ - byte_view_range(ByteBuffer&&); + explicit byte_view_range(ByteBuffer&&); /** * Get pointer to start of contiguous buffer memory