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

archive/zip: NewReader panics on negative size #26589

LIUHUANUCAS opened this issue Jul 25, 2018 · 8 comments

archive/zip: NewReader panics on negative size #26589

LIUHUANUCAS opened this issue Jul 25, 2018 · 8 comments


Copy link

@LIUHUANUCAS LIUHUANUCAS commented Jul 25, 2018

Please answer these questions before submitting your issue. Thanks!

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

go version go1.7 linux/amd64

Does this issue reproduce with the latest release?

have no yet test

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

GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build197527898=/tmp/go-build -gno-record-gcc-switches"

What did you do?

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on is best.

zipReader, err := zip.NewReader(bytes.NewReader(respValue), -1)
  • -1 may be the http.Response.ContentLength

What did you expect to see?

panic(0x60a540, 0xc42000d190)
	/usr/local/go/src/runtime/panic.go:500 +0x1a1
archive/zip.readDirectoryEnd(0x93eec0, 0xc420013230, 0xffffffffffffffff, 0x6672a9, 0xc41ffff1d9, 0x0)
	/usr/local/go/src/archive/zip/reader.go:397 +0x9d
archive/zip.(*Reader).init(0xc42001c480, 0x93eec0, 0xc420013230, 0xffffffffffffffff, 0x620ba0, 0x1)
	/usr/local/go/src/archive/zip/reader.go:79 +0x5d
archive/zip.NewReader(0x93eec0, 0xc420013230, 0xffffffffffffffff, 0x4, 0x0, 0x0)
	/usr/local/go/src/archive/zip/reader.go:72 +0x62

My Question

  • The second parameter of function zip.NewReader is int64,and -1 has passed ,the program panic but not return a error.
  • because the function zip.NewReader has an error as returned value, so the way of design is Reasonable
Copy link

@zegl zegl commented Jul 25, 2018

This is reproducible with Go 1.10: playground.

I suggest that we add a check to zip.NewReader to make sure that size >= 0, and return an error if it's not. Alternatively improve the documentation to mention that the function will panic if size is smaller than 0.

Copy link

@iamoryanmoshe iamoryanmoshe commented Jul 25, 2018

What is causing the panic though? No input checks?

@bradfitz bradfitz changed the title NewReader(r io.ReaderAt, size int64) function panic while I pass -1 as the second parameters archive/zip: NewReader panics on negative size Jul 25, 2018
@bradfitz bradfitz added this to the Go1.12 milestone Jul 25, 2018
@bradfitz bradfitz added the NeedsFix label Jul 25, 2018
Copy link

@bradfitz bradfitz commented Jul 25, 2018

I suppose we could just return an error there instead, as it already returns an error.

I think adding documentation would be overkill, as -1 for a size seems like something you'd only do by mistake anyway.

Copy link

@jeet-parekh jeet-parekh commented Jul 28, 2018

I see @dsnet is assigned. Mind if I go ahead and submit a CL to fix this?

Copy link

@bradfitz bradfitz commented Jul 28, 2018

Go for it. Note that we won't review it until Go 1.11 is out in August, though, so no rush.

Copy link

@jeet-parekh jeet-parekh commented Jul 28, 2018

Got it.

Copy link

@dsnet dsnet commented Jul 28, 2018

Yep, go for it.

Copy link

@gopherbot gopherbot commented Jul 28, 2018

Change mentions this issue: archive/zip: return error from NewReader when negative size is passed

@gopherbot gopherbot closed this in 539ff60 Aug 21, 2018
@golang golang locked and limited conversation to collaborators Aug 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants
You can’t perform that action at this time.