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

src/bytes/buffer.go - ReadFrom() causes panic if a reader is empty #39069

Closed
mateusz-szczyrzyca opened this issue May 14, 2020 · 2 comments
Closed

Comments

@mateusz-szczyrzyca
Copy link

@mateusz-szczyrzyca mateusz-szczyrzyca commented May 14, 2020

I don't know if it's intended behavior so I opened this issue

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

go version go1.14.2 linux/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
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/mateusz/.cache/go-build"
GOENV="/home/mateusz/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/mateusz/go/gopath"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/mateusz/go/goroot/current"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/mateusz/go/goroot/current/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="0"
GOMOD="/home/mateusz/git/my-project/go.mod"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build787106721=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I'm trying to get a string from htmlContentReader which is a reader from http client response.

I have the following code:

	// extract html from reader
	buf := &bytes.Buffer{}
	_, err = buf.ReadFrom(htmlContentReader)
	if err != nil {
		logger.Err(err).Msg("there was an error during creating string from io.Reader")
		return postData, err
	}
	htmlContent := buf.String()

buf.ReadFrom(htmlContentReader) causes panic if a reader is empty (see src/bytes/buffer.go:204) - b.buf is not checked if it's empty and index anyway is used and this is the result:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4e03d8]

goroutine 1 [running]:
bytes.(*Buffer).ReadFrom(0xc001fbf958, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/mateusz/go/goroot/current/src/bytes/buffer.go:207 +0x1f8
my-project/source/spider.(*Website).processContent(0xc000100340, 0xc0009b95e0, 0x1e, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        /home/mateusz/git/my-project/source/spider/posts.go:26 +0x1e7
my-project/source/spider.(*Website).ReadPosts(0xc000100340, 0x0, 0x0, 0x5ebd3011, 0xc000165ef0, 0x4aa296, 0x5ebd3011)
        /home/mateusz/git/my-project/source/spider/posts.go:147 +0x46c
main.main()
        /home/mateusz/git/my-project/converter.go:19 +0xe1
exit status 2

What did you expect to see?

I think error errEmptyReader or something like that should be returned in such case (instead of panicing) to handle this correctly - in my case, empty reader means empty webpage with statusCode=200 or statuscode=404, so I should simply "skip" processing of this page, I don't "expect" panic in such scenario.

@seankhliao
Copy link
Contributor

@seankhliao seankhliao commented May 14, 2020

it only panics if the reader is nil, not if it's empty
https://play.golang.org/p/xoFOaBPgYe-

@mateusz-szczyrzyca
Copy link
Author

@mateusz-szczyrzyca mateusz-szczyrzyca commented May 14, 2020

@seankhliao that's correct, one function silently passed nil reader and that caused the problem. So I'm closing the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

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