From 5205109516381bc54cb3fb18e9b7a9b3811ef721 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Mon, 1 Sep 2025 12:51:06 +0200 Subject: [PATCH 1/5] Refs #23623. Always use StringMatching. Signed-off-by: Miguel Company --- src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp b/src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp index 23af66d43b0..afe7ea05a68 100644 --- a/src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp +++ b/src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp @@ -67,12 +67,6 @@ namespace rtps { using reader_map_helper = utilities::collections::map_size_helper; using writer_map_helper = utilities::collections::map_size_helper; -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) @@ -988,7 +982,7 @@ bool EDP::valid_matching( for (auto rnameit = rdata->partition.begin(); rnameit != rdata->partition.end(); ++rnameit) { - if (is_partition_empty(*rnameit)) + if (StringMatching::matchString("", rnameit->name())) { matched = true; break; @@ -1000,7 +994,7 @@ bool EDP::valid_matching( for (auto wnameit = wdata->partition.begin(); wnameit != wdata->partition.end(); ++wnameit) { - if (is_partition_empty(*wnameit)) + if (StringMatching::matchString(wnameit->name(), "")) { matched = true; break; From 95a356447ca0e9adb998f91784b5edff0f72967b Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 2 Sep 2025 11:17:57 +0200 Subject: [PATCH 2/5] Refs #23623. Refactor StringMatching. Signed-off-by: Miguel Company --- src/cpp/utils/StringMatching.cpp | 57 +++++++------------------------- 1 file changed, 12 insertions(+), 45 deletions(-) diff --git a/src/cpp/utils/StringMatching.cpp b/src/cpp/utils/StringMatching.cpp index cb8cd28ecc2..a56f8ecebd9 100644 --- a/src/cpp/utils/StringMatching.cpp +++ b/src/cpp/utils/StringMatching.cpp @@ -46,7 +46,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 +59,7 @@ void replace_all( } } -bool StringMatching::matchPattern( +static bool do_match_pattern( const char* pattern, const char* str) { @@ -79,25 +79,8 @@ bool StringMatching::matchPattern( return false; } -bool StringMatching::matchString( - const char* str1, - const char* str2) -{ - if (StringMatching::matchPattern(str1, str2)) - { - return true; - } - - if (StringMatching::matchPattern(str2, str1)) - { - return true; - } - - return false; -} - #elif defined(_WIN32) -bool StringMatching::matchPattern( +static bool do_match_pattern( const char* pattern, const char* str) { @@ -108,50 +91,34 @@ bool StringMatching::matchPattern( return false; } -bool StringMatching::matchString( - const char* str1, - const char* str2) +#else +static bool do_match_pattern( + const char* pattern, + const char* str) { - if (PathMatchSpecA(str1, str2)) - { - return true; - } - if (PathMatchSpecA(str2, str1)) + if (fnmatch(pattern, str, FNM_NOESCAPE) == 0) { return true; } return false; } -#else +#endif // if defined(__cplusplus_winrt) + 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 */ } /* namespace fastdds */ } /* namespace eprosima */ From 95923fbc655ffc4b24e520be588eb4955b402308 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 2 Sep 2025 11:26:51 +0200 Subject: [PATCH 3/5] Refs #23623. Regression unit test.. Signed-off-by: Miguel Company --- test/unittest/utils/StringMatchingTests.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/unittest/utils/StringMatchingTests.cpp b/test/unittest/utils/StringMatchingTests.cpp index 3ef84302476..1f728c912b6 100644 --- a/test/unittest/utils/StringMatchingTests.cpp +++ b/test/unittest/utils/StringMatchingTests.cpp @@ -62,6 +62,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 9bdd3317ce64b84f34ecd4403f4ada6c3d33bf00 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 2 Sep 2025 11:45:38 +0200 Subject: [PATCH 4/5] Refs #23623. Fix StringMatching on Windows. Signed-off-by: Miguel Company --- src/cpp/utils/StringMatching.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/cpp/utils/StringMatching.cpp b/src/cpp/utils/StringMatching.cpp index a56f8ecebd9..429139bf89e 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 @@ -84,11 +85,18 @@ static bool do_match_pattern( const char* pattern, const char* str) { - if (PathMatchSpecA(str, pattern)) + // An empty pattern only matches an empty string + if (strlen(pattern) == 0) { - return true; + return strlen(str) == 0; } - return false; + // An empty string also matches a pattern of "*" + if (strlen(str) == 0) + { + return strcmp(pattern, "*") == 0; + } + // Leave rest of cases to PathMatchSpecA + return PathMatchSpecA(str, pattern); } #else From 82f47b66bc5cc4ee20ddb165e664c93b9a940e5d Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 2 Sep 2025 16:29:26 +0200 Subject: [PATCH 5/5] Refs #23624. Avoid unnecessary conditionals. Signed-off-by: Miguel Company --- src/cpp/utils/StringMatching.cpp | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/cpp/utils/StringMatching.cpp b/src/cpp/utils/StringMatching.cpp index 429139bf89e..2df1815c6ee 100644 --- a/src/cpp/utils/StringMatching.cpp +++ b/src/cpp/utils/StringMatching.cpp @@ -72,12 +72,7 @@ static bool do_match_pattern( 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); } #elif defined(_WIN32) @@ -104,11 +99,7 @@ static bool do_match_pattern( const char* pattern, const char* str) { - if (fnmatch(pattern, str, FNM_NOESCAPE) == 0) - { - return true; - } - return false; + return fnmatch(pattern, str, FNM_NOESCAPE) == 0; } #endif // if defined(__cplusplus_winrt)