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

Closed
martin-juhlin opened this issue Jul 3, 2019 · 10 comments
Closed
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@martin-juhlin
Copy link

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 the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 3, 2019
@bcmills bcmills added this to the Go1.14 milestone Jul 3, 2019
@bcmills
Copy link
Member

bcmills commented Jul 3, 2019

CC @griesemer for encoding/binary

@vitaminniy
Copy link
Contributor

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
Copy link

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

@griesemer griesemer added NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jul 3, 2019
@griesemer
Copy link
Contributor

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
Copy link
Contributor

vitaminniy 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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Author

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

@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
@smasher164 smasher164 modified the milestones: Backlog, Go1.14 Oct 11, 2019
@odeke-em
Copy link
Member

odeke-em commented Nov 7, 2019

Hello @griesemer, I am just prodding you about this issue. From the precedent, it seems to be that returning an error is alright. This issue is marked for Go1.14 and there is already a CL that's just waiting for your blessing https://go-review.googlesource.com/c/go/+/184957/, please take a look and thank you.

@griesemer
Copy link
Contributor

@odeke-em Thanks. On it.

@golang golang locked and limited conversation to collaborators Nov 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

9 participants