Opened 6 years ago

Closed 6 years ago

#12235 closed Bugs (invalid)

boost/endian causes reads from unaligned address.

Reported by: amotsok@… Owned by: Beman Dawes
Milestone: To Be Determined Component: endian
Version: Boost 1.61.0 Severity: Problem
Keywords: Cc:

Description

Example:

#include <boost/endian/arithmetic.hpp>

int main()
{
  char __attribute__ ((aligned (4))) buf[20];

  boost::endian::native_int32_t * ptr = (boost::endian::native_int32_t *)&buf[1];

  return ptr->value();
}

when stepping inside boost::endian::native_int32_t::value() I see the code which is doing actual work:

  return *reinterpret_cast<T const *>(bytes); 

where bytes is argument which corresponds to class member of endian_buffer which declared as:

  char m_value[n_bits/8];

In this particular example m_value gets address buf + 1 and it is not dividable by four:

  (int)ptr->m_value & 3 == 1

Not all platforms support reading from unaligned address. This behavior is dangerous and possibly can cause crashes on those platforms.

Change History (2)

comment:1 by Andrii Motsok <amotsok@…>, 6 years ago

Component: Noneendian
Owner: set to Beman Dawes

comment:2 by Beman Dawes, 6 years ago

Resolution: invalid
Status: newclosed

The code you are complaining about is inside this:

# if defined(x86_64)
defined(_M_X64) defined(i386) defined(_M_IX86)

On x86 (which is little endian), unaligned loads are permitted

so is only reached on machines which permit unaligned loads.

--Beman

Note: See TracTickets for help on using tickets.