Skip to content

Commit 3e96022

Browse files
committed
🐛 Fix field extent
Problem: - There is an off-by-one error in the field extent code such that a message whose "rightmost" field is a single bit on a data boundary computes its storage wrongly. Solution: - Fix off-by-one error.
1 parent e2cf3cf commit 3e96022

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

include/msg/field.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ struct bits_locator_t {
206206
}
207207

208208
template <typename T> constexpr static auto extent_in() -> std::size_t {
209-
constexpr auto msb = Lsb + BitSize - 1;
210-
constexpr auto msb_extent = (msb + CHAR_BIT - 1) / CHAR_BIT;
209+
constexpr auto msb_exclusive = Lsb + BitSize;
210+
constexpr auto msb_extent = (msb_exclusive + CHAR_BIT - 1) / CHAR_BIT;
211211
constexpr auto base_extent = Index * sizeof(std::uint32_t);
212212
constexpr auto extent = base_extent + msb_extent;
213213
return (extent + sizeof(T) - 1) / sizeof(T);

test/msg/message.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,3 +840,12 @@ TEST_CASE("message with uninitialized field", "[message]") {
840840
auto data = msg.data();
841841
CHECK(data[0] == 1);
842842
}
843+
844+
TEST_CASE("field extents", "[message]") {
845+
using f1 = msg::detail::bits_locator_t<0, 1, 0>;
846+
STATIC_CHECK(f1::extent_in<std::uint32_t>() == 1);
847+
STATIC_CHECK(f1::extent_in<std::uint8_t>() == 1);
848+
using f2 = msg::detail::bits_locator_t<0, 1, 31>;
849+
STATIC_CHECK(f2::extent_in<std::uint32_t>() == 1);
850+
STATIC_CHECK(f2::extent_in<std::uint8_t>() == 4);
851+
}

0 commit comments

Comments
 (0)