From c4007799057dbe3609a0925c6d6b4a59e099ce2b Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Fri, 3 Nov 2023 18:37:25 +0100 Subject: [PATCH] #781 etl::multi_span::iterator::operator *() fails if first span is empty --- include/etl/multi_span.h | 14 +++++++++++++- test/test_multi_span.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/include/etl/multi_span.h b/include/etl/multi_span.h index 7f98865d..2838e007 100644 --- a/include/etl/multi_span.h +++ b/include/etl/multi_span.h @@ -173,7 +173,19 @@ namespace etl { if (p_current != p_end) { - p_value = p_current->begin(); + while ((p_current != p_end) && p_current->empty()) + { + ++p_current; + } + + if (p_current != p_end) + { + p_value = p_current->begin(); + } + else + { + p_value = ETL_NULLPTR; + } } } diff --git a/test/test_multi_span.cpp b/test/test_multi_span.cpp index d59c8d75..f4a4b066 100644 --- a/test/test_multi_span.cpp +++ b/test/test_multi_span.cpp @@ -174,5 +174,36 @@ namespace ++itr; CHECK_EQUAL(struct_data2[2].i, itr->i); } + + //************************************************************************* + TEST(test_multi_span_with_empty_spans) + { + std::vector> span_list = + { + etl::span(), + etl::span(struct_data1), + etl::span(), + etl::span(struct_data2), + etl::span() + }; + + etl::multi_span ms_data(etl::multi_span::span_list_type(std::begin(span_list), std::end(span_list))); + + etl::multi_span::iterator itr = ms_data.begin(); + + CHECK_EQUAL(struct_data1[0].i, (*itr).i); + ++itr; + CHECK_EQUAL(struct_data1[1].i, (*itr).i); + ++itr; + CHECK_EQUAL(struct_data1[2].i, (*itr).i); + ++itr; + CHECK_EQUAL(struct_data2[0].i, (*itr).i); + ++itr; + CHECK_EQUAL(struct_data2[1].i, (*itr).i); + ++itr; + CHECK_EQUAL(struct_data2[2].i, (*itr).i); + ++itr; + CHECK_EQUAL(ETL_NULLPTR, itr.operator->()); + } }; }