From 4d94de2751d0d7e6bea89952af3b70c915dcdf1a Mon Sep 17 00:00:00 2001 From: PengZheng Date: Mon, 1 Apr 2024 15:08:21 +0800 Subject: [PATCH] gh-674: Fix broken substring match. It also replaces celix_utils_stringEquals with cheaper celix_utils_isStringNullOrEmpty. --- libs/utils/gtest/src/FilterTestSuite.cc | 8 ++++++++ libs/utils/src/filter.c | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/libs/utils/gtest/src/FilterTestSuite.cc b/libs/utils/gtest/src/FilterTestSuite.cc index 8f7d07b4f..ccb32257d 100644 --- a/libs/utils/gtest/src/FilterTestSuite.cc +++ b/libs/utils/gtest/src/FilterTestSuite.cc @@ -751,6 +751,14 @@ TEST_F(FilterTestSuite, SubStringWithArrayAttributesTest) { celix_autoptr(celix_filter_t) filter3 = celix_filter_create("(strings=*Johnson)"); EXPECT_TRUE(filter3 != nullptr); EXPECT_FALSE(celix_filter_match(filter3, props)); + + celix_autoptr(celix_filter_t) filter4 = celix_filter_create("(strings=Jane*)"); + EXPECT_TRUE(filter4 != nullptr); + EXPECT_TRUE(celix_filter_match(filter4, props)); + + celix_autoptr(celix_filter_t) filter5 = celix_filter_create("(strings=*Smith)"); + EXPECT_TRUE(filter5 != nullptr); + EXPECT_TRUE(celix_filter_match(filter5, props)); } #include "filter.h" diff --git a/libs/utils/src/filter.c b/libs/utils/src/filter.c index 3f1386736..fe776aa19 100644 --- a/libs/utils/src/filter.c +++ b/libs/utils/src/filter.c @@ -685,7 +685,7 @@ static bool celix_filter_matchSubStringForValue(const celix_filter_t* filter, co const char* currentValue = value; - if (initial) { + if (!celix_utils_isStringNullOrEmpty(initial)) { const char* found = strstr(value, initial); currentValue = found + celix_utils_strlen(initial); if (!found || found != value) { @@ -702,7 +702,7 @@ static bool celix_filter_matchSubStringForValue(const celix_filter_t* filter, co currentValue = found + celix_utils_strlen(substr); } - if (!celix_utils_stringEquals(final, "")) { + if (!celix_utils_isStringNullOrEmpty(final)) { const char* found = strstr(currentValue, final); if (!found || found + celix_utils_strlen(final) != value + strLen) { return false; @@ -719,8 +719,8 @@ static bool celix_filter_matchSubString(const celix_filter_t* filter, const celi if (celix_filter_matchSubStringForValue(filter, substr)) { return true; } - return false; } + return false; } return celix_filter_matchSubStringForValue(filter, entry->value); }