Skip to content

Commit

Permalink
fix: don't crash accessing invalid pathinfo
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 14, 2022
1 parent a5bbcd2 commit 8ceb0c1
Show file tree
Hide file tree
Showing 2 changed files with 10 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
7 changes: 7 additions & 0 deletions repository_test.go
Expand Up @@ -2948,6 +2948,13 @@ func (s *RepositorySuite) TestBrokenMultipleShallowFetch(c *C) {
c.Assert(err, IsNil)
}

func TestDotGitToOSFilesystems(t *testing.T) {
_, _, err := dotGitToOSFilesystems("\000", false)
if err == nil {
t.Fatal("expected error, got nil")
}
}

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

Expand Down

0 comments on commit 8ceb0c1

Please sign in to comment.