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

compress/zlib: zlib.NewReader with (yet) empty buffer causes unexpected EOF even before calling Read #58992

Closed
0xThiebaut opened this issue Mar 12, 2023 · 1 comment

Comments

@0xThiebaut
Copy link

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

$ go version
go version go1.20.2 windows/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\REDACTED\AppData\Local\go-build
set GOENV=C:\Users\REDACTED\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\REDACTED\go\pkg\mod
set GONOPROXY=REDACTED
set GONOSUMDB=REDACTED
set GOOS=windows
set GOPATH=C:\Users\REDACTED\go
set GOPRIVATE=REDACTED
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.20.2
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\REDACTED\AppData\Local\Temp\go-build2122050057=/tmp/go-build -gno-record-gcc-switches

What did you do?

Initiated a new zlib Reader with a (yet) empty buffer as input (e.g. on new network connection). The buffer is later populated as data becomes available (e.g. reading from a network connection) and, after data has been written, the zlib reader is consumed.

https://go.dev/play/p/cgSFSQYy8bL

package main

import (
	"bytes"
	"compress/zlib"
	"io"
	"os"
)

func main() {
	// Initiate empty buffer (e.g. network connection)
	buff := &bytes.Buffer{}

	// Create new zlib reader
	r, err := zlib.NewReader(buff)
	if err != nil {
		panic(err)
	}

	// Populate the buffer
	buff.Write([]byte{0x78, 0x9c, 0x05, 0x80, 0x41, 0x09, 0x00, 0x00, 0x08, 0x03, 0xab, 0x68, 0x1b, 0x1b, 0xac, 0x80, 0xfe, 0x0e, 0x06, 0xf6, 0x7f, 0x8c, 0x39, 0x70, 0xc9, 0xcf, 0x76, 0x00, 0x1c, 0x49, 0x04, 0x3e})

	// Read from the zlib reader
	io.Copy(os.Stdout, r)
	r.Close()
}

What did you expect to see?

No errors and the Hello World! message. The underlying buffer should only be consumed once the zlib Reader starts to be consumed.

What did you see instead?

The zlib.NewReader call errors with unexpected EOF as it tries to read data before the buffer is populated, a behavior which is not documented.

@ianlancetaylor
Copy link
Contributor

This is expected behavior. An io.Reader should either respond with data, or io.EOF, or block until data is available. A bytes.Buffer does not implement that, because it never blocks until data is available. You can only use a bytes.Buffer as an io.Reader if all data is already in the buffer. To do what you want you need a different data structure.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Mar 12, 2023
@golang golang locked and limited conversation to collaborators Mar 11, 2024
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

3 participants