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

io: should ReadAtLeast's example return ErrUnexpectedEOF #30700

Open
iporsut opened this issue Mar 9, 2019 · 2 comments
Open

io: should ReadAtLeast's example return ErrUnexpectedEOF #30700

iporsut opened this issue Mar 9, 2019 · 2 comments

Comments

@iporsut
Copy link
Contributor

@iporsut iporsut commented Mar 9, 2019

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

$ go version
go version go1.12 darwin/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
GOARCH="amd64"
GOBIN="$HOME/go/bin"
GOCACHE="$HOME/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="$HOME/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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/9k/pvfwqqzn6fqbpwghhrcjkm480000gn/T/go-build355036879=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I read the document of io.ReadAtLeast https://golang.org/pkg/io/#ReadAtLeast. It tells If an EOF happens after reading fewer than min bytes, ReadAtLeast returns ErrUnexpectedEOF. .
The example code is

package main

import (
	"fmt"
	"io"
	"log"
	"strings"
)

func main() {
	r := strings.NewReader("some io.Reader stream to be read\n")

	buf := make([]byte, 33)
	if _, err := io.ReadAtLeast(r, buf, 4); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s\n", buf)

	// buffer smaller than minimal read size.
	shortBuf := make([]byte, 3)
	if _, err := io.ReadAtLeast(r, shortBuf, 4); err != nil {
		fmt.Println("error:", err)
	}

	// minimal read size bigger than io.Reader stream
	longBuf := make([]byte, 64)
	if _, err := io.ReadAtLeast(r, longBuf, 64); err != nil {
		fmt.Println("error:", err)
	}

}

What did you expect to see?

The last case of example.

	// minimal read size bigger than io.Reader stream
	longBuf := make([]byte, 64)
	if _, err := io.ReadAtLeast(r, longBuf, 64); err != nil {
		fmt.Println("error:", err)
	}

Should print

error: unexpected EOF

That is message from io.ErrUnexpectedEOF

What did you see instead?

error: EOF

Because the example use the same reader r that io.EOF from the first case.
I think should reset or create new reader before use in the last case.

	// minimal read size bigger than io.Reader stream
	r.Reset("some io.Reader stream to be read\n")
	longBuf := make([]byte, 64)
	if _, err := io.ReadAtLeast(r, longBuf, 64); err != nil {
		fmt.Println("error:", err)
	}
@julieqiu
Copy link
Contributor

@julieqiu julieqiu commented Mar 11, 2019

@bcmills
Copy link
Member

@bcmills bcmills commented Mar 11, 2019

Note that the initial call to ReadAtLeast (with buffer size 33) consumes the entire input, so the reader really is at EOF at the last call.

That said, the example could certainly be made clearer, perhaps by being split into multiple (separate) example functions.

@agnivade agnivade added this to the Unplanned milestone Apr 8, 2019
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
4 participants