Commit 7ec76e25 authored by Dave Cheney's avatar Dave Cheney

archive/zip: handle extra data headers with no body

Fixes #4393.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6854058
parent 4f250132
...@@ -238,7 +238,7 @@ func readDirectoryHeader(f *File, r io.Reader) error { ...@@ -238,7 +238,7 @@ func readDirectoryHeader(f *File, r io.Reader) error {
if len(f.Extra) > 0 { if len(f.Extra) > 0 {
b := readBuf(f.Extra) b := readBuf(f.Extra)
for len(b) > 4 { // need at least tag and size for len(b) >= 4 { // need at least tag and size
tag := b.uint16() tag := b.uint16()
size := b.uint16() size := b.uint16()
if int(size) > len(b) { if int(size) > len(b) {
......
...@@ -195,6 +195,27 @@ func testInvalidHeader(h *FileHeader, t *testing.T) { ...@@ -195,6 +195,27 @@ func testInvalidHeader(h *FileHeader, t *testing.T) {
} }
} }
func testValidHeader(h *FileHeader, t *testing.T) {
var buf bytes.Buffer
z := NewWriter(&buf)
f, err := z.CreateHeader(h)
if err != nil {
t.Fatalf("error creating header: %v", err)
}
if _, err := f.Write([]byte("hi")); err != nil {
t.Fatalf("error writing content: %v", err)
}
if err := z.Close(); err != nil {
t.Fatalf("error closing zip writer: %v", err)
}
b := buf.Bytes()
if _, err = NewReader(bytes.NewReader(b), int64(len(b))); err != nil {
t.Fatalf("got %v, expected nil", err)
}
}
// Issue 4302. // Issue 4302.
func TestHeaderInvalidTagAndSize(t *testing.T) { func TestHeaderInvalidTagAndSize(t *testing.T) {
const timeFormat = "20060102T150405.000.txt" const timeFormat = "20060102T150405.000.txt"
...@@ -220,3 +241,17 @@ func TestHeaderTooShort(t *testing.T) { ...@@ -220,3 +241,17 @@ func TestHeaderTooShort(t *testing.T) {
} }
testInvalidHeader(&h, t) testInvalidHeader(&h, t)
} }
// Issue 4393. It is valid to have an extra data header
// which contains no body.
func TestZeroLengthHeader(t *testing.T) {
h := FileHeader{
Name: "extadata.txt",
Method: Deflate,
Extra: []byte{
85, 84, 5, 0, 3, 154, 144, 195, 77, // tag 21589 size 5
85, 120, 0, 0, // tag 30805 size 0
},
}
testValidHeader(&h, t)
}
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