Skip to content

Commit 1453ab9

Browse files
committed
Fix glob matching algo
There was one issue when you were using only `?`. I've added a circuit break that fixes that issue and also improve the perf of the algo in the case of patterns with no `*` and shorter than the length of the input string.
1 parent 96ecad3 commit 1453ab9

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

ext/ddshared.c

+9
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,19 @@ bool dd_glob_rule_matches(zval *pattern, zend_string* value) {
4242
char *s = ZSTR_VAL(value);
4343

4444
int wildcards = 0;
45+
int patternLength = 0;
46+
int stringLength = ZSTR_LEN(value);
4547
while (*p) {
4648
if (*(p++) == '*') {
4749
++wildcards;
4850
}
51+
patternLength++;
52+
}
53+
54+
// If there are no wildcards, no need to go through the whole string if pattern is shorter than the input string
55+
// Indeed wildcards (ie '*') can replace multiple characters while '?' canonly replace one
56+
if (wildcards == 0 && patternLength < stringLength) {
57+
return false;
4958
}
5059

5160
p = Z_STRVAL_P(pattern);

tests/ext/priority_sampling/021-rule-name-as-glob-with-question-mark.phpt

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ $tests = [
1212
["**fooname", "fooname", true],
1313
["*", "fooname", true],
1414
["???????", "fooname", true],
15+
["??????", "fooname", false],
16+
["??", "fooname", false],
1517
["*?", "fooname", true],
1618
["?*", "fooname", true],
1719
["f*o*e", "fooname", true],
@@ -61,6 +63,10 @@ As expected, * matches fooname (name)
6163
As expected, * matches fooname (service)
6264
As expected, ??????? matches fooname (name)
6365
As expected, ??????? matches fooname (service)
66+
As expected, ?????? doesn't match fooname (name)
67+
As expected, ?????? doesn't match fooname (service)
68+
As expected, ?? doesn't match fooname (name)
69+
As expected, ?? doesn't match fooname (service)
6470
As expected, *? matches fooname (name)
6571
As expected, *? matches fooname (service)
6672
As expected, ?* matches fooname (name)

0 commit comments

Comments
 (0)