Skip to content

Commit 399228b

Browse files
gurgundayaduh95
authored andcommitted
fs,path: fix glob globstar section bounds
The last start index of each globstar-delimited body section was computed from the non-globstar prefix counts in reverse order. When sections have different lengths, the bound is too tight, so paths such as `a/b/c/d/e` failed to match `**/a/b/c/**/d/**`. This affected `path.matchesGlob()` and `exclude` patterns in `fs.glob()`. Compute the bound from the number of non-globstar parts in the section and every section after it. Signed-off-by: Gürgün Dayıoğlu <hey@gurgun.day> PR-URL: #66738 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Efe Karasakal <hi@efe.dev>
1 parent 50aa231 commit 399228b

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

‎src/glob/glob_matcher.cc‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -673,12 +673,9 @@ class PathMatcher {
673673
}
674674
const ptrdiff_t file_length = static_cast<ptrdiff_t>(file.size()) -
675675
static_cast<ptrdiff_t>(file_tail_match);
676-
{
677-
size_t i = sections.size() - 1;
678-
for (Section& s : sections) {
679-
s.after = file_length - (static_cast<ptrdiff_t>(sections[i--].non_gs) +
680-
static_cast<ptrdiff_t>(s.end - s.begin));
681-
}
676+
// leave room for this section and every section after it
677+
for (Section& s : sections) {
678+
s.after = file_length - static_cast<ptrdiff_t>(non_gs - s.non_gs);
682679
}
683680
return MatchBodySections(
684681
file, pattern, sections, fi, 0, 0, file_tail_match != 0);

‎test/parallel/test-path-glob.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const globs = {
1616
['foo\\bar\\baz\\boo', 'foo\\[bc-r]ar\\baz\\*', true], // Matches 'bar' or 'car' in 'foo\\bar'
1717
['foo\\bar\\baz', 'foo/**', true], // Matches anything in 'foo'
1818
['foo\\bar\\baz', '*', false], // No match
19+
['a\\b\\c\\d\\e', '**/a/b/c/**/d/**', true], // Globstar sections of different lengths
20+
['x\\a\\b\\c\\d\\e', '**/a/b/c/**/d/**/e', true], // Globstar sections of different lengths
21+
['a\\b\\c\\d', '**/a/b/c/**/d/**', false], // Trailing globstar needs a segment
1922
],
2023
posix: [
2124
['foo/bar/baz', 'foo/[bcr]ar/baz', true], // Matches 'bar' or 'car' in 'foo/bar'
@@ -28,6 +31,9 @@ const globs = {
2831
['foo/bar/baz/boo', 'foo/[bc-r]ar/baz/*', true], // Matches 'bar' or 'car' in 'foo/bar'
2932
['foo/bar/baz', 'foo/**', true], // Matches anything in 'foo'
3033
['foo/bar/baz', '*', false], // No match
34+
['a/b/c/d/e', '**/a/b/c/**/d/**', true], // Globstar sections of different lengths
35+
['x/a/b/c/d/e', '**/a/b/c/**/d/**/e', true], // Globstar sections of different lengths
36+
['a/b/c/d', '**/a/b/c/**/d/**', false], // Trailing globstar needs a segment
3137
],
3238
};
3339

0 commit comments

Comments
 (0)