mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
fileutils: Fix incorrect handling of "**/foo" pattern
(*PatternMatcher).Matches includes a special case for when the pattern matches a parent dir, even though it doesn't match the current path. However, it assumes that the parent dir which would match the pattern must have the same number of separators as the pattern itself. This doesn't hold true with a patern like "**/foo". A file foo/bar would have len(parentPathDirs) == 1, which is less than the number of path len(pattern.dirs) == 2... therefore this check would be skipped. Given that "**/foo" matches "foo", I think it's a bug that the "parent subdir matches" check is being skipped in this case. It seems safer to loop over the parent subdirs and check each against the pattern. It's possible there is a safe optimization to check only a certain subset, but the existing logic seems unsafe. Signed-off-by: Aaron Lehmann <alehmann@netflix.com>
This commit is contained in:
parent
12f1b3ce43
commit
90f8d1b675
2 changed files with 7 additions and 2 deletions
|
@ -77,8 +77,11 @@ func (pm *PatternMatcher) Matches(file string) (bool, error) {
|
||||||
|
|
||||||
if !match && parentPath != "." {
|
if !match && parentPath != "." {
|
||||||
// Check to see if the pattern matches one of our parent dirs.
|
// Check to see if the pattern matches one of our parent dirs.
|
||||||
if len(pattern.dirs) <= len(parentPathDirs) {
|
for i := range parentPathDirs {
|
||||||
match, _ = pattern.match(strings.Join(parentPathDirs[:len(pattern.dirs)], string(os.PathSeparator)))
|
match, _ = pattern.match(strings.Join(parentPathDirs[:i+1], string(os.PathSeparator)))
|
||||||
|
if match {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -328,6 +328,8 @@ func TestMatches(t *testing.T) {
|
||||||
{"dir/**", "dir/file/", true},
|
{"dir/**", "dir/file/", true},
|
||||||
{"dir/**", "dir/dir2/file", true},
|
{"dir/**", "dir/dir2/file", true},
|
||||||
{"dir/**", "dir/dir2/file/", true},
|
{"dir/**", "dir/dir2/file/", true},
|
||||||
|
{"**/dir", "dir", true},
|
||||||
|
{"**/dir", "dir/file", true},
|
||||||
{"**/dir2/*", "dir/dir2/file", true},
|
{"**/dir2/*", "dir/dir2/file", true},
|
||||||
{"**/dir2/*", "dir/dir2/file/", true},
|
{"**/dir2/*", "dir/dir2/file/", true},
|
||||||
{"**/dir2/**", "dir/dir2/dir3/file", true},
|
{"**/dir2/**", "dir/dir2/dir3/file", true},
|
||||||
|
|
Loading…
Reference in a new issue