From b030b8e78911eef688f0f5b7da74dadf70cbe04a Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Sun, 7 Jul 2024 14:58:07 -0500 Subject: [PATCH] AP_Common: correctly bound bitmask datatypes Probably won't work correctly (and of questionable use) if less than 1. The `first_set()` function might not be able to return a valid value if greater than INT16_MAX. unsigned int needs to be >= uint32_t so that the shift ops are in range. --- libraries/AP_Common/Bitmask.h | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/libraries/AP_Common/Bitmask.h b/libraries/AP_Common/Bitmask.h index 4b14de01b3..24ee2fcf1b 100644 --- a/libraries/AP_Common/Bitmask.h +++ b/libraries/AP_Common/Bitmask.h @@ -25,6 +25,12 @@ template class Bitmask { + static_assert(num_bits > 0, "must store something"); + // for first_set()'s return value + static_assert(num_bits <= INT16_MAX, "must fit in int16_t"); + // so that 1U << bits is in range + static_assert(sizeof(unsigned int) >= sizeof(uint32_t), "int too small"); + public: Bitmask() : numbits(num_bits), @@ -122,13 +128,7 @@ public: uint16_t count() const { uint16_t sum = 0; for (uint16_t i=0; i