1
0
mirror of https://github.com/golang/go synced 2024-11-22 19:54:39 -07:00

archive/zip: handle extra data headers with no body

Fixes #4393.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6854058
This commit is contained in:
Dave Cheney 2012-11-18 00:45:47 +11:00
parent 4f250132f7
commit 7ec76e25b6
2 changed files with 36 additions and 1 deletions

View File

@ -238,7 +238,7 @@ func readDirectoryHeader(f *File, r io.Reader) error {
if len(f.Extra) > 0 {
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()
size := b.uint16()
if int(size) > len(b) {

View File

@ -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.
func TestHeaderInvalidTagAndSize(t *testing.T) {
const timeFormat = "20060102T150405.000.txt"
@ -220,3 +241,17 @@ func TestHeaderTooShort(t *testing.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)
}