From 288587070c33385aa2a621819938b4f6e8c638ff Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 31 May 2026 14:00:51 +0200 Subject: [PATCH] Fix fixed_iterator::operator* return type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixed_iterator::operator* returned value_type by value, so writes like *it = ... modified a temporary rather than the underlying location. This broke the iterator’s intended use (e.g., writing to a fixed register/memory location). Now returning iterator_traits::reference instead (const and non-const overloads). --- include/etl/fixed_iterator.h | 4 ++-- test/test_fixed_iterator.cpp | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/etl/fixed_iterator.h b/include/etl/fixed_iterator.h index f0ab7609..fb274b6c 100644 --- a/include/etl/fixed_iterator.h +++ b/include/etl/fixed_iterator.h @@ -108,7 +108,7 @@ namespace etl //*************************************************************************** /// Dereference operator. //*************************************************************************** - ETL_CONSTEXPR14 typename etl::iterator_traits::value_type operator*() + ETL_CONSTEXPR14 typename etl::iterator_traits::reference operator*() { return *it; } @@ -116,7 +116,7 @@ namespace etl //*************************************************************************** /// Dereference operator. //*************************************************************************** - ETL_CONSTEXPR const typename etl::iterator_traits::value_type operator*() const + ETL_CONSTEXPR typename etl::iterator_traits::reference operator*() const { return *it; } diff --git a/test/test_fixed_iterator.cpp b/test/test_fixed_iterator.cpp index af6d54a1..72b6c907 100644 --- a/test/test_fixed_iterator.cpp +++ b/test/test_fixed_iterator.cpp @@ -229,6 +229,22 @@ namespace CHECK(fi1 != fi3); } + //************************************************************************* + TEST(test_dereference_write_through) + { + int value = 42; + etl::fixed_iterator fi(&value); + + // Writing through the dereferenced iterator should modify the underlying value. + *fi = 99; + CHECK_EQUAL(99, value); + + // Increment does nothing, so writing again still targets the same location. + ++fi; + *fi = 123; + CHECK_EQUAL(123, value); + } + #if ETL_USING_CPP14 //************************************************************************* TEST(test_fixed_iterator_constexpr_ctor)