From e5844b23b75e33ef37eb49b7d159304e27c3c4f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 9 Apr 2023 10:40:47 +0200 Subject: [PATCH] Math: use a more common integer rounding calculation in BitVector. The (size - 1)/8 + 1 expression is the same as (size - 1)/8 + 8/8 which is the same as the following in all cases except when size == 0, for which it underflows: (size - 1 + 8)/8 == (size + 7)/8 To avoid needless surprises, I'm changing to what's used everywhere else, including the BitArray, and also reusing the already-calculated value for the _data array size. --- src/Magnum/Math/BitVector.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Magnum/Math/BitVector.h b/src/Magnum/Math/BitVector.h index ab5a393b2..ef50a9e49 100644 --- a/src/Magnum/Math/BitVector.h +++ b/src/Magnum/Math/BitVector.h @@ -88,7 +88,7 @@ template class BitVector { public: enum: std::size_t { Size = size, /**< Vector size */ - DataSize = (size-1)/8+1 /**< Vector storage size */ + DataSize = (size + 7)/8 /**< Vector storage size */ }; /** @@ -303,7 +303,7 @@ template class BitVector { /* Implementation for Vector::Vector(U) */ template constexpr explicit BitVector(Corrade::Containers::Implementation::Sequence, UnsignedByte value): _data{Implementation::repeat(value, sequence)...} {} - UnsignedByte _data[(size-1)/8+1]; + UnsignedByte _data[DataSize]; }; #ifndef CORRADE_NO_DEBUG