Opened 6 years ago
#12162 new Bugs
buffer interface violates strict-aliasing rule
Reported by: | Owned by: | Beman Dawes | |
---|---|---|---|
Milestone: | To Be Determined | Component: | endian |
Version: | Boost 1.60.0 | Severity: | Problem |
Keywords: | Cc: |
Description
The buffer class interfaces can cause violations of the strict-aliasing rule. This example program emits this warning when compiled with optimizations on GCC 5.1.1:
#include <boost/asio/streambuf.hpp> #include <boost/endian/buffers.hpp> #include <cstdint> #include <memory> template<class Streambuf> std::uint16_t get(Streambuf& sb) { using namespace boost::asio; using namespace boost::endian; std::uint8_t b[2]; sb.consume(buffer_copy(buffer(b), sb.data())); return reinterpret_cast<big_uint16_buf_t const*>(&b[0])->value(); } int main() { using namespace boost::asio; streambuf sb; std::uint8_t b[2]; memset(&b[0], 0, sizeof(b)); sb.commit(buffer_copy(sb.prepare(sizeof(b)), buffer(b))); return get(sb); }
Produces:
prog.cc: In instantiation of 'uint16_t get(Streambuf&) [with Streambuf = boost::asio::basic_streambuf<>; uint16_t = short unsigned int]': prog.cc:24:18: required from here prog.cc:14:67: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] return reinterpret_cast<big_uint16_buf_t const*>(&b[0])->value();
Link to program and output: http://melpon.org/wandbox/permlink/x7xQsxaU3jv0lOtD
Note:
See TracTickets
for help on using tickets.