From e77cc9a8e84fcecc97ba9fcce5f5ae8a99f35b88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Gonz=C3=A1lez?= Date: Thu, 22 Jul 2021 07:34:31 +0200 Subject: [PATCH] Unprotected code loaning samples (#2086) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refs #11831. Fix unprotected code accessing writer pool Signed-off-by: Ricardo González * Refs #11831. Fix wrong test testing dirty payloads Signed-off-by: Ricardo González Moreno --- src/cpp/fastdds/publisher/DataWriterImpl.cpp | 4 ++++ .../common/DDSBlackboxTestsDataSharing.cpp | 19 +++++-------------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/cpp/fastdds/publisher/DataWriterImpl.cpp b/src/cpp/fastdds/publisher/DataWriterImpl.cpp index 52c55cbda04..85b5c96e2f6 100644 --- a/src/cpp/fastdds/publisher/DataWriterImpl.cpp +++ b/src/cpp/fastdds/publisher/DataWriterImpl.cpp @@ -391,6 +391,8 @@ ReturnCode_t DataWriterImpl::loan_sample( return ReturnCode_t::RETCODE_NOT_ENABLED; } + std::lock_guard lock(writer_->getMutex()); + // Get one payload from the pool PayloadInfo_t payload; uint32_t size = type_->m_typeSize; @@ -466,6 +468,8 @@ ReturnCode_t DataWriterImpl::discard_loan( return ReturnCode_t::RETCODE_NOT_ENABLED; } + std::lock_guard lock(writer_->getMutex()); + // Remove sample from loans collection PayloadInfo_t payload; if ((nullptr == sample) || !check_and_remove_loan(sample, payload)) diff --git a/test/blackbox/common/DDSBlackboxTestsDataSharing.cpp b/test/blackbox/common/DDSBlackboxTestsDataSharing.cpp index 5a019e0df0a..d283e9f5cd4 100644 --- a/test/blackbox/common/DDSBlackboxTestsDataSharing.cpp +++ b/test/blackbox/common/DDSBlackboxTestsDataSharing.cpp @@ -201,23 +201,14 @@ TEST(DDSDataSharing, ReliableDirtyPayloads) std::advance(data_it, writer_sent_data - writer_history_depth - 1); std::copy(data_it, data.end(), std::back_inserter(valid_data)); + writer.send(data, 100); + ASSERT_TRUE(data.empty()); + // Send the data to fill the history and overwrite old changes // The reader will receive and process all changes so that the writer can reuse them, // but will keep them in the history. - read_reader.startReception(data); - writer.send(data, 100); - ASSERT_TRUE(data.empty()); + read_reader.startReception(valid_data); read_reader.block_for_all(); - - // Doing a second read on the same history, the application will see only the last samples - while (!valid_data.empty()) - { - FixedSized value; - ASSERT_TRUE(read_reader.take_first_data(&value)); - ASSERT_EQ(valid_data.front(), value); - valid_data.pop_front(); - } - ASSERT_TRUE(valid_data.empty()); } TEST(DDSDataSharing, DataSharingWriter_DifferentDomainReaders) @@ -497,4 +488,4 @@ TEST(DDSDataSharing, DataSharingPoolError) writer_auto.send(data); ASSERT_TRUE(data.empty()); reader.block_for_all(); -} \ No newline at end of file +}