Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic when fi is nil in walk callback #122

Merged
merged 1 commit into from
Mar 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err
return nil
}

var dir visitedDir
var (
dir visitedDir
isDir bool
)
if fi != nil {
isDir = fi.IsDir()
}

if includeMatcher != nil || excludeMatcher != nil {
for len(parentDirs) != 0 {
Expand All @@ -134,7 +140,7 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err
parentDirs = parentDirs[:len(parentDirs)-1]
}

if fi.IsDir() {
if isDir {
dir = visitedDir{
fi: fi,
path: path,
Expand All @@ -156,12 +162,12 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err
return errors.Wrap(err, "failed to match includepatterns")
}

if fi.IsDir() {
if isDir {
dir.includeMatchInfo = matchInfo
}

if !m {
if fi.IsDir() && onlyPrefixIncludes {
if isDir && onlyPrefixIncludes {
// Optimization: we can skip walking this dir if no include
// patterns could match anything inside it.
dirSlash := path + string(filepath.Separator)
Expand Down Expand Up @@ -191,12 +197,12 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err
return errors.Wrap(err, "failed to match excludepatterns")
}

if fi.IsDir() {
if isDir {
dir.excludeMatchInfo = matchInfo
}

if m {
if fi.IsDir() && onlyPrefixExcludeExceptions {
if isDir && onlyPrefixExcludeExceptions {
// Optimization: we can skip walking this dir if no
// exceptions to exclude patterns could match anything
// inside it.
Expand Down Expand Up @@ -230,7 +236,7 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err

if includeMatcher != nil || excludeMatcher != nil {
defer func() {
if fi.IsDir() {
if isDir {
parentDirs = append(parentDirs, dir)
}
}()
Expand Down