From 1f029fa6d35e5ab6322ffae6562c0c6a3c23a2f7 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 10 Jun 2015 11:19:14 -0700 Subject: [PATCH] archive/zip: clarify that CreateHeader takes ownership of FileHeader Fixes #11144 Change-Id: I1da0b72ef00a84c9b5751be0e72ad07d664bc98b Reviewed-on: https://go-review.googlesource.com/10883 Reviewed-by: Andrew Gerrand Reviewed-by: Rob Pike Reviewed-by: Dmitry Vyukov --- src/archive/zip/writer.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/archive/zip/writer.go b/src/archive/zip/writer.go index 87ac694a4e..3be2b5fdb2 100644 --- a/src/archive/zip/writer.go +++ b/src/archive/zip/writer.go @@ -195,14 +195,20 @@ func (w *Writer) Create(name string) (io.Writer, error) { // CreateHeader adds a file to the zip file using the provided FileHeader // for the file metadata. // It returns a Writer to which the file contents should be written. +// // The file's contents must be written to the io.Writer before the next -// call to Create, CreateHeader, or Close. +// call to Create, CreateHeader, or Close. The provided FileHeader fh +// must not be modified after a call to CreateHeader. func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) { if w.last != nil && !w.last.closed { if err := w.last.close(); err != nil { return nil, err } } + if len(w.dir) > 0 && w.dir[len(w.dir)-1].FileHeader == fh { + // See https://golang.org/issue/11144 confusion. + return nil, errors.New("archive/zip: invalid duplicate FileHeader") + } fh.Flags |= 0x8 // we will write a data descriptor -- 2.50.0