From 819f9de59aeaf00379e82152202aa19b5359e8ad Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Wed, 3 Sep 2025 14:12:43 +0200 Subject: [PATCH 1/3] Allow empty partition list to match against `"*"` (#5989) * Refs #23623. Always use StringMatching. Signed-off-by: Miguel Company * Refs #23623. Refactor StringMatching. Signed-off-by: Miguel Company * Refs #23623. Regression unit test.. Signed-off-by: Miguel Company * Refs #23623. Fix StringMatching on Windows. Signed-off-by: Miguel Company * Refs #23624. Avoid unnecessary conditionals. Signed-off-by: Miguel Company --------- Signed-off-by: Miguel Company (cherry picked from commit 27ce90d95dfdd3b0b70af28ca2194e73a05c98c2) # Conflicts: # src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp # src/cpp/utils/StringMatching.cpp --- .../rtps/builtin/discovery/endpoint/EDP.cpp | 15 +++- src/cpp/utils/StringMatching.cpp | 83 ++++++------------- test/unittest/utils/StringMatchingTests.cpp | 8 ++ 3 files changed, 47 insertions(+), 59 deletions(-) diff --git a/src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp b/src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp index 53ebb47275b..fa04f914201 100644 --- a/src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp +++ b/src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp @@ -67,10 +67,21 @@ namespace rtps { using reader_map_helper = utilities::collections::map_size_helper; using writer_map_helper = utilities::collections::map_size_helper; +<<<<<<< HEAD static bool is_partition_empty( const fastdds::dds::Partition_t& partition) { return partition.size() <= 1 && 0 == strlen(partition.name()); +======= +static bool is_same_type( + const dds::xtypes::TypeInformation& t1, + const dds::xtypes::TypeInformation& t2) +{ + return (dds::xtypes::TK_NONE != t1.complete().typeid_with_size().type_id()._d() + && t1.complete().typeid_with_size() == t2.complete().typeid_with_size()) + || (dds::xtypes::TK_NONE != t1.minimal().typeid_with_size().type_id()._d() + && t1.minimal().typeid_with_size() == t2.minimal().typeid_with_size()); +>>>>>>> 27ce90d9 (Allow empty partition list to match against `"*"` (#5989)) } EDP::EDP( @@ -816,7 +827,7 @@ bool EDP::valid_matching( for (auto rnameit = rdata->m_qos.m_partition.begin(); rnameit != rdata->m_qos.m_partition.end(); ++rnameit) { - if (is_partition_empty(*rnameit)) + if (StringMatching::matchString("", rnameit->name())) { matched = true; break; @@ -828,7 +839,7 @@ bool EDP::valid_matching( for (auto wnameit = wdata->m_qos.m_partition.begin(); wnameit != wdata->m_qos.m_partition.end(); ++wnameit) { - if (is_partition_empty(*wnameit)) + if (StringMatching::matchString(wnameit->name(), "")) { matched = true; break; diff --git a/src/cpp/utils/StringMatching.cpp b/src/cpp/utils/StringMatching.cpp index 4e254a9297a..c29213be1c7 100644 --- a/src/cpp/utils/StringMatching.cpp +++ b/src/cpp/utils/StringMatching.cpp @@ -25,6 +25,7 @@ #include #include #elif defined(_WIN32) +#include #include "Shlwapi.h" #else #include @@ -46,7 +47,7 @@ StringMatching::~StringMatching() } #if defined(__cplusplus_winrt) -void replace_all( +static void replace_all( std::string& subject, const std::string& search, const std::string& replace) @@ -59,7 +60,7 @@ void replace_all( } } -bool StringMatching::matchPattern( +static bool do_match_pattern( const char* pattern, const char* str) { @@ -71,87 +72,55 @@ bool StringMatching::matchPattern( std::regex path_regex(path); std::smatch spec_match; - if (std::regex_match(spec, spec_match, path_regex)) - { - return true; - } - - return false; + return std::regex_match(spec, spec_match, path_regex); } -bool StringMatching::matchString( - const char* str1, - const char* str2) +#elif defined(_WIN32) +static bool do_match_pattern( + const char* pattern, + const char* str) { - if (StringMatching::matchPattern(str1, str2)) + // An empty pattern only matches an empty string + if (strlen(pattern) == 0) { - return true; + return strlen(str) == 0; } - - if (StringMatching::matchPattern(str2, str1)) + // An empty string also matches a pattern of "*" + if (strlen(str) == 0) { - return true; + return strcmp(pattern, "*") == 0; } - - return false; + // Leave rest of cases to PathMatchSpecA + return PathMatchSpecA(str, pattern); } -#elif defined(_WIN32) -bool StringMatching::matchPattern( +#else +static bool do_match_pattern( const char* pattern, const char* str) { - if (PathMatchSpecA(str, pattern)) - { - return true; - } - return false; + return fnmatch(pattern, str, FNM_NOESCAPE) == 0; } -bool StringMatching::matchString( - const char* str1, - const char* str2) -{ - if (PathMatchSpecA(str1, str2)) - { - return true; - } - if (PathMatchSpecA(str2, str1)) - { - return true; - } - return false; -} +#endif // if defined(__cplusplus_winrt) -#else +<<<<<<< HEAD +} // namespace rtps +======= bool StringMatching::matchPattern( const char* pattern, const char* str) { - if (fnmatch(pattern, str, FNM_NOESCAPE) == 0) - { - return true; - } - return false; + return do_match_pattern(pattern, str); } bool StringMatching::matchString( const char* str1, const char* str2) { - if (fnmatch(str1, str2, FNM_NOESCAPE) == 0) - { - return true; - } - if (fnmatch(str2, str1, FNM_NOESCAPE) == 0) - { - return true; - } - return false; + return do_match_pattern(str1, str2) || do_match_pattern(str2, str1); } -#endif // if defined(__cplusplus_winrt) - -} // namespace rtps +>>>>>>> 27ce90d9 (Allow empty partition list to match against `"*"` (#5989)) } /* namespace rtps */ } /* namespace eprosima */ diff --git a/test/unittest/utils/StringMatchingTests.cpp b/test/unittest/utils/StringMatchingTests.cpp index 736b59b2ddd..a0d1d355db3 100644 --- a/test/unittest/utils/StringMatchingTests.cpp +++ b/test/unittest/utils/StringMatchingTests.cpp @@ -61,6 +61,14 @@ TEST_F(StringMatchingTests, patterns_with_wildcards) ASSERT_FALSE(StringMatching::matchString(path, pattern9)); } +TEST_F(StringMatchingTests, empty_string) +{ + ASSERT_TRUE(StringMatching::matchString("", "")); + ASSERT_TRUE(StringMatching::matchString("", "*")); + ASSERT_FALSE(StringMatching::matchString("", "?")); + ASSERT_FALSE(StringMatching::matchString("", "a")); +} + int main( int argc, From 4ee9cd6c7c04a745b80151c3c57b1a6b65633cf9 Mon Sep 17 00:00:00 2001 From: Emilio Cuesta Date: Mon, 20 Oct 2025 14:23:43 +0200 Subject: [PATCH 2/3] Fix backport issues Signed-off-by: Emilio Cuesta --- src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp | 17 ----------------- src/cpp/utils/StringMatching.cpp | 5 +---- 2 files changed, 1 insertion(+), 21 deletions(-) diff --git a/src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp b/src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp index fa04f914201..fb6c4ed2747 100644 --- a/src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp +++ b/src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp @@ -67,23 +67,6 @@ namespace rtps { using reader_map_helper = utilities::collections::map_size_helper; using writer_map_helper = utilities::collections::map_size_helper; -<<<<<<< HEAD -static bool is_partition_empty( - const fastdds::dds::Partition_t& partition) -{ - return partition.size() <= 1 && 0 == strlen(partition.name()); -======= -static bool is_same_type( - const dds::xtypes::TypeInformation& t1, - const dds::xtypes::TypeInformation& t2) -{ - return (dds::xtypes::TK_NONE != t1.complete().typeid_with_size().type_id()._d() - && t1.complete().typeid_with_size() == t2.complete().typeid_with_size()) - || (dds::xtypes::TK_NONE != t1.minimal().typeid_with_size().type_id()._d() - && t1.minimal().typeid_with_size() == t2.minimal().typeid_with_size()); ->>>>>>> 27ce90d9 (Allow empty partition list to match against `"*"` (#5989)) -} - EDP::EDP( PDP* p, RTPSParticipantImpl* part) diff --git a/src/cpp/utils/StringMatching.cpp b/src/cpp/utils/StringMatching.cpp index c29213be1c7..fd67cfe3f22 100644 --- a/src/cpp/utils/StringMatching.cpp +++ b/src/cpp/utils/StringMatching.cpp @@ -104,9 +104,6 @@ static bool do_match_pattern( #endif // if defined(__cplusplus_winrt) -<<<<<<< HEAD -} // namespace rtps -======= bool StringMatching::matchPattern( const char* pattern, const char* str) @@ -121,6 +118,6 @@ bool StringMatching::matchString( return do_match_pattern(str1, str2) || do_match_pattern(str2, str1); } ->>>>>>> 27ce90d9 (Allow empty partition list to match against `"*"` (#5989)) } /* namespace rtps */ +} /* namespace fastrtps */ } /* namespace eprosima */ From d0eae7adabba0fb18cedd196c9b2983c9dbccb13 Mon Sep 17 00:00:00 2001 From: Emilio Cuesta Fernandez Date: Tue, 21 Oct 2025 09:42:40 +0200 Subject: [PATCH 3/3] Update src/cpp/utils/StringMatching.cpp Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Emilio Cuesta Fernandez --- src/cpp/utils/StringMatching.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cpp/utils/StringMatching.cpp b/src/cpp/utils/StringMatching.cpp index fd67cfe3f22..844a8e683fc 100644 --- a/src/cpp/utils/StringMatching.cpp +++ b/src/cpp/utils/StringMatching.cpp @@ -118,6 +118,6 @@ bool StringMatching::matchString( return do_match_pattern(str1, str2) || do_match_pattern(str2, str1); } -} /* namespace rtps */ -} /* namespace fastrtps */ -} /* namespace eprosima */ +} // namespace rtps +} // namespace fastrtps +} // namespace eprosima