[Boost-bugs] [Boost C++ Libraries] #12162: buffer interface violates strict-aliasing rule

Subject: [Boost-bugs] [Boost C++ Libraries] #12162: buffer interface violates strict-aliasing rule
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2016-04-27 12:08:13


#12162: buffer interface violates strict-aliasing rule
-------------------------------------------+------------------------
 Reporter: Vinnie Falco <vinnie.falco@…> | Owner: bemandawes
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: endian
  Version: Boost 1.60.0 | Severity: Problem
 Keywords: |
-------------------------------------------+------------------------
 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

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/12162>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:20 UTC