Skip to content

Commit 15e3ab5

Browse files
Make DomainParticipant creation fail if MaxMessageSize is lower than PDP package size (#5473)
* Test RTPSParticipant creation fails if SHM MaxMessageSize is lower than minimum Signed-off-by: Eugenio Collado <[email protected]> * Check max message big enough for registering transport Signed-off-by: Eugenio Collado <[email protected]> * Update Copyright date Co-authored-by: Miguel Company <[email protected]> Signed-off-by: EugenioCollado <[email protected]> * Update Copyright date Co-authored-by: Miguel Company <[email protected]> Signed-off-by: EugenioCollado <[email protected]> --------- Signed-off-by: Eugenio Collado <[email protected]> Signed-off-by: EugenioCollado <[email protected]> Co-authored-by: Miguel Company <[email protected]>
1 parent 4e6a68f commit 15e3ab5

File tree

5 files changed

+122
-4
lines changed

5 files changed

+122
-4
lines changed

src/cpp/rtps/participant/RTPSParticipantImpl.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,25 @@ static bool get_unique_flows_parameters(
232232
return true;
233233
}
234234

235+
/**
236+
* @brief This method checks if the maximum message size is equal or higher than the PDP package size.
237+
* @return true if the maximum message size is equal or higher than the PDP package size, false otherwise.
238+
*/
239+
static bool is_max_message_size_big_enough(
240+
const uint32_t max_message_size)
241+
{
242+
constexpr uint32_t info_dst_message_length = 16;
243+
constexpr uint32_t info_ts_message_length = 12;
244+
uint32_t statistics_message_length = 0;
245+
#ifdef FASTDDS_STATISTICS
246+
statistics_message_length = eprosima::fastdds::statistics::rtps::statistics_submessage_length;
247+
#endif // FASTDDS_STATISTICS
248+
249+
return max_message_size >=
250+
(RTPSMESSAGE_HEADER_SIZE + BUILTIN_DATA_MAX_SIZE + info_dst_message_length +
251+
info_ts_message_length + statistics_message_length);
252+
}
253+
235254
Locator_t& RTPSParticipantImpl::applyLocatorAdaptRule(
236255
Locator_t& loc)
237256
{
@@ -482,6 +501,14 @@ bool RTPSParticipantImpl::setup_transports()
482501
register_transport = false;
483502
}
484503
}
504+
auto max_message_size = transportDescriptor->max_message_size();
505+
if (!is_max_message_size_big_enough(max_message_size))
506+
{
507+
EPROSIMA_LOG_ERROR(RTPS_PARTICIPANT,
508+
"User transport failed to register. Maximum message size needs to be equal or higher than "
509+
"the PDP package size.");
510+
register_transport = false;
511+
}
485512

486513
bool transport_registered = register_transport && m_network_Factory.RegisterTransport(
487514
transportDescriptor.get(), &m_att.properties, m_att.max_msg_size_no_frag);
@@ -497,12 +524,12 @@ bool RTPSParticipantImpl::setup_transports()
497524
if (transport_registered)
498525
{
499526
has_shm_transport_ |=
500-
(dynamic_cast<SharedMemTransportDescriptor*>(transportDescriptor.get()) != nullptr);
527+
(nullptr != dynamic_cast<SharedMemTransportDescriptor*>(transportDescriptor.get()));
501528
}
502529
else
503530
{
504531
// SHM transport could be disabled
505-
if ((dynamic_cast<SharedMemTransportDescriptor*>(transportDescriptor.get()) != nullptr))
532+
if ((nullptr != dynamic_cast<SharedMemTransportDescriptor*>(transportDescriptor.get())))
506533
{
507534
EPROSIMA_LOG_ERROR(RTPS_PARTICIPANT,
508535
"Unable to Register SHM Transport. SHM Transport is not supported in"

test/unittest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ if(NOT QNX)
4040
endif()
4141
add_subdirectory(rtps/history)
4242
add_subdirectory(rtps/network)
43+
add_subdirectory(rtps/participant)
4344
add_subdirectory(rtps/persistence)
4445
add_subdirectory(rtps/reader)
4546
add_subdirectory(rtps/resources/timedevent)

test/unittest/rtps/history/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ target_link_libraries(TopicPayloadPoolTests
167167
${CMAKE_DL_LIBS})
168168
gtest_discover_tests(TopicPayloadPoolTests)
169169

170-
171-
172170
add_executable(WriterHistoryTests ${WRITERHISTORYTESTS_SOURCE})
173171
target_compile_definitions(WriterHistoryTests PRIVATE
174172
BOOST_ASIO_STANDALONE
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2025 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
set(PARTICIPANTTESTS_SOURCE ParticipantTests.cpp)
16+
17+
add_executable(RTPSParticipantTests ${PARTICIPANTTESTS_SOURCE})
18+
target_compile_definitions(RTPSParticipantTests PRIVATE
19+
BOOST_ASIO_STANDALONE
20+
ASIO_STANDALONE
21+
$<$<AND:$<NOT:$<BOOL:${WIN32}>>,$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">>:__DEBUG>
22+
$<$<BOOL:${INTERNAL_DEBUG}>:__INTERNALDEBUG> # Internal debug activated.
23+
)
24+
target_link_libraries(RTPSParticipantTests fastcdr fastdds foonathan_memory
25+
fastcdr
26+
fastdds
27+
foonathan_memory
28+
GTest::gtest
29+
${CMAKE_DL_LIBS})
30+
gtest_discover_tests(RTPSParticipantTests)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2025 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <gtest/gtest.h>
16+
17+
#include <fastdds/rtps/RTPSDomain.hpp>
18+
#include <fastdds/rtps/participant/RTPSParticipant.hpp>
19+
20+
namespace eprosima {
21+
namespace fastdds {
22+
namespace rtps {
23+
24+
using namespace testing;
25+
26+
RTPSParticipant* transport_size_participant_init(
27+
uint32_t max_message_size)
28+
{
29+
uint32_t domain_id = 0;
30+
std::string max_message_size_str = std::to_string(max_message_size);
31+
32+
RTPSParticipantAttributes p_attr;
33+
BuiltinTransportsOptions options;
34+
options.maxMessageSize = max_message_size;
35+
p_attr.setup_transports(BuiltinTransports::SHM, options);
36+
RTPSParticipant* participant = RTPSDomain::createParticipant(
37+
domain_id, true, p_attr);
38+
39+
return participant;
40+
}
41+
42+
/**
43+
* This test checks that the participant is not created when the max message size is smaller than the PDP package size
44+
* but it is properly created when the max message size is bigger than the PDP package size.
45+
*/
46+
TEST(RTPSParticipantTests, participant_creation_message_size)
47+
{
48+
ASSERT_EQ(transport_size_participant_init(100), nullptr);
49+
ASSERT_NE(transport_size_participant_init(1000), nullptr);
50+
}
51+
52+
} // namespace rtps
53+
} // namespace fastdds
54+
} // namespace eprosima
55+
56+
int main(
57+
int argc,
58+
char** argv)
59+
{
60+
testing::InitGoogleTest(&argc, argv);
61+
return RUN_ALL_TESTS();
62+
}

0 commit comments

Comments
 (0)