Skip to content

Commit

Permalink
Added tests for unaligned_type::at_address with buffer overflow check
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandreichweinbmw committed Dec 24, 2024
1 parent 36724d9 commit 4a8f3af
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion include/etl/unaligned_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ namespace etl
///\return Reference to unaligned_type object at location specified by address
//*******************************************
template <size_t BufferSize>
static unaligned_type<T, Endian_>& at_address(const void* address)
static const unaligned_type<T, Endian_>& at_address(const void* address)
{
ETL_STATIC_ASSERT(sizeof(T) <= BufferSize, "Buffer size to small for type");

Expand Down
42 changes: 42 additions & 0 deletions test/test_unaligned_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<sizeof(data)>(data), 0x01020304);
CHECK_EQUAL(etl::le_uint32_t::at_address<sizeof(data)>(data), 0x04030201);
CHECK_EQUAL(etl::be_uint32_t::at_address<sizeof(data) + 1>(data), 0x01020304);
CHECK_EQUAL(etl::le_uint32_t::at_address<sizeof(data) + 1>(data), 0x04030201);
// static_assert:
//CHECK_THROW(etl::be_uint32_t::at_address<sizeof(data) - 1>(data), etl::unaligned_type_buffer_size);
//CHECK_THROW(etl::le_uint32_t::at_address<sizeof(data) - 1>(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);
}
Expand All @@ -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<sizeof(data)>(data), 0x01020304);
CHECK_EQUAL(etl::le_uint32_t::at_address<sizeof(data)>(data), 0x04030201);
CHECK_EQUAL(etl::be_uint32_t::at_address<sizeof(data) + 1>(data), 0x01020304);
CHECK_EQUAL(etl::le_uint32_t::at_address<sizeof(data) + 1>(data), 0x04030201);
// static_assert:
//CHECK_THROW(etl::be_uint32_t::at_address<sizeof(data) - 1>(data), etl::unaligned_type_buffer_size);
//CHECK_THROW(etl::le_uint32_t::at_address<sizeof(data) - 1>(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);
}
};
}
Expand Down

0 comments on commit 4a8f3af

Please sign in to comment.