encoding/json: roll back Unmarshal optimization + test

The second attempt at the Unmarshal optimization allowed
panics to get out of the json package. Add test for that bug
and remove the optimization.

Let's stop trying to optimize Unmarshal.

Fixes #4784.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/7300108
This commit is contained in:
Russ Cox 2013-02-14 14:46:15 -05:00
parent da6207f7a4
commit d340a89d9c
2 changed files with 29 additions and 19 deletions

View file

@ -56,8 +56,7 @@ import (
// an UnmarshalTypeError describing the earliest such error.
//
func Unmarshal(data []byte, v interface{}) error {
// Quick check for well-formedness.
// Check for well-formedness.
// Avoids filling out half a data structure
// before discovering a JSON syntax error.
var d decodeState
@ -66,23 +65,6 @@ func Unmarshal(data []byte, v interface{}) error {
return err
}
// skip heavy processing for primitive values
var first byte
var i int
for i, first = range data {
if first > ' ' || !isSpace(rune(first)) {
break
}
}
if first != '{' && first != '[' {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return &InvalidUnmarshalError{reflect.TypeOf(v)}
}
d.literalStore(data[i:], rv.Elem(), false)
return d.savedError
}
d.init(data)
return d.unmarshal(v)
}