Skip to content

Commit

Permalink
Repository: don't crash accessing invalid pathinfo (#443)
Browse files Browse the repository at this point in the history
When fs.Stat returns an error, pathinfo may be nil. In such situations
the only safe response seems to be to return the error to the caller.

Without this fix, accessing pathinfo.IsDir() below would lead to a crash
dereferencing a nil pointer.

This crash can be reproduced by trying to initialize a Git repo with an
invalid path name.

Also see: muesli/gitty#36
  • Loading branch information
muesli committed Jan 19, 2022
1 parent 1b36beb commit 935af59
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions repository.go
Expand Up @@ -280,6 +280,9 @@ func dotGitToOSFilesystems(path string, detect bool) (dot, wt billy.Filesystem,

pathinfo, err := fs.Stat("/")
if !os.IsNotExist(err) {
if pathinfo == nil {
return nil, nil, err
}
if !pathinfo.IsDir() && detect {
fs = osfs.New(filepath.Dir(path))
}
Expand Down
5 changes: 5 additions & 0 deletions repository_test.go
Expand Up @@ -2948,6 +2948,11 @@ func (s *RepositorySuite) TestBrokenMultipleShallowFetch(c *C) {
c.Assert(err, IsNil)
}

func (s *RepositorySuite) TestDotGitToOSFilesystemsInvalidPath(c *C) {
_, _, err := dotGitToOSFilesystems("\000", false)
c.Assert(err, NotNil)
}

func BenchmarkObjects(b *testing.B) {
defer fixtures.Clean()

Expand Down

0 comments on commit 935af59

Please sign in to comment.