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/json: crash unmarshalling into error #4222

Closed
rsc opened this issue Oct 9, 2012 · 9 comments
Closed

encoding/json: crash unmarshalling into error #4222

rsc opened this issue Oct 9, 2012 · 9 comments
Labels
FrozenDueToAge Suggested Issues that may be good for new contributors looking for work to do.
Milestone

Comments

@rsc
Copy link
Contributor

rsc commented Oct 9, 2012

http://tip.play.golang.org/p/cf0Y-rYI6G

package main

import (
    "encoding/json"
    "errors"
    "fmt"
)

func main() {
    err := errors.New("boom")
    err_json, e0 := json.Marshal(err)
    fmt.Println(string(err_json), e0)

    var err2 error
    json.Unmarshal(err_json, &err2) // <- BOOM!
    fmt.Println(err2)
}

{} <nil>
panic: reflect.Set: value of type map[string]interface {} is not assignable to type
error [recovered]
    panic: interface conversion: string is not error: missing method Error

goroutine 1 [running]:
encoding/json._func_001(0x2b3b7b55ae98, 0x2b3b7b55a100, 0x2b3b7b55afb8, 0x2b3b7b55a730)
    go/src/pkg/encoding/json/decode.go:123 +0xaf
----- stack segment boundary -----
reflect.Value.assignTo(0x462fa8, 0xf840029880, 0x150, 0x4a5efc, 0x6c6665720000000b, ...)
    go/src/pkg/reflect/value.go:1781 +0x41a
reflect.Value.Set(0x482860, 0xf84002c040, 0x146, 0x462fa8, 0xf840029880, ...)
    go/src/pkg/reflect/value.go:1197 +0xa0
encoding/json.(*decodeState).object(0xf84004b000, 0x482860, 0xf84002c040, 0x146)
    go/src/pkg/encoding/json/decode.go:421 +0x1f8
encoding/json.(*decodeState).value(0xf84004b000, 0x460db0, 0xf84002c040, 0x160)
    go/src/pkg/encoding/json/decode.go:249 +0x1da
encoding/json.(*decodeState).unmarshal(0xf84004b000, 0x460da0, 0xf84002c040, 0x0, 0x0,
...)
    go/src/pkg/encoding/json/decode.go:136 +0x159
encoding/json.Unmarshal(0xf840048018, 0x4000000002, 0x460da0, 0xf84002c040, 0x0, ...)
    go/src/pkg/encoding/json/decode.go:65 +0xef
main.main()
    /tmpfs/gosandbox-1fd663dc_84dfdfd3_f11c0d3d_d3f9c24e_425e2883/prog.go:15 +0x21e
@rsc
Copy link
Contributor Author

rsc commented Dec 10, 2012

Comment 1:

Labels changed: added size-m.

@rsc
Copy link
Contributor Author

rsc commented Dec 10, 2012

Comment 2:

Labels changed: added suggested.

@davecheney
Copy link
Contributor

Comment 3:

https://golang.org/cl/6938045

Owner changed to @davecheney.

Status changed to Started.

@davecheney
Copy link
Contributor

Comment 4:

Throwing this one back into the queue.

Owner changed to ---.

Status changed to Accepted.

@gopherbot
Copy link

Comment 5 by vegacom:

I'm taking this.
My patch is assuming the problem is same as for http://tip.play.golang.org/p/Ffg9bH2UJW

@gopherbot
Copy link

Comment 6 by vegacom:

FYI: https://golang.org/cl/7102053

@remyoudompheng
Copy link
Contributor

Comment 7:

Thanks for your patch, but many more places must be fixed.
I made https://golang.org/cl/7100049 yesterday, you might be interested in
having a look.

Status changed to Started.

@gopherbot
Copy link

Comment 8 by vegacom:

nice,
there's one test case you might add:
    {new(error), `null`},

@remyoudompheng
Copy link
Contributor

Comment 9:

This issue was closed by revision 406ca3c.

Status changed to Fixed.

@rsc rsc added fixed Suggested Issues that may be good for new contributors looking for work to do. labels Jan 14, 2013
@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Suggested Issues that may be good for new contributors looking for work to do.
Projects
None yet
Development

No branches or pull requests

4 participants