Skip to content

Commit

Permalink
Do not panic on passing empty tar reader to tarfs
Browse files Browse the repository at this point in the history
Corrects a bug where passing an empty tar reader to tarfs would cause
adding the pseudoroot to panic with assignment to entry in nil map.

Signed-off-by: hasheddan <[email protected]>
  • Loading branch information
hasheddan committed Oct 5, 2020
1 parent a4ea980 commit 9fa7c3b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tarfs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func New(t *tar.Reader) *Fs {

}

if fs.files[afero.FilePathSeparator] == nil {
fs.files[afero.FilePathSeparator] = make(map[string]*File)
}
// Add a pseudoroot
fs.files[afero.FilePathSeparator][""] = &File{
h: &tar.Header{
Expand Down
4 changes: 4 additions & 0 deletions tarfs/tarfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"reflect"
"strings"
"syscall"
"testing"

Expand Down Expand Up @@ -54,6 +55,9 @@ func TestMain(m *testing.M) {

tfs := New(tar.NewReader(tf))
afs = &afero.Afero{Fs: tfs}

// Check that an empty reader does not panic.
_ = New(tar.NewReader(strings.NewReader("")))
os.Exit(m.Run())
}

Expand Down

0 comments on commit 9fa7c3b

Please sign in to comment.