Skip to content

Commit 98fb8fb

Browse files
authored
fix(glob): Correctly match longer strings with '*' or '**' pattern (#5001)
* fix(glob): Correctly match longer strings with '*' or '**' pattern If string is longer than 16 chars we are using reflex library for matching. When used pattern is '*' or '**' we are going to remove trailing and leading star and have empty pattern. We should, in this, edge case set manually star pattern. Fixes #4948 Signed-off-by: mkaruza <mario@dragonflydb.io> * Core matching test --------- Signed-off-by: mkaruza <mario@dragonflydb.io>
1 parent 23e6db9 commit 98fb8fb

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/core/dfly_core_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ TEST_F(StringMatchTest, Basic) {
155155

156156
// Wildcards
157157
EXPECT_EQ(MatchLen("*", "hello", 0), 1);
158+
EXPECT_EQ(MatchLen("*", "1234567890123456", 0), 1);
158159
EXPECT_EQ(MatchLen("h*", "hello", 0), 1);
159160
EXPECT_EQ(MatchLen("h*", "abc", 0), 0);
160161
EXPECT_EQ(MatchLen("h*o", "hello", 0), 1);

src/core/glob_matcher.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,11 @@ GlobMatcher::GlobMatcher(string_view pattern, bool case_sensitive)
236236
regex.push_back('i');
237237
}
238238
regex.push_back(')');
239-
regex.append(Glob2Regex(pattern));
239+
if (pattern.empty()) {
240+
regex.append(Glob2Regex("*"));
241+
} else {
242+
regex.append(Glob2Regex(pattern));
243+
}
240244
matcher_.pattern(regex);
241245
#elif USE_PCRE2
242246
string regex("(?s"); // dotall mode

src/server/dragonfly_test.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,23 @@ TEST_F(DflyEngineTest, PSubscribe) {
548548
EXPECT_EQ("a*", msg.pattern);
549549
}
550550

551+
TEST_F(DflyEngineTest, PSubscribeMatchOnlyStar) {
552+
single_response_ = false;
553+
auto resp = pp_->at(1)->Await([&] { return Run({"psubscribe", "*"}); });
554+
EXPECT_THAT(resp, ArrLen(3));
555+
resp = pp_->at(0)->Await([&] { return Run({"PUBLISH", "1234567890123456", "abc"}); });
556+
EXPECT_THAT(resp, IntArg(1));
557+
558+
pp_->AwaitFiberOnAll([](ProactorBase* pb) {});
559+
560+
ASSERT_EQ(1, SubscriberMessagesLen("IO1"));
561+
562+
const auto& msg = GetPublishedMessage("IO1", 0);
563+
EXPECT_EQ("abc", msg.message);
564+
EXPECT_EQ("1234567890123456", msg.channel);
565+
EXPECT_EQ("*", msg.pattern);
566+
}
567+
551568
TEST_F(DflyEngineTest, Unsubscribe) {
552569
auto resp = Run({"unsubscribe", "a"});
553570
EXPECT_THAT(resp.GetVec(), ElementsAre("unsubscribe", "a", IntArg(0)));

0 commit comments

Comments
 (0)