Commit 6d63d4f3 authored by Guillaume J. Charmes's avatar Guillaume J. Charmes Committed by Brad Fitzpatrick

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
parent 8c8c260d
...@@ -724,6 +724,9 @@ func (tr *Reader) numBytes() int64 { ...@@ -724,6 +724,9 @@ func (tr *Reader) numBytes() int64 {
// It returns 0, io.EOF when it reaches the end of that entry, // It returns 0, io.EOF when it reaches the end of that entry,
// until Next is called to advance to the next entry. // until Next is called to advance to the next entry.
func (tr *Reader) Read(b []byte) (n int, err error) { func (tr *Reader) Read(b []byte) (n int, err error) {
if tr.curr == nil {
return 0, io.EOF
}
n, err = tr.curr.Read(b) n, err = tr.curr.Read(b)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
tr.err = err tr.err = err
......
...@@ -725,3 +725,19 @@ func TestReadGNUSparseMap1x0(t *testing.T) { ...@@ -725,3 +725,19 @@ func TestReadGNUSparseMap1x0(t *testing.T) {
t.Errorf("Incorrect sparse map: got %v, wanted %v", sp, expected) 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)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment