Fix nil pointer dereference in directory resolver's indexPath method (#924)

* Add failing test for dir resolver panic

Signed-off-by: Dan Luhring <dan+github@luhrings.com>

* Fix panic

Signed-off-by: Dan Luhring <dan+github@luhrings.com>
This commit is contained in:
Dan Luhring 2022-03-28 13:15:09 -04:00 committed by GitHub
parent 5549939cc6
commit 028cd9e27e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 6 deletions

View File

@ -132,10 +132,15 @@ func (r *directoryResolver) indexTree(root string, stager *progress.Stage) ([]st
} }
func (r *directoryResolver) indexPath(path string, info os.FileInfo, err error) (string, error) { func (r *directoryResolver) indexPath(path string, info os.FileInfo, err error) (string, error) {
// link cycles could cause a revisit --we should not allow this
if r.hasBeenIndexed(path) {
return "", nil
}
// ignore any path which a filter function returns true // ignore any path which a filter function returns true
for _, filterFn := range r.pathFilterFns { for _, filterFn := range r.pathFilterFns {
if filterFn != nil && filterFn(path, info) { if filterFn != nil && filterFn(path, info) {
if info.IsDir() { if info != nil && info.IsDir() {
return "", fs.SkipDir return "", fs.SkipDir
} }
return "", nil return "", nil
@ -146,11 +151,6 @@ func (r *directoryResolver) indexPath(path string, info os.FileInfo, err error)
return "", nil return "", nil
} }
// link cycles could cause a revisit --we should not allow this
if r.hasBeenIndexed(path) {
return "", nil
}
if info == nil { if info == nil {
// walk may not be able to provide a FileInfo object, don't allow for this to stop indexing; keep track of the paths and continue. // walk may not be able to provide a FileInfo object, don't allow for this to stop indexing; keep track of the paths and continue.
r.errPaths[path] = fmt.Errorf("no file info observable at path=%q", path) r.errPaths[path] = fmt.Errorf("no file info observable at path=%q", path)

View File

@ -865,3 +865,24 @@ func Test_IncludeRootPathInIndex(t *testing.T) {
_, exists = resolver.metadata[ref.ID()] _, exists = resolver.metadata[ref.ID()]
require.True(t, exists) require.True(t, exists)
} }
func TestDirectoryResolver_indexPath(t *testing.T) {
// TODO: Ideally we can use an OS abstraction, which would obviate the need for real FS setup.
tempFile, err := os.CreateTemp("", "")
require.NoError(t, err)
resolver, err := newDirectoryResolver(tempFile.Name())
require.NoError(t, err)
t.Run("filtering path with nil os.FileInfo", func(t *testing.T) {
// We use one of these prefixes in order to trigger a pathFilterFn
filteredPath := unixSystemRuntimePrefixes[0]
var fileInfo os.FileInfo = nil
assert.NotPanics(t, func() {
_, err := resolver.indexPath(filteredPath, fileInfo, nil)
assert.NoError(t, err)
})
})
}