1
0
mirror of https://github.com/golang/go synced 2024-11-23 15:00:03 -07:00

archive/tar: Do not panic on Read if uninitialized

Calling tar.Reader.Read() used to work fine, but without this patch it panics.
Simply return EOF to indicate the tar.Reader.Next() needs to be called.

LGTM=iant, bradfitz
R=golang-codereviews, bradfitz, iant, mikioh.mikioh, dominik.honnef
CC=golang-codereviews
https://golang.org/cl/94530043
This commit is contained in:
Guillaume J. Charmes 2014-05-15 15:18:05 -07:00 committed by Brad Fitzpatrick
parent 8c8c260d2e
commit 6d63d4f3be
2 changed files with 19 additions and 0 deletions

View File

@ -724,6 +724,9 @@ func (tr *Reader) numBytes() int64 {
// It returns 0, io.EOF when it reaches the end of that entry,
// until Next is called to advance to the next entry.
func (tr *Reader) Read(b []byte) (n int, err error) {
if tr.curr == nil {
return 0, io.EOF
}
n, err = tr.curr.Read(b)
if err != nil && err != io.EOF {
tr.err = err

View File

@ -725,3 +725,19 @@ func TestReadGNUSparseMap1x0(t *testing.T) {
t.Errorf("Incorrect sparse map: got %v, wanted %v", sp, expected)
}
}
func TestUninitializedRead(t *testing.T) {
test := gnuTarTest
f, err := os.Open(test.file)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer f.Close()
tr := NewReader(f)
_, err = tr.Read([]byte{})
if err == nil || err != io.EOF {
t.Errorf("Unexpected error: %v, wanted %v", err, io.EOF)
}
}