Skip to content

Commit 935af59

Browse files
authored
Repository: don't crash accessing invalid pathinfo (go-git#443)
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
1 parent 1b36beb commit 935af59

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

repository.go

+3
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ func dotGitToOSFilesystems(path string, detect bool) (dot, wt billy.Filesystem,
280280

281281
pathinfo, err := fs.Stat("/")
282282
if !os.IsNotExist(err) {
283+
if pathinfo == nil {
284+
return nil, nil, err
285+
}
283286
if !pathinfo.IsDir() && detect {
284287
fs = osfs.New(filepath.Dir(path))
285288
}

repository_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -2948,6 +2948,11 @@ func (s *RepositorySuite) TestBrokenMultipleShallowFetch(c *C) {
29482948
c.Assert(err, IsNil)
29492949
}
29502950

2951+
func (s *RepositorySuite) TestDotGitToOSFilesystemsInvalidPath(c *C) {
2952+
_, _, err := dotGitToOSFilesystems("\000", false)
2953+
c.Assert(err, NotNil)
2954+
}
2955+
29512956
func BenchmarkObjects(b *testing.B) {
29522957
defer fixtures.Clean()
29532958

0 commit comments

Comments
 (0)