From f57a1c3b171fa8b95ff3148d05fdaff0f4768547 Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Tue, 6 Jul 2021 00:08:55 +0200 Subject: [PATCH] CircularIndex: add static checks for index/size limits The empty/full distinction fails to work if size matches the number of representable positions for the index type. Add a static check to ensure this invariant is met where possible. --- src/hal/circular_buffer.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/hal/circular_buffer.h b/src/hal/circular_buffer.h index 89b16d8..4201a87 100644 --- a/src/hal/circular_buffer.h +++ b/src/hal/circular_buffer.h @@ -2,6 +2,9 @@ #include #include +#ifndef __AVR__ + #include +#endif /// A generic circular index class which can be used to build circular buffers /// Can hold up to size elements @@ -12,6 +15,11 @@ template class CircularIndex { public: +#ifndef __AVR__ + static_assert(size <= std::numeric_limits::max(), + "index_t is too small for the requested size"); +#endif + constexpr inline CircularIndex() : tail(0) , head(0) {}