Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zip.NewReader read some format zip file but write file has panic #43709

Closed
cor0ps opened this issue Jan 15, 2021 · 1 comment
Closed

zip.NewReader read some format zip file but write file has panic #43709

cor0ps opened this issue Jan 15, 2021 · 1 comment

Comments

@cor0ps
Copy link

cor0ps commented Jan 15, 2021

What version of Go are you using (go version)?

$ go version
>go version
go version go1.15.4 windows/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
C:\Users\xx\go\src\awesomeProject1>go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\xx\AppData\Local\go-build
set GOENV=C:\Users\xx\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\xx\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\xx\go
set GOPRIVATE=
set GOPROXY=direct
set GOROOT=C:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\xx\go\src\awesomeProject1\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\xx\AppData\Local\Temp\go-build958360038=/tmp/go-build -gno-record-gcc-switches

What did you do?

package main

import (
	"archive/zip"
	"bytes"
	"fmt"
	"io/ioutil"
)

func main() {

	data := []byte("00000000000000000000" +
		"00000000000000000000" +
		"00000000000000000000" +
		"00000000PK\x03\x0400000000" +
		"00000000000000\x03\x00\x1c\x0000" +
		"00000000000000000000" +
		"000000000asidhuasih " +
		"a\n sdjn osdn \nsdvs n" +
		"dv\nPK\x01\x020000000000000" +
		"00000000000\x03\x00\x18\x00\x00\x00000" +
		"00000000000000000000" +
		"0000000000000000PK\x01\x02" +
		"000000\x00\x000000OVET\"\x00\x00\x00" +
		"\"\x00\x00\x00\x03\x00\x18\x00\x00\x0000000000D\x00" +
		"\x00\x0000/000PK\x05\x06000000\x02\x00" +
		"0000\xa3\x00\x00\x00\x00\x00")
	z, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
	if err != nil {
		if z != nil {
			panic("non nil z")
		}
		return
	}
	var headers []*zip.FileHeader
	var contents [][]byte
	for _, f := range z.File {
		r, err := f.Open()
		if err != nil {
			continue
		}
		if f.UncompressedSize64 < 1e6 {
			c, err := ioutil.ReadAll(r)
			if err != nil {
				continue
			}
			if uint64(len(c)) != f.UncompressedSize64 {
				println("bad size:", len(c), f.UncompressedSize64)
				panic("bad size")
			}
			hdr := f.FileHeader
			headers = append(headers, &hdr)
			contents = append(contents, c)
		}
		r.Close()
	}
	if len(headers) == 0 {
		return
	}
	// Write a new archive with the same files.
	buf := new(bytes.Buffer)
	w := zip.NewWriter(buf)
	for i, h := range headers {
		w1, err := w.CreateHeader(h)
		if err != nil {
			panic(err)
		}
		xx:=len(contents[i])
		fmt.Println(xx)
		n, err := w1.Write(contents[i])
		if err != nil {
			panic(err)
		}
		if n != len(contents[i]) {
			panic("short write")
		}
	}
	err = w.Close()
	if err != nil {
		panic(err)
	}

}

What did you expect to see?

write via return zip file

What did you see instead?

panic: zip: write to directory

@davecheney
Copy link
Contributor

If I add

                if h.FileInfo().IsDir() {
                        continue
                }

before writing out the contents, this solves the error.

I don't think this is a bug in Go, zip is a weird, under specified format.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For asking questions, see:

@golang golang locked and limited conversation to collaborators Jan 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants