Skip to content

Commit 6b343cf

Browse files
committed
Fix two bugs in wildcard match
Two or more consecutive * characters were not handled. A fix was on the web site this code comes from. Before calling wildMatch recursively with "str + 1", the code did not check whether *str was zero, in which case the "str + 1" is illegal. This also changes the names of the function parameters which were not consistent in .hpp and .cpp files.
1 parent da5a1d5 commit 6b343cf

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

src/wildcmp.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,37 @@
1515
* Case sensitive wild card match with a string.
1616
* * matches any string or no character.
1717
* ? matches any single character.
18-
* anything else etc must match the character exactly.
18+
* anything else must match the character exactly.
1919
*
2020
* Returns if a match was found.
2121
*/
22-
bool wildMatch(char const *first, char const *second)
22+
bool wildMatch(char const *expr, char const *str) noexcept
2323
{
24-
// Code borrowed from
24+
// Code based on
2525
// http://www.geeksforgeeks.org/wildcard-character-matching/
26-
if (*first == '\0' && *second == '\0') {
26+
if (*expr == '\0' && *str == '\0') {
2727
return true;
2828
}
2929

30-
if (*first == '*' && *(first + 1) != '\0' && *second == '\0') {
30+
if (*expr == '*') {
31+
while (*(expr + 1) == '*') {
32+
++expr;
33+
}
34+
}
35+
36+
if (*expr == '*' && *(expr + 1) != '\0' && *str == '\0') {
3137
return false;
3238
}
3339

34-
if (*first == '?' || *first == *second) {
35-
return wildMatch(first + 1, second + 1);
40+
if (*expr == '?' || *expr == *str) {
41+
if (*str == '\0') {
42+
return false;
43+
}
44+
return wildMatch(expr + 1, str + 1);
3645
}
3746

38-
if (*first == '*') {
39-
return wildMatch(first + 1, second) || wildMatch(first, second + 1);
47+
if (*expr == '*') {
48+
return wildMatch(expr + 1, str) || wildMatch(expr, str + 1);
4049
}
4150

4251
return false;

src/wildcmp.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
* For a full list of authors see the git log.
1111
*/
1212

13-
bool wildMatch(char const *wildCard, char const *string);
13+
bool wildMatch(char const *expr, char const *str) noexcept;
1414

1515
#endif // OSM2PGSQL_WILDCMP_HPP

tests/test-wildcard-match.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ TEST_CASE("Wildcard matching", "[NoDB]")
1717
CHECK_FALSE(wildMatch("fhwieurwe", "fhwieurw"));
1818
CHECK_FALSE(wildMatch("fhwieurw", "fhwieurwe"));
1919
CHECK(wildMatch("*", "foo"));
20+
CHECK(wildMatch("**", "foo"));
2021
CHECK_FALSE(wildMatch("r*", "foo"));
2122
CHECK(wildMatch("r*", "roo"));
2223
CHECK(wildMatch("*bar", "Hausbar"));
2324
CHECK_FALSE(wildMatch("*bar", "Haustar"));
2425
CHECK(wildMatch("*", ""));
26+
CHECK(wildMatch("**", ""));
2527
CHECK(wildMatch("kin*la", "kinla"));
2628
CHECK(wildMatch("kin*la", "kinLLla"));
2729
CHECK(wildMatch("kin*la", "kinlalalala"));
30+
CHECK(wildMatch("kin**la", "kinlalalala"));
2831
CHECK_FALSE(wildMatch("kin*la", "kinlaa"));
2932
CHECK_FALSE(wildMatch("kin*la", "ki??laa"));
3033
CHECK(wildMatch("1*2*3", "123"));
@@ -35,4 +38,6 @@ TEST_CASE("Wildcard matching", "[NoDB]")
3538
CHECK_FALSE(wildMatch("bo??f", "boxf"));
3639
CHECK(wildMatch("?5?", "?5?"));
3740
CHECK(wildMatch("?5?", "x5x"));
41+
CHECK_FALSE(wildMatch("?abc", ""));
42+
CHECK_FALSE(wildMatch("?", ""));
3843
}

0 commit comments

Comments
 (0)