Skip to content

bufio: calling Reset with itself leads to infinite recursion #58423

@abursavich

Description

@abursavich

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

$ go version
go version go1.20 darwin/arm64

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="arm64"
GOBIN=""
GOCACHE="/Users/REDACTED/Library/Caches/go-build"
GOENV="/Users/REDACTED/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/REDACTED/go/pkg/mod"
GONOPROXY="REDACTED"
GONOSUMDB="REDACTED"
GOOS="darwin"
GOPATH="/Users/REDACTED/go"
GOPRIVATE="REDACTED"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/p4/t2ydl1qj10s99sfykmbnp6cc0000gq/T/go-build2269043344=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I ran into this issue in a prometheus package that I was using.

The problem is that bufio.NewReader will return its given io.Reader as-is, if it happens to already be a *bufio.Reader of sufficient size. If you later call bufio.(*Reader).Reset on the "new" *bufio.Reader with the original io.Reader, it ends up setting the *bufio.Reader as its own underlying io.Reader. Then when you try to Read from it, there's an infinite recursion and stack overflow.

It's kinda strange to Reset a *bufio.Reader with the same io.Reader from which it was created, but I don't think bufio.NewReader should use the return as-is optimization without also handling this Reset with self issue.

My repro code is obviously contrived, but the actual case where this happened seemed fairly reasonable.

https://go.dev/play/p/2UTNBLVR1J__E

What did you expect to see?

I expected to see my server running smoothly.

What did you see instead?

My server kept crashing or getting OOM killed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions