Do not panic on passing empty tar reader to tarfs

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 <georgedanielmangum@gmail.com>
This commit is contained in:
hasheddan 2020-10-05 17:23:18 -05:00
parent a4ea980f2d
commit 9fa7c3b3c6
No known key found for this signature in database
GPG Key ID: BD68BC686A14C271
2 changed files with 7 additions and 0 deletions

View File

@ -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{

View File

@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"reflect"
"strings"
"syscall"
"testing"
@ -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())
}