Skip to content

Commit 572f98e

Browse files
jncfaMichaelOrlov
andauthored
Check for nullptrs when pushing new messages to the message cache (#2219)
* fix: add nullptr check when pushing new messages to the message cache Signed-off-by: Jose Faria <[email protected]> * refactor: apply changes from review Signed-off-by: Jose Faria <[email protected]> * nit: update doxygen comments Signed-off-by: jncfa <[email protected]> * Update Doxygen comment for the MessageCacheCircularBuffer::push(msg) Signed-off-by: Michael Orlov <[email protected]> --------- Signed-off-by: Jose Faria <[email protected]> Signed-off-by: jncfa <[email protected]> Signed-off-by: Michael Orlov <[email protected]> Co-authored-by: Michael Orlov <[email protected]>
1 parent 77edeb8 commit 572f98e

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

rosbag2_cpp/include/rosbag2_cpp/cache/circular_message_cache.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class ROSBAG2_CPP_PUBLIC CircularMessageCache
5858

5959
/// Puts msg into circular buffer, replacing the oldest msg when buffer is full
6060
/// \return True if message was successfully pushed, otherwise false.
61-
/// NOTE: This will always return true, since the circular buffer by design drops old messages
62-
/// when the buffer is full.
61+
/// NOTE: Unless message is null or too large for the buffer, this will always return true
62+
/// since the circular buffer by design drops old messages when the buffer is full.
6363
bool push(std::shared_ptr<const rosbag2_storage::SerializedBagMessage> msg) override;
6464

6565
/// Get current buffer to consume.

rosbag2_cpp/include/rosbag2_cpp/cache/message_cache_circular_buffer.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ class ROSBAG2_CPP_PUBLIC MessageCacheCircularBuffer
5454
explicit MessageCacheCircularBuffer(size_t max_cache_size);
5555

5656
/**
57-
* If buffer size has some space left, we push the message regardless of its size,
58-
* but if this results in exceeding buffer size, we begin dropping old messages.
57+
* \brief Pushes a SerializedBagMessage into the cache buffer.
58+
* \details If buffer size has some space left, we push the message regardless of its size,
59+
* but if this results in exceeding buffer size, we begin dropping old messages.
60+
* \param msg SerializedBagMessage to add to the buffer.
61+
* \return True if message was successfully pushed. Returns false if msg is null or if msg size
62+
* exceeds max buffer size.
5963
*/
6064
bool push(CacheBufferInterface::buffer_element_t msg) override;
6165

rosbag2_cpp/src/rosbag2_cpp/cache/circular_message_cache.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ CircularMessageCache::~CircularMessageCache()
4343
bool CircularMessageCache::push(std::shared_ptr<const rosbag2_storage::SerializedBagMessage> msg)
4444
{
4545
std::lock_guard<std::mutex> cache_lock(producer_buffer_mutex_);
46-
producer_buffer_->push(msg);
47-
// Always return true since circular message cache drops old messages by design and it
48-
// shouldn't be counted as a lost messages.
49-
return true;
46+
return producer_buffer_->push(msg);
5047
}
5148

5249
std::shared_ptr<CacheBufferInterface> CircularMessageCache::get_consumer_buffer()

rosbag2_cpp/src/rosbag2_cpp/cache/message_cache_circular_buffer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ MessageCacheCircularBuffer::MessageCacheCircularBuffer(size_t max_cache_size)
3232

3333
bool MessageCacheCircularBuffer::push(CacheBufferInterface::buffer_element_t msg)
3434
{
35+
if (!msg || !msg->serialized_data) {
36+
ROSBAG2_CPP_LOG_ERROR("Attempted to push null message into circular buffer. Dropping message!");
37+
return false;
38+
}
39+
3540
// Drop message if it exceeds the buffer size
3641
if (msg->serialized_data->buffer_length > max_bytes_size_) {
37-
ROSBAG2_CPP_LOG_WARN_STREAM("Last message exceeds snapshot buffer size. Dropping message!");
42+
ROSBAG2_CPP_LOG_WARN("Last message exceeds snapshot buffer size. Dropping message!");
3843
return false;
3944
}
4045

rosbag2_cpp/test/rosbag2_cpp/test_circular_message_cache.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,12 @@ TEST_F(CircularMessageCacheTest, circular_message_cache_ensure_empty) {
133133
EXPECT_THAT(circular_message_cache->get_consumer_buffer()->size(), Eq(0u));
134134
circular_message_cache->release_consumer_buffer();
135135
}
136+
137+
TEST_F(CircularMessageCacheTest, circular_message_cache_rejects_null_message) {
138+
auto circular_message_cache_ = std::make_shared<rosbag2_cpp::cache::CircularMessageCache>(
139+
cache_size_);
140+
141+
bool result = true;
142+
ASSERT_NO_THROW(result = circular_message_cache_->push(nullptr));
143+
EXPECT_FALSE(result);
144+
}

0 commit comments

Comments
 (0)