Add checked index for circular buffer

This commit is contained in:
Mike Bloom 2026-10-11 22:08:45 +00:00
parent e1ec90aade
commit 551deac9da
3 changed files with 22 additions and 0 deletions

View File

@ -123,6 +123,7 @@ const_reference operator[](size_t i) const
``` ```
**Description** **Description**
Returns a reference or const reference to the indexed element. Returns a reference or const reference to the indexed element.
Emits an `etl::circular_buffer_out_of_bounds` if ETL_CHECK_INDEX_OPERATOR is defined and the index is out of range of the deque. Undefined behaviour if asserts or exceptions are not enabled.
--- ---

View File

@ -83,6 +83,19 @@ namespace etl
} }
}; };
//***************************************************************************
/// circular_buffer out of bounds exception.
//***************************************************************************
class circular_buffer_out_of_bounds : public etl::circular_buffer_exception
{
public:
circular_buffer_out_of_bounds(string_type file_name_, numeric_type line_number_)
: etl::circular_buffer_exception(ETL_ERROR_TEXT("circular_buffer:bounds", ETL_CIRCULAR_BUFFER_FILE_ID"C"), file_name_, line_number_)
{
}
};
//*************************************************************************** //***************************************************************************
/// ///
//*************************************************************************** //***************************************************************************
@ -888,6 +901,8 @@ namespace etl
//************************************************************************* //*************************************************************************
reference operator[](size_t index) reference operator[](size_t index)
{ {
ETL_ASSERT_CHECK_INDEX_OPERATOR(index < size(), ETL_ERROR(circular_buffer_out_of_bounds));
return pbuffer[(out + index) % buffer_size]; return pbuffer[(out + index) % buffer_size];
} }
@ -897,6 +912,8 @@ namespace etl
//************************************************************************* //*************************************************************************
const_reference operator[](size_t index) const const_reference operator[](size_t index) const
{ {
ETL_ASSERT_CHECK_INDEX_OPERATOR(index < size(), ETL_ERROR(circular_buffer_out_of_bounds));
return pbuffer[(out + index) % buffer_size]; return pbuffer[(out + index) % buffer_size];
} }

View File

@ -647,6 +647,8 @@ namespace
{ {
CHECK_EQUAL(input2[i], data[i]); CHECK_EQUAL(input2[i], data[i]);
} }
CHECK_THROW(data[data.size()], etl::circular_buffer_out_of_bounds);
} }
//************************************************************************* //*************************************************************************
@ -662,6 +664,8 @@ namespace
{ {
CHECK_EQUAL(input[i + 3], data[i]); CHECK_EQUAL(input[i + 3], data[i]);
} }
CHECK_THROW(data[data.size()], etl::circular_buffer_out_of_bounds);
} }
//************************************************************************* //*************************************************************************