Skip to content

Commit

Permalink
Work around root directory Open/Stat corner case leading to panic
Browse files Browse the repository at this point in the history
  • Loading branch information
desdeel2d0m authored and hillu committed Mar 30, 2020
1 parent 16a0670 commit c4eca06
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion zipfs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ func (f *File) Readdirnames(count int) (names []string, err error) {
return
}

func (f *File) Stat() (os.FileInfo, error) { return f.zipfile.FileInfo(), nil }
func (f *File) Stat() (os.FileInfo, error) {
if f.zipfile == nil {
return &pseudoRoot{}, nil
}
return f.zipfile.FileInfo(), nil
}

func (f *File) Sync() error { return nil }

Expand Down
12 changes: 12 additions & 0 deletions zipfs/zipfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ func TestZipFS(t *testing.T) {
t.Errorf("expected to get <aaaabbbb>, got <%s>", string(buf))
}

d, err := a.Open("/")
if d == nil {
t.Error(`Open("/") returns nil`)
}
if err != nil {
t.Errorf(`Open("/"): err = %v`, err)
}
if s, _ := d.Stat(); !s.IsDir() {
t.Error(`expected root ("/") to be a directory`)
}

buf = make([]byte, 8192)
if n, err := f.Read(buf); err != nil {
t.Error(err)
Expand All @@ -51,6 +62,7 @@ func TestZipFS(t *testing.T) {
path string
dir bool
}{
{"/", true},
{"testDir1", true},
{"testDir1/testFile", false},
{"testFile", false},
Expand Down

0 comments on commit c4eca06

Please sign in to comment.