Skip to content
31 changes: 28 additions & 3 deletions include/etl/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ namespace etl
ETL_CONSTEXPR14
reference operator[](size_t i)
{
ETL_DEBUG_ASSERT(i < SIZE, ETL_ERROR(array_out_of_range));

return _buffer[i];
}

Expand All @@ -154,8 +156,10 @@ namespace etl
///\param i The index of the element to access.
//*************************************************************************
ETL_NODISCARD
ETL_CONSTEXPR const_reference operator[](size_t i) const
ETL_CONSTEXPR14 const_reference operator[](size_t i) const
{
ETL_DEBUG_ASSERT(i < SIZE, ETL_ERROR(array_out_of_range));

return _buffer[i];
}

Expand All @@ -166,6 +170,8 @@ namespace etl
ETL_CONSTEXPR14
reference front()
{
ETL_STATIC_ASSERT(SIZE > 0, "Array is empty.");

return _buffer[0];
}

Expand All @@ -175,6 +181,8 @@ namespace etl
ETL_NODISCARD
ETL_CONSTEXPR const_reference front() const
{
ETL_STATIC_ASSERT(SIZE > 0, "Array is empty.");

return _buffer[0];
}

Expand All @@ -185,6 +193,8 @@ namespace etl
ETL_CONSTEXPR14
reference back()
{
ETL_STATIC_ASSERT(SIZE > 0, "Array is empty.");

return _buffer[SIZE - 1];
}

Expand All @@ -194,6 +204,8 @@ namespace etl
ETL_NODISCARD
ETL_CONSTEXPR const_reference back() const
{
ETL_STATIC_ASSERT(SIZE > 0, "Array is empty.");

return _buffer[SIZE - 1];
}

Expand Down Expand Up @@ -439,6 +451,8 @@ namespace etl
//*************************************************************************
iterator insert(const_iterator position, parameter_t value)
{
ETL_DEBUG_ASSERT(cbegin() <= position && position < cend(), ETL_ERROR(array_out_of_range));

iterator p = to_iterator(position);

etl::move_backward(p, end() - 1, end());
Expand Down Expand Up @@ -468,6 +482,8 @@ namespace etl
template <typename TIterator>
iterator insert(const_iterator position, TIterator first, const TIterator last)
{
ETL_DEBUG_ASSERT(cbegin() <= position && position < cend(), ETL_ERROR(array_out_of_range));

iterator p = to_iterator(position);
iterator result(p);

Expand Down Expand Up @@ -504,6 +520,8 @@ namespace etl
//*************************************************************************
iterator erase(const_iterator position)
{
ETL_DEBUG_ASSERT(cbegin() <= position && position < cend(), ETL_ERROR(array_out_of_range));

iterator p = to_iterator(position);
etl::move(p + 1, end(), p);

Expand All @@ -529,6 +547,8 @@ namespace etl
//*************************************************************************
iterator erase(const_iterator first, const_iterator last)
{
ETL_DEBUG_ASSERT(cbegin() <= first && first < last && last <= cend(), ETL_ERROR(array_out_of_range));

iterator p = to_iterator(first);
etl::move(last, cend(), p);
return p;
Expand All @@ -551,6 +571,8 @@ namespace etl
//*************************************************************************
iterator erase(const_iterator position, parameter_t value)
{
ETL_DEBUG_ASSERT(cbegin() <= position && position < cend(), ETL_ERROR(array_out_of_range));

iterator p = to_iterator(position);

etl::move(p + 1, end(), p);
Expand All @@ -572,11 +594,14 @@ namespace etl

//*************************************************************************
/// Erases a range of values from the array.
///\param position The iterator to the position to erase at.
///\param value The value to use to overwrite the last elements in the array.
///\param first The first item to erase.
///\param last The one past the last item to erase.
///\param value The value to use to overwrite the last elements in the array.
//*************************************************************************
iterator erase(const_iterator first, const_iterator last, parameter_t value)
{
ETL_DEBUG_ASSERT(cbegin() <= first && first < last && last <= cend(), ETL_ERROR(array_out_of_range));

iterator p = to_iterator(first);

p = etl::move(last, cend(), p);
Expand Down
10 changes: 10 additions & 0 deletions include/etl/error_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,16 @@ namespace etl
#endif
#endif

#if ETL_IS_DEBUG_BUILD
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should be other macros that make ETL_DEBUG_ASSERT consistent with the options provided by the ETL_ASSERT_XXX set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I filled out the rest of the asserts from the ETL_ASSERT_XXX set. I'm not sure if its useful or not but I also included macros for ETL_DEBUG_USE_ASSERT_FUNCTION and ETL_DEBUG_LOG_ERRORS.

#if ETL_USING_EXCEPTIONS
#define ETL_DEBUG_ASSERT(b, e) {if (!(b)) ETL_UNLIKELY {throw((e));}} // If the condition fails, throws an exception.
#else
#define ETL_DEBUG_ASSERT(b, e) assert((b)) // If the condition fails, asserts.
#endif
#else
#define ETL_DEBUG_ASSERT(b, e) // Does nothing.
#endif

#if defined(ETL_VERBOSE_ERRORS)
#define ETL_ERROR(e) (e(__FILE__, __LINE__)) // Make an exception with the file name and line number.
#define ETL_ERROR_WITH_VALUE(e, v) (e(__FILE__, __LINE__, (v))) // Make an exception with the file name, line number and value.
Expand Down
24 changes: 6 additions & 18 deletions include/etl/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -675,9 +675,7 @@ namespace etl
//*******************************************
value_type* operator ->()
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
#endif
ETL_DEBUG_ASSERT(has_value(), ETL_ERROR(expected_invalid));

return etl::addressof(etl::get<value_type>(storage));
}
Expand All @@ -687,9 +685,7 @@ namespace etl
//*******************************************
const value_type* operator ->() const
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
#endif
ETL_DEBUG_ASSERT(has_value(), ETL_ERROR(expected_invalid));

return etl::addressof(etl::get<value_type>(storage));
}
Expand All @@ -699,9 +695,7 @@ namespace etl
//*******************************************
value_type& operator *() ETL_LVALUE_REF_QUALIFIER
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
#endif
ETL_DEBUG_ASSERT(has_value(), ETL_ERROR(expected_invalid));

return etl::get<value_type>(storage);
}
Expand All @@ -711,9 +705,7 @@ namespace etl
//*******************************************
const value_type& operator *() const ETL_LVALUE_REF_QUALIFIER
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
#endif
ETL_DEBUG_ASSERT(has_value(), ETL_ERROR(expected_invalid));

return etl::get<value_type>(storage);
}
Expand All @@ -724,9 +716,7 @@ namespace etl
//*******************************************
value_type&& operator *()&&
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
#endif
ETL_DEBUG_ASSERT(has_value(), ETL_ERROR(expected_invalid));

return etl::move(etl::get<value_type>(storage));
}
Expand All @@ -736,9 +726,7 @@ namespace etl
//*******************************************
const value_type&& operator *() const&&
{
#if ETL_IS_DEBUG_BUILD
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
#endif
ETL_DEBUG_ASSERT(has_value(), ETL_ERROR(expected_invalid));

return etl::move(etl::get<value_type>(storage));
}
Expand Down
25 changes: 25 additions & 0 deletions test/test_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ namespace
{
CHECK_EQUAL(data[i], compare_data[i]);
}

//ETL_DEBUG and ETL_THROW_EXCEPTIONS are defined
CHECK_THROW({ int d = data[data.size()]; (void)d; }, etl::array_out_of_range);
}
Comment on lines +138 to 140
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Gate the out-of-bounds [] checks behind the same flags used by ETL_DEBUG_ASSERT.

Unconditionally requiring a throw will fail in release or non-exception builds. Guard the checks so they only run when ETL_IS_DEBUG_BUILD && ETL_USING_EXCEPTIONS (or ETL_DEBUG_USING_EXCEPTIONS if adopted).

-      //ETL_DEBUG and ETL_THROW_EXCEPTIONS are defined
-      CHECK_THROW({ int d = data[data.size()]; (void)d; }, etl::array_out_of_range);
+      // Only active when debug + exceptions route is enabled for ETL_DEBUG_ASSERT
+#if ETL_IS_DEBUG_BUILD && ETL_USING_EXCEPTIONS
+      CHECK_THROW({ int d = data[data.size()]; (void)d; }, etl::array_out_of_range);
+#endif
🤖 Prompt for AI Agents
In test/test_array.cpp around lines 138 to 140, the test unconditionally expects
an exception from an out-of-bounds operator[] which will fail in release or
non-exception builds; wrap the CHECK_THROW block with a preprocessor guard so it
only runs when both debug and exceptions are enabled (e.g. #if
defined(ETL_IS_DEBUG_BUILD) && (defined(ETL_USING_EXCEPTIONS) ||
defined(ETL_DEBUG_USING_EXCEPTIONS))) and close the #endif after the
CHECK_THROW; ensure the guarded code preserves the existing CHECK_THROW
invocation and adds no additional side effects.

Copy link
Contributor Author

@mike919192 mike919192 Sep 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this applies because there are many tests that that rely on exceptions and debug build already. Basically most tests already rely on these build options.


//*************************************************************************
Expand All @@ -145,6 +148,9 @@ namespace
{
CHECK_EQUAL(data[i], compare_data[i]);
}

//ETL_DEBUG and ETL_THROW_EXCEPTIONS are defined
CHECK_THROW({ int d = data[data.size()]; (void)d; }, etl::array_out_of_range);
}

//*************************************************************************
Expand Down Expand Up @@ -443,6 +449,10 @@ namespace
CHECK_EQUAL(data[9], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check3));
CHECK(isEqual);

// Insert out of range
//ETL_DEBUG and ETL_THROW_EXCEPTIONS are defined
CHECK_THROW({ result = data.insert_at(data.size(), 99); }, etl::array_out_of_range);
}

//*************************************************************************
Expand Down Expand Up @@ -493,6 +503,10 @@ namespace
CHECK_EQUAL(data[4], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check5));
CHECK(isEqual);

// Insert out of range
//ETL_DEBUG and ETL_THROW_EXCEPTIONS are defined
CHECK_THROW({ result = data.insert_at(data.size(), &source2[0], &source2[13]); }, etl::array_out_of_range);
}

//*************************************************************************
Expand Down Expand Up @@ -547,6 +561,10 @@ namespace
CHECK_EQUAL(data[9], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check3b));
CHECK(isEqual);

// Erase out of range
//ETL_DEBUG and ETL_THROW_EXCEPTIONS are defined
CHECK_THROW({ result = data.erase_at(data.size()); }, etl::array_out_of_range);
}

//*************************************************************************
Expand Down Expand Up @@ -601,6 +619,13 @@ namespace
CHECK_EQUAL(data[5], *result);
isEqual = std::equal(data.begin(), data.end(), std::begin(check3b));
CHECK(isEqual);

//ETL_DEBUG and ETL_THROW_EXCEPTIONS are defined
// Erase indexes are equal
CHECK_THROW({ result = data.erase_range(5, 5, 99); }, etl::array_out_of_range);

// Erase out of range
CHECK_THROW({ result = data.erase_range(5, data.size() + 1, 99); }, etl::array_out_of_range);
}

//*************************************************************************
Expand Down
Loading