diff --git a/include/etl/unaligned_type.h b/include/etl/unaligned_type.h index c0a79e142..5abe33343 100644 --- a/include/etl/unaligned_type.h +++ b/include/etl/unaligned_type.h @@ -833,7 +833,7 @@ namespace etl ///\return Reference to unaligned_type object at location specified by address //******************************************* template - static unaligned_type& at_address(const void* address) + static const unaligned_type& at_address(const void* address) { ETL_STATIC_ASSERT(sizeof(T) <= BufferSize, "Buffer size to small for type"); diff --git a/test/test_unaligned_type.cpp b/test/test_unaligned_type.cpp index 5c07a7665..c5c5d6927 100644 --- a/test/test_unaligned_type.cpp +++ b/test/test_unaligned_type.cpp @@ -990,6 +990,27 @@ namespace CHECK_EQUAL(etl::be_uint32_t::at_address(data), 0x01020304); CHECK_EQUAL(etl::le_uint32_t::at_address(data), 0x04030201); + // with overflow checks: at runtime + CHECK_EQUAL(etl::be_uint32_t::at_address(data, sizeof(data)), 0x01020304); + CHECK_EQUAL(etl::le_uint32_t::at_address(data, sizeof(data)), 0x04030201); + CHECK_EQUAL(etl::be_uint32_t::at_address(data, sizeof(data) + 1), 0x01020304); + CHECK_EQUAL(etl::le_uint32_t::at_address(data, sizeof(data) + 1), 0x04030201); + CHECK_THROW(etl::be_uint32_t::at_address(data, sizeof(data) - 1), etl::unaligned_type_buffer_size); + CHECK_THROW(etl::le_uint32_t::at_address(data, sizeof(data) - 1), etl::unaligned_type_buffer_size); + CHECK_THROW(etl::be_uint32_t::at_address(data, 0), etl::unaligned_type_buffer_size); + CHECK_THROW(etl::le_uint32_t::at_address(data, 0), etl::unaligned_type_buffer_size); + + // with overflow checks: at compile time + CHECK_EQUAL(etl::be_uint32_t::at_address(data), 0x01020304); + CHECK_EQUAL(etl::le_uint32_t::at_address(data), 0x04030201); + CHECK_EQUAL(etl::be_uint32_t::at_address(data), 0x01020304); + CHECK_EQUAL(etl::le_uint32_t::at_address(data), 0x04030201); + // static_assert: + //CHECK_THROW(etl::be_uint32_t::at_address(data), etl::unaligned_type_buffer_size); + //CHECK_THROW(etl::le_uint32_t::at_address(data), etl::unaligned_type_buffer_size); + //CHECK_THROW(etl::be_uint32_t::at_address<0>(data), etl::unaligned_type_buffer_size); + //CHECK_THROW(etl::le_uint32_t::at_address<0>(data), etl::unaligned_type_buffer_size); + etl::be_uint32_t::at_address(data) = 0x12345678; CHECK_EQUAL(etl::le_uint32_t::at_address(data), 0x78563412); } @@ -1000,6 +1021,27 @@ namespace CHECK_EQUAL(etl::be_uint32_t::at_address(data), 0x01020304); CHECK_EQUAL(etl::le_uint32_t::at_address(data), 0x04030201); + + // with overflow checks: at runtime + CHECK_EQUAL(etl::be_uint32_t::at_address(data, sizeof(data)), 0x01020304); + CHECK_EQUAL(etl::le_uint32_t::at_address(data, sizeof(data)), 0x04030201); + CHECK_EQUAL(etl::be_uint32_t::at_address(data, sizeof(data) + 1), 0x01020304); + CHECK_EQUAL(etl::le_uint32_t::at_address(data, sizeof(data) + 1), 0x04030201); + CHECK_THROW(etl::be_uint32_t::at_address(data, sizeof(data) - 1), etl::unaligned_type_buffer_size); + CHECK_THROW(etl::le_uint32_t::at_address(data, sizeof(data) - 1), etl::unaligned_type_buffer_size); + CHECK_THROW(etl::be_uint32_t::at_address(data, 0), etl::unaligned_type_buffer_size); + CHECK_THROW(etl::le_uint32_t::at_address(data, 0), etl::unaligned_type_buffer_size); + + // with overflow checks: at compile time + CHECK_EQUAL(etl::be_uint32_t::at_address(data), 0x01020304); + CHECK_EQUAL(etl::le_uint32_t::at_address(data), 0x04030201); + CHECK_EQUAL(etl::be_uint32_t::at_address(data), 0x01020304); + CHECK_EQUAL(etl::le_uint32_t::at_address(data), 0x04030201); + // static_assert: + //CHECK_THROW(etl::be_uint32_t::at_address(data), etl::unaligned_type_buffer_size); + //CHECK_THROW(etl::le_uint32_t::at_address(data), etl::unaligned_type_buffer_size); + //CHECK_THROW(etl::be_uint32_t::at_address<0>(data), etl::unaligned_type_buffer_size); + //CHECK_THROW(etl::le_uint32_t::at_address<0>(data), etl::unaligned_type_buffer_size); } }; }