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

cmd/vet: reject json.Unmarshal(data, non-pointer) #27564

Closed
AnikHasibul opened this issue Sep 8, 2018 · 4 comments

Comments

Projects
None yet
5 participants
@AnikHasibul
Copy link

commented Sep 8, 2018

Here is a snippet from encoding/json

func Unmarshal(data []byte, v interface{}) error 

In that snippet v is an interface{} . So it can be anything. But it needs to be a pointer to receive the unmarshaled value.

If we don't use a pointer we will get a runtime error. And it's must be a pointer.

Can we add a build tag to throw error when a interface is not a pointer?

So that, we can get a compile time error if that interface must be a pointer instead of runtime error.

@gopherbot gopherbot added this to the Proposal milestone Sep 8, 2018

@gopherbot gopherbot added the Proposal label Sep 8, 2018

@AnikHasibul AnikHasibul changed the title proposal: Build tags to pass runtime errors to compile error proposal: Go2: Build tags to pass runtime errors to compile error Sep 8, 2018

@rsc rsc changed the title proposal: Go2: Build tags to pass runtime errors to compile error proposal: cmd/vet: reject json.Unmarshal(data, non-pointer) Sep 19, 2018

@rsc

This comment has been minimized.

Copy link
Contributor

commented Sep 19, 2018

The way to reject this code early would be with cmd/vet. This would apply to gob and xml too, of course: you can't unmarshal into a non-(pointer or interface).

@rsc rsc changed the title proposal: cmd/vet: reject json.Unmarshal(data, non-pointer) cmd/vet: reject json.Unmarshal(data, non-pointer) Oct 3, 2018

@rsc rsc modified the milestones: Proposal, Unplanned Oct 3, 2018

@iand

This comment has been minimized.

Copy link
Contributor

commented Oct 4, 2018

I've made a start on implementing this check.

@gopherbot

This comment has been minimized.

Copy link

commented Oct 5, 2018

Change https://golang.org/cl/139997 mentions this issue: cmd/vet: detect non-pointer arguments for unmarshal and decode

@gopherbot gopherbot closed this in 7fb60eb Oct 11, 2018

@gopherbot

This comment has been minimized.

Copy link

commented Nov 8, 2018

Change https://golang.org/cl/148562 mentions this issue: go/analysis/passes/unmarshal: port vet's unmarshal checker

gopherbot pushed a commit to golang/tools that referenced this issue Nov 9, 2018

go/analysis/passes/unmarshal: port vet's unmarshal checker
The checker has been modified to avoid making two memory allocations
for every CallExpr in the program.

Originally: https://go-review.googlesource.com/c/139997
Updates golang/go#27564

Change-Id: I168869272a1d78d47d84c049aba619bb223cad70
Reviewed-on: https://go-review.googlesource.com/c/148562
Reviewed-by: Michael Matloob <matloob@golang.org>
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.