1
0
mirror of https://github.com/golang/go synced 2024-11-20 04:44:40 -07:00

archive/zip: handle corrupt extra data records

Fixes #4302.

R=golang-dev, bradfitz, adg
CC=golang-dev
https://golang.org/cl/6811048
This commit is contained in:
Dave Cheney 2012-10-31 03:51:59 +11:00
parent d77eb12998
commit 640d818f2a
2 changed files with 37 additions and 0 deletions

View File

@ -241,6 +241,9 @@ func readDirectoryHeader(f *File, r io.Reader) error {
for len(b) > 0 {
tag := b.uint16()
size := b.uint16()
if int(size) > len(b) {
return ErrFormat
}
if tag == zip64ExtraId {
// update directory values from the zip64 extra block
eb := readBuf(b)

View File

@ -173,3 +173,37 @@ func TestZip64(t *testing.T) {
t.Errorf("UncompressedSize64 %d, want %d", got, want)
}
}
// Issue 4302.
func TestInvalidExtraHedaer(t *testing.T) {
const timeFormat = "20060102T150405.000.txt"
var buf bytes.Buffer
z := NewWriter(&buf)
ts := time.Now()
filename := ts.Format(timeFormat)
h := FileHeader{
Name: filename,
Method: Deflate,
Extra: []byte(ts.Format(time.RFC3339Nano)), // missing tag and len
}
h.SetModTime(ts)
fh, err := z.CreateHeader(&h)
if err != nil {
t.Fatalf("error creating header: %v", err)
}
if _, err := fh.Write([]byte("hi")); err != nil {
t.Fatalf("error writing content: %v", err)
}
if err := z.Close(); err != nil {
t.Fatal("error closing zip writer: %v", err)
}
b := buf.Bytes()
if _, err = NewReader(bytes.NewReader(b), int64(len(b))); err == nil {
t.Fatal("expected ErrFormat")
}
}