Skip to content

Commit

Permalink
Bugfix/segfault when getting surrounding interval of empty cache (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cakem1x authored Aug 1, 2024
1 parent c69aaed commit e60450d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/message_filters/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ class Cache : public SimpleFilter<M>
namespace mt = message_filters::message_traits;

std::lock_guard<std::mutex> lock(cache_lock_);
if (cache_.size() == 0) {
// Early return for empty cache
// Logic below is only valid for caches with at least one element
return {};
}

// Find the starting index. (Find the first index after [or at] the start of the interval)
int start_index = static_cast<int>(cache_.size()) - 1;
while (start_index > 0 &&
Expand Down
10 changes: 10 additions & 0 deletions test/msg_cache_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ void fillCacheEasy(message_filters::Cache<Msg> & cache, unsigned int start, unsi
}
}

TEST(Cache, emptySurroundingInterval)
{
message_filters::Cache<Msg> cache(10);
const std::vector<std::shared_ptr<Msg const>> interval_data = cache.getSurroundingInterval(
rclcpp::Time(5, 0), rclcpp::Time(9, 0));

// empty cache shall return empty interval
EXPECT_EQ(interval_data.size(), static_cast<unsigned int>(0));
}

TEST(Cache, easyInterval)
{
message_filters::Cache<Msg> cache(10);
Expand Down

0 comments on commit e60450d

Please sign in to comment.