Skip to content

Commit 6cbdd58

Browse files
mergify[bot]MiguelCompanyemiliocuestafCopilot
authored
Allow empty partition list to match against "*" (#5989) (#6008)
* Allow empty partition list to match against `"*"` (#5989) * Refs #23623. Always use StringMatching. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #23623. Refactor StringMatching. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #23623. Regression unit test.. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #23623. Fix StringMatching on Windows. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #23624. Avoid unnecessary conditionals. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> --------- Signed-off-by: Miguel Company <miguelcompany@eprosima.com> (cherry picked from commit 27ce90d) # Conflicts: # src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp # src/cpp/utils/StringMatching.cpp * Fix backport issues Signed-off-by: Emilio Cuesta <emiliocuesta@eprosima.com> * Update src/cpp/utils/StringMatching.cpp Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Emilio Cuesta Fernandez <emiliocuesta@eprosima.com> --------- Signed-off-by: Emilio Cuesta <emiliocuesta@eprosima.com> Signed-off-by: Emilio Cuesta Fernandez <emiliocuesta@eprosima.com> Co-authored-by: Miguel Company <miguelcompany@eprosima.com> Co-authored-by: Emilio Cuesta <emiliocuesta@eprosima.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 71de149 commit 6cbdd58

File tree

3 files changed

+34
-66
lines changed

3 files changed

+34
-66
lines changed

src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ namespace rtps {
6767
using reader_map_helper = utilities::collections::map_size_helper<GUID_t, SubscriptionMatchedStatus>;
6868
using writer_map_helper = utilities::collections::map_size_helper<GUID_t, PublicationMatchedStatus>;
6969

70-
static bool is_partition_empty(
71-
const fastdds::dds::Partition_t& partition)
72-
{
73-
return partition.size() <= 1 && 0 == strlen(partition.name());
74-
}
75-
7670
EDP::EDP(
7771
PDP* p,
7872
RTPSParticipantImpl* part)
@@ -816,7 +810,7 @@ bool EDP::valid_matching(
816810
for (auto rnameit = rdata->m_qos.m_partition.begin();
817811
rnameit != rdata->m_qos.m_partition.end(); ++rnameit)
818812
{
819-
if (is_partition_empty(*rnameit))
813+
if (StringMatching::matchString("", rnameit->name()))
820814
{
821815
matched = true;
822816
break;
@@ -828,7 +822,7 @@ bool EDP::valid_matching(
828822
for (auto wnameit = wdata->m_qos.m_partition.begin();
829823
wnameit != wdata->m_qos.m_partition.end(); ++wnameit)
830824
{
831-
if (is_partition_empty(*wnameit))
825+
if (StringMatching::matchString(wnameit->name(), ""))
832826
{
833827
matched = true;
834828
break;

src/cpp/utils/StringMatching.cpp

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <algorithm>
2626
#include <regex>
2727
#elif defined(_WIN32)
28+
#include <cstring>
2829
#include "Shlwapi.h"
2930
#else
3031
#include <fnmatch.h>
@@ -46,7 +47,7 @@ StringMatching::~StringMatching()
4647
}
4748

4849
#if defined(__cplusplus_winrt)
49-
void replace_all(
50+
static void replace_all(
5051
std::string& subject,
5152
const std::string& search,
5253
const std::string& replace)
@@ -59,7 +60,7 @@ void replace_all(
5960
}
6061
}
6162

62-
bool StringMatching::matchPattern(
63+
static bool do_match_pattern(
6364
const char* pattern,
6465
const char* str)
6566
{
@@ -71,87 +72,52 @@ bool StringMatching::matchPattern(
7172

7273
std::regex path_regex(path);
7374
std::smatch spec_match;
74-
if (std::regex_match(spec, spec_match, path_regex))
75-
{
76-
return true;
77-
}
78-
79-
return false;
75+
return std::regex_match(spec, spec_match, path_regex);
8076
}
8177

82-
bool StringMatching::matchString(
83-
const char* str1,
84-
const char* str2)
78+
#elif defined(_WIN32)
79+
static bool do_match_pattern(
80+
const char* pattern,
81+
const char* str)
8582
{
86-
if (StringMatching::matchPattern(str1, str2))
83+
// An empty pattern only matches an empty string
84+
if (strlen(pattern) == 0)
8785
{
88-
return true;
86+
return strlen(str) == 0;
8987
}
90-
91-
if (StringMatching::matchPattern(str2, str1))
88+
// An empty string also matches a pattern of "*"
89+
if (strlen(str) == 0)
9290
{
93-
return true;
91+
return strcmp(pattern, "*") == 0;
9492
}
95-
96-
return false;
93+
// Leave rest of cases to PathMatchSpecA
94+
return PathMatchSpecA(str, pattern);
9795
}
9896

99-
#elif defined(_WIN32)
100-
bool StringMatching::matchPattern(
97+
#else
98+
static bool do_match_pattern(
10199
const char* pattern,
102100
const char* str)
103101
{
104-
if (PathMatchSpecA(str, pattern))
105-
{
106-
return true;
107-
}
108-
return false;
102+
return fnmatch(pattern, str, FNM_NOESCAPE) == 0;
109103
}
110104

111-
bool StringMatching::matchString(
112-
const char* str1,
113-
const char* str2)
114-
{
115-
if (PathMatchSpecA(str1, str2))
116-
{
117-
return true;
118-
}
119-
if (PathMatchSpecA(str2, str1))
120-
{
121-
return true;
122-
}
123-
return false;
124-
}
105+
#endif // if defined(__cplusplus_winrt)
125106

126-
#else
127107
bool StringMatching::matchPattern(
128108
const char* pattern,
129109
const char* str)
130110
{
131-
if (fnmatch(pattern, str, FNM_NOESCAPE) == 0)
132-
{
133-
return true;
134-
}
135-
return false;
111+
return do_match_pattern(pattern, str);
136112
}
137113

138114
bool StringMatching::matchString(
139115
const char* str1,
140116
const char* str2)
141117
{
142-
if (fnmatch(str1, str2, FNM_NOESCAPE) == 0)
143-
{
144-
return true;
145-
}
146-
if (fnmatch(str2, str1, FNM_NOESCAPE) == 0)
147-
{
148-
return true;
149-
}
150-
return false;
118+
return do_match_pattern(str1, str2) || do_match_pattern(str2, str1);
151119
}
152120

153-
#endif // if defined(__cplusplus_winrt)
154-
155121
} // namespace rtps
156-
} /* namespace rtps */
157-
} /* namespace eprosima */
122+
} // namespace fastrtps
123+
} // namespace eprosima

test/unittest/utils/StringMatchingTests.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ TEST_F(StringMatchingTests, patterns_with_wildcards)
6161
ASSERT_FALSE(StringMatching::matchString(path, pattern9));
6262
}
6363

64+
TEST_F(StringMatchingTests, empty_string)
65+
{
66+
ASSERT_TRUE(StringMatching::matchString("", ""));
67+
ASSERT_TRUE(StringMatching::matchString("", "*"));
68+
ASSERT_FALSE(StringMatching::matchString("", "?"));
69+
ASSERT_FALSE(StringMatching::matchString("", "a"));
70+
}
71+
6472

6573
int main(
6674
int argc,

0 commit comments

Comments
 (0)