go: bytes: panic from Read in (*Buffer).ReadFrom fills with 512 zero bytes
Please answer these questions before submitting your issue. Thanks!
What did you do?
I discovered this bug when testing an ssh client on go1.10. The client used a context.WithTimeout() to hang up and return after a defined timeout period. When checking the value of the *Buffer passed in to session.Stderr, I was presented with a 512-length slice of zero-value bytes any time the context timed out.
This bug only presents in go 1.10 and above.
I managed to recreate the issue directly with Buffer.ReadFrom(), the call to b.grow() causes the buffer to use a new slice of bytes with a length of 512 (MinRead), which is subsequently re-managed by the Reader.
Until the Reader completes its Read, the previously empty buffer remains “full”:
https://play.golang.org/p/W-VEKbdsOfF
What did you expect to see?
An empty Buffer
What did you see instead?
A buffer filled with 512 zero-value bytes
System details
go version go1.10.2 darwin/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/forfuncsake/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/forfuncsake/go"
GORACE=""
GOROOT="/Users/forfuncsake/sdk/go1.10.2"
GOTMPDIR=""
GOTOOLDIR="/Users/forfuncsake/sdk/go1.10.2/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/7g/1681qtj95_31k480ygpsk_88xw4s06/T/go-build089491914=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOROOT/bin/go version: go version go1.10.2 darwin/amd64
GOROOT/bin/go tool compile -V: compile version go1.10.2
uname -v: Darwin Kernel Version 17.5.0: Fri Apr 13 19:32:32 PDT 2018; root:xnu-4570.51.2~1/RELEASE_X86_64
ProductName: Mac OS X
ProductVersion: 10.13.4
BuildVersion: 17E202
lldb --version: lldb-902.0.79.2
Swift-4.1
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 19 (12 by maintainers)
@forfuncsake you can use my last example in your test case: https://go-review.googlesource.com/c/go/+/113495/2/src/bytes/buffer_test.go