Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/segfault when getting surrounding interval of empty cache #116

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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