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

encoding/binary: Read fails to detect a non-pointer 'data' argument #32927

Open
martin-juhlin opened this issue Jul 3, 2019 · 8 comments

Comments

Projects
None yet
6 participants
@martin-juhlin
Copy link

commented Jul 3, 2019

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

$ go version
go version go1.12.5 darwin/amd64

Does this issue reproduce with the latest release?

It does on play.golang.org

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/martin/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/martin/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/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/r2/nz7__kld2qq9w4dhg19fmx5h0000gn/T/go-build214042936=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I was trying to read a value with binary.Read. That function need a pointer for the Data parameter. I was using that function faulty by passing a non pointer value to it.

var value int32
err := binary.Read(input, binary.LittleEndian, value)

https://play.golang.org/p/ZFNQ4PjM9H-

What did you expect to see?

An error message saying that Data need to be a pointer

What did you see instead?

No error and and my value was zero. The method doesn't do anything at all.

@bcmills bcmills changed the title binary.Read doesn't return error if Data isn't a pointer encoding/binary: Read fails to detect a non-pointer 'data' argument Jul 3, 2019

@bcmills bcmills added this to the Go1.14 milestone Jul 3, 2019

@bcmills

This comment has been minimized.

Copy link
Member

commented Jul 3, 2019

CC @griesemer for encoding/binary

@vitaminniy

This comment has been minimized.

Copy link
Contributor

commented Jul 3, 2019

Looks like binary.Read should return an error when passed data interface is not a pointer or slice.

intDataSize handles the case when dst arg is a simple/primitive value but binary.Read not.

@gopherbot

This comment has been minimized.

Copy link

commented Jul 3, 2019

Change https://golang.org/cl/184957 mentions this issue: encoding/binary: make Read return an error when data is not a pointer

@griesemer

This comment has been minimized.

Copy link
Contributor

commented Jul 3, 2019

It is not clear to me that the CL chose the right approach. Given that the Read function panics when other conditions that must be satisfied are not satisfied, it seems ok to panic in this case as well.

Marked as NeedsDecision.

@vitaminniy

This comment has been minimized.

Copy link
Contributor

commented Jul 3, 2019

My point is that in the case when a pointer to a struct is passed and struct itself is incorrect then the reflect-related code may (or may not?) panic.
It seems to me that documentation on Read is a little bit unclear.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Jul 3, 2019

I think we should do the same thing we do in the fallback case, which is

	if size < 0 {
		return errors.New("binary.Read: invalid type " + reflect.TypeOf(data).String())
	}
@martin-juhlin

This comment has been minimized.

Copy link
Author

commented Jul 4, 2019

Shouldn't the behaviour be equal to other packages? Both encoding/json and encoding/xml is returning error.
https://play.golang.org/p/o9hl4rmIUDA

@martin-juhlin

This comment has been minimized.

Copy link
Author

commented Jul 4, 2019

encoding/json and encoding/xml with int32 instead of struct{} : https://play.golang.org/p/KKE6ssLsBmy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.