Skip to content

Commit 0a4c0dc

Browse files
Added runtime and compile time check versions with buffer overflow check
1 parent 97f2321 commit 0a4c0dc

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

include/etl/file_error_numbers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,6 @@ SOFTWARE.
103103
#define ETL_EXPECTED_FILE_ID "70"
104104
#define ETL_ALIGNMENT_FILE_ID "71"
105105
#define ETL_BASE64_FILE_ID "72"
106+
#define ETL_UNALIGNED_TYPE_FILE_ID "73"
106107

107108
#endif

include/etl/unaligned_type.h

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ SOFTWARE.
3838
#include "platform.h"
3939
#include "type_traits.h"
4040
#include "endianness.h"
41+
#include "error_handler.h"
42+
#include "exception.h"
4143
#include "iterator.h"
4244
#include "algorithm.h"
4345
#include "bit.h"
@@ -46,6 +48,34 @@ SOFTWARE.
4648

4749
namespace etl
4850
{
51+
//***************************************************************************
52+
/// The base class for unaligned_type exceptions.
53+
///\ingroup pool
54+
//***************************************************************************
55+
struct unaligned_type_exception : public etl::exception
56+
{
57+
public:
58+
59+
unaligned_type_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
60+
: exception(reason_, file_name_, line_number_)
61+
{
62+
}
63+
};
64+
65+
//***************************************************************************
66+
/// The base class for unaligned_type buffer overflow.
67+
///\ingroup pool
68+
//***************************************************************************
69+
class unaligned_type_buffer_size : public unaligned_type_exception
70+
{
71+
public:
72+
73+
unaligned_type_buffer_size(string_type file_name_, numeric_type line_number_)
74+
: unaligned_type_exception(ETL_ERROR_TEXT("unaligned_type:buffer size", ETL_UNALIGNED_TYPE_FILE_ID"A"), file_name_, line_number_)
75+
{
76+
}
77+
};
78+
4979
namespace private_unaligned_type
5080
{
5181
//*************************************************************************
@@ -733,7 +763,7 @@ namespace etl
733763
//*******************************************
734764
/// at_address
735765
///\brief Helps to reinterprete memory as unaligned_type. Overload for write access.
736-
///\tparam address Pointer to memory to be reinterpreted.
766+
///\param address Pointer to memory to be reinterpreted.
737767
///\return Reference to unaligned_type object at location specified by address
738768
//*******************************************
739769
static unaligned_type<T, Endian_>& at_address(void* address)
@@ -744,13 +774,71 @@ namespace etl
744774
//*******************************************
745775
/// at_address
746776
///\brief Helps to reinterprete memory as unaligned_type. Overload for read only access to const memory.
747-
///\tparam address Pointer to memory to be reinterpreted.
777+
///\param address Pointer to memory to be reinterpreted.
748778
///\return Reference to unaligned_type object at location specified by address
749779
//*******************************************
750780
static const unaligned_type<T, Endian_>& at_address(const void* address)
751781
{
752782
return *reinterpret_cast<const unaligned_type<T, Endian_>*>(address);
753783
}
784+
785+
//*******************************************
786+
/// at_address
787+
///\brief Helps to reinterprete memory as unaligned_type. Overload for write access.
788+
///\param address Pointer to memory to be reinterpreted.
789+
///\param buffer_size Size in bytes for run time size check
790+
///\return Reference to unaligned_type object at location specified by address
791+
//*******************************************
792+
static unaligned_type<T, Endian_>& at_address(void* address, size_t buffer_size)
793+
{
794+
ETL_ASSERT(sizeof(T) <= buffer_size, ETL_ERROR(etl::unaligned_type_buffer_size));
795+
796+
return *reinterpret_cast<unaligned_type<T, Endian_>*>(address);
797+
}
798+
799+
//*******************************************
800+
/// at_address
801+
///\brief Helps to reinterprete memory as unaligned_type. Overload for read only access to const memory.
802+
///\param address Pointer to memory to be reinterpreted.
803+
///\param buffer_size Size in bytes for runtime size check
804+
///\return Reference to unaligned_type object at location specified by address
805+
//*******************************************
806+
static const unaligned_type<T, Endian_>& at_address(const void* address, size_t buffer_size)
807+
{
808+
ETL_ASSERT(sizeof(T) <= buffer_size, ETL_ERROR(etl::unaligned_type_buffer_size));
809+
810+
return *reinterpret_cast<const unaligned_type<T, Endian_>*>(address);
811+
}
812+
813+
//*******************************************
814+
/// at_address
815+
///\brief Helps to reinterprete memory as unaligned_type. Overload for write access.
816+
///\tparam BufferSize Size in bytes for compile time size check
817+
///\param address Pointer to memory to be reinterpreted.
818+
///\return Reference to unaligned_type object at location specified by address
819+
//*******************************************
820+
template <size_t BufferSize>
821+
static unaligned_type<T, Endian_>& at_address(void* address)
822+
{
823+
ETL_STATIC_ASSERT(sizeof(T) <= BufferSize, "Buffer size to small for type");
824+
825+
return *reinterpret_cast<unaligned_type<T, Endian_>*>(address);
826+
}
827+
828+
//*******************************************
829+
/// at_address
830+
///\brief Helps to reinterprete memory as unaligned_type. Overload for read only access to const memory.
831+
///\tparam BufferSize Size in bytes for compile size check
832+
///\param address Pointer to memory to be reinterpreted.
833+
///\return Reference to unaligned_type object at location specified by address
834+
//*******************************************
835+
template <size_t BufferSize>
836+
static unaligned_type<T, Endian_>& at_address(const void* address)
837+
{
838+
ETL_STATIC_ASSERT(sizeof(T) <= BufferSize, "Buffer size to small for type");
839+
840+
return *reinterpret_cast<const unaligned_type<T, Endian_>*>(address);
841+
}
754842
};
755843

756844
template <typename T, int Endian_>

0 commit comments

Comments
 (0)