mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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:
parent
da6207f7a4
commit
d340a89d9c
2 changed files with 29 additions and 19 deletions
|
|
@ -11,6 +11,7 @@ import (
|
|||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type T struct {
|
||||
|
|
@ -1113,3 +1114,30 @@ func TestUnmarshalUnexported(t *testing.T) {
|
|||
t.Errorf("got %q, want %q", out, want)
|
||||
}
|
||||
}
|
||||
|
||||
// Time3339 is a time.Time which encodes to and from JSON
|
||||
// as an RFC 3339 time in UTC.
|
||||
type Time3339 time.Time
|
||||
|
||||
func (t *Time3339) UnmarshalJSON(b []byte) error {
|
||||
if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' {
|
||||
return fmt.Errorf("types: failed to unmarshal non-string value %q as an RFC 3339 time")
|
||||
}
|
||||
tm, err := time.Parse(time.RFC3339, string(b[1:len(b)-1]))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*t = Time3339(tm)
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestUnmarshalJSONLiteralError(t *testing.T) {
|
||||
var t3 Time3339
|
||||
err := Unmarshal([]byte(`"0000-00-00T00:00:00Z"`), &t3)
|
||||
if err == nil {
|
||||
t.Fatalf("expected error; got time %v", time.Time(t3))
|
||||
}
|
||||
if !strings.Contains(err.Error(), "range") {
|
||||
t.Errorf("got err = %v; want out of range error", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue