diff --git a/include/fastrtps/utils/fixed_size_bitmap.hpp b/include/fastrtps/utils/fixed_size_bitmap.hpp index 4fead5e6d80..40bbd627b47 100644 --- a/include/fastrtps/utils/fixed_size_bitmap.hpp +++ b/include/fastrtps/utils/fixed_size_bitmap.hpp @@ -427,9 +427,11 @@ class BitmapRange uint32_t num_bytes = num_items * static_cast(sizeof(uint32_t)); bitmap_.fill(0u); memcpy(bitmap_.data(), bitmap, num_bytes); - if (0 < num_bits) + // trim unused bits if (0 < num_bits && num_bits % 32 != 0) + short shift = num_bits & 31u; + if (0 < num_bits && shift != 0) { - bitmap_[num_items - 1] &= ~(std::numeric_limits::max() >> (num_bits & 31u)); + bitmap_[num_items - 1] &= ~(std::numeric_limits::max() >> shift); } calc_maximum_bit_set(num_items, 0); } diff --git a/test/unittest/utils/BitmapRangeTests.cpp b/test/unittest/utils/BitmapRangeTests.cpp index 1f268347fe3..686d44e6e95 100644 --- a/test/unittest/utils/BitmapRangeTests.cpp +++ b/test/unittest/utils/BitmapRangeTests.cpp @@ -431,6 +431,8 @@ TEST_F(BitmapRangeTests, full_range) TEST_F(BitmapRangeTests, serialization) { + using value_type = TestType::bitmap_type::value_type; + uint32_t num_bits; uint32_t num_longs; TestType::bitmap_type bitmap; @@ -455,6 +457,25 @@ TEST_F(BitmapRangeTests, serialization) EXPECT_EQ(num_bits, 20u); EXPECT_EQ(num_longs, 1u); EXPECT_EQ(bitmap[0], 0xFFFFF000u); + + // Case when whole bitmap elements are used + uint32_t test_longs = 1; + + do + { + uint32_t test_bits = test_longs * sizeof(value_type) * 8; + bitmap.fill(std::numeric_limits::max()); + uut.bitmap_set(test_bits, bitmap.data()); + uut.bitmap_get(num_bits, bitmap, num_longs); + EXPECT_EQ(num_bits, test_bits); + EXPECT_EQ(num_longs, test_longs); + + // use a vector as result pattern + std::vector pattern(test_longs, std::numeric_limits::max()); + pattern.resize(bitmap.max_size(), 0); + EXPECT_TRUE(std::equal(bitmap.begin(), bitmap.end(), pattern.begin())); + } + while ( ++test_longs <= bitmap.max_size()); } TEST_F(BitmapRangeTests, traversal)