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

bufio\bufio.go: Is Peek() in Reader incorrect? #39395

Closed
chenquan opened this issue Jun 4, 2020 · 1 comment
Closed

bufio\bufio.go: Is Peek() in Reader incorrect? #39395

chenquan opened this issue Jun 4, 2020 · 1 comment

Comments

@chenquan
Copy link

@chenquan chenquan commented Jun 4, 2020

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

$ go version

go version go1.14.2 windows/amd64

Does this issue reproduce with the latest release?

Unkown

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

go env Output
$ go env

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Xiaoyi\AppData\Local\go-build
set GOENV=C:\Users\Xiaoyi\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=E:\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,https://goproxy.io,direct
set GOROOT=E:\Program Files\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=E:\Program Files\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\Xiaoyi\AppData\Local\Temp\go-build969071118=/tmp/go-build -gno-record-gcc-switches

What did you do?

package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	f, err := os.Open("b.txt")
	if err != nil {
		panic(err)
	}

	fmt.Printf("%p\n", f)
	peek1, _ := bufio.NewReader(f).Peek(4)
	fmt.Println("1=>", string(peek1))

	fmt.Printf("%p\n", f)

	peek2, _ := bufio.NewReader(f).Peek(4)
	fmt.Println("2=>", string(peek2))

	r, err := ioutil.ReadAll(f)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(r))
}

What did you expect to see?

0xc000006028
1=> 1111
0xc000006028
2=> 1111

What did you see instead?

0xc000006028
1=> 1111
0xc000006028
2=>
@chenquan chenquan changed the title Is peek() in Reader incorrect? Is Peek() in Reader incorrect? Jun 4, 2020
@chenquan chenquan changed the title Is Peek() in Reader incorrect? bufio\bufio.go: Is Peek() in Reader incorrect? Jun 4, 2020
@acln0
Copy link
Contributor

@acln0 acln0 commented Jun 4, 2020

You're creating a new buffered reader for each Peek call, but re-using the file descriptor. The first Peek call advances the file offset, because it has to fill the buffer so that it can read. The second Peek call happens on a fresh bufio.Reader, which uses the same file descriptor to read data. That file descriptor is probably at EOF at that point.

If you change your program to re-use the buffered reader, you should see the results you expect: https://play.golang.org/p/ukUoAvmA3r2

For questions like these in the future, please use one of the community venues listed here: https://golang.org/help/#help

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
3 participants
You can’t perform that action at this time.