Skip to content

Commit 82d66cd

Browse files
authored
Fix single-char matches on phrase_match (#462)
1 parent b9b159e commit 82d66cd

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/matcher/phrase_match.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ std::pair<bool, dynamic_string> phrase_match::match_impl(std::string_view patter
6464
auto begin = static_cast<std::size_t>(result.match_begin);
6565
auto end = static_cast<std::size_t>(result.match_end);
6666

67-
if (result.match_begin < 0 || result.match_end < 0 || begin >= end ||
67+
if (result.match_begin < 0 || result.match_end < 0 ||
6868
(enforce_word_boundary_ && !is_bounded_word(pattern, begin, end))) {
6969
return {false, {}};
7070
}

tests/unit/matcher/phrase_match_test.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,30 @@ TEST(TestPhraseMatch, TestInvalidInput)
191191
EXPECT_FALSE(matcher.match(std::string_view{"*", 0}).first);
192192
}
193193

194+
TEST(TestPhraseMatch, TestSingleCharMatch)
195+
{
196+
std::vector<const char *> strings{"a", "1"};
197+
std::vector<uint32_t> lengths{1, 1};
198+
199+
phrase_match matcher(strings, lengths);
200+
201+
EXPECT_STR(matcher.name(), "phrase_match");
202+
EXPECT_STR(matcher.to_string(), "");
203+
204+
ddwaf_object param;
205+
ddwaf_object_string(&param, "a");
206+
207+
auto [res, highlight] = matcher.match(param);
208+
EXPECT_TRUE(res);
209+
EXPECT_STR(highlight, "a");
210+
211+
ddwaf_object param2;
212+
ddwaf_object_string(&param2, "2");
213+
214+
EXPECT_FALSE(matcher.match(param2).first);
215+
216+
ddwaf_object_free(&param2);
217+
ddwaf_object_free(&param);
218+
}
219+
194220
} // namespace

0 commit comments

Comments
 (0)