diff options
author | Ondřej Surý <ondrej@sury.org> | 2012-06-14 13:23:46 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2012-06-14 13:23:46 +0200 |
commit | 917c5fb8ec48e22459d77e3849e6d388f93d3260 (patch) | |
tree | 9c23734a6ffd4d2a8ac99502eda3cc812a8b130b /src/pkg/encoding | |
parent | 0003ee229fd33ff46cb5f2fe1e35f5c0284debc4 (diff) | |
download | golang-917c5fb8ec48e22459d77e3849e6d388f93d3260.tar.gz |
Imported Upstream version 1.0.2upstream/1.0.2
Diffstat (limited to 'src/pkg/encoding')
-rw-r--r-- | src/pkg/encoding/base64/base64.go | 2 | ||||
-rw-r--r-- | src/pkg/encoding/base64/base64_test.go | 49 | ||||
-rw-r--r-- | src/pkg/encoding/gob/doc.go | 2 | ||||
-rw-r--r-- | src/pkg/encoding/json/decode.go | 14 | ||||
-rw-r--r-- | src/pkg/encoding/json/decode_test.go | 65 | ||||
-rw-r--r-- | src/pkg/encoding/json/encode.go | 4 |
6 files changed, 131 insertions, 5 deletions
diff --git a/src/pkg/encoding/base64/base64.go b/src/pkg/encoding/base64/base64.go index f8a51a4e7..0b842f066 100644 --- a/src/pkg/encoding/base64/base64.go +++ b/src/pkg/encoding/base64/base64.go @@ -318,7 +318,7 @@ func (d *decoder) Read(p []byte) (n int, err error) { } nn, d.err = io.ReadAtLeast(d.r, d.buf[d.nbuf:nn], 4-d.nbuf) d.nbuf += nn - if d.nbuf < 4 { + if d.err != nil || d.nbuf < 4 { return 0, d.err } diff --git a/src/pkg/encoding/base64/base64_test.go b/src/pkg/encoding/base64/base64_test.go index 9c3537259..f9b863c36 100644 --- a/src/pkg/encoding/base64/base64_test.go +++ b/src/pkg/encoding/base64/base64_test.go @@ -6,9 +6,11 @@ package base64 import ( "bytes" + "errors" "io" "io/ioutil" "testing" + "time" ) type testpair struct { @@ -226,3 +228,50 @@ func TestNewLineCharacters(t *testing.T) { } } } + +type nextRead struct { + n int // bytes to return + err error // error to return +} + +// faultInjectReader returns data from source, rate-limited +// and with the errors as written to nextc. +type faultInjectReader struct { + source string + nextc <-chan nextRead +} + +func (r *faultInjectReader) Read(p []byte) (int, error) { + nr := <-r.nextc + if len(p) > nr.n { + p = p[:nr.n] + } + n := copy(p, r.source) + r.source = r.source[n:] + return n, nr.err +} + +// tests that we don't ignore errors from our underlying reader +func TestDecoderIssue3577(t *testing.T) { + next := make(chan nextRead, 10) + wantErr := errors.New("my error") + next <- nextRead{5, nil} + next <- nextRead{10, wantErr} + d := NewDecoder(StdEncoding, &faultInjectReader{ + source: "VHdhcyBicmlsbGlnLCBhbmQgdGhlIHNsaXRoeSB0b3Zlcw==", // twas brillig... + nextc: next, + }) + errc := make(chan error) + go func() { + _, err := ioutil.ReadAll(d) + errc <- err + }() + select { + case err := <-errc: + if err != wantErr { + t.Errorf("got error %v; want %v", err, wantErr) + } + case <-time.After(5 * time.Second): + t.Errorf("timeout; Decoder blocked without returning an error") + } +} diff --git a/src/pkg/encoding/gob/doc.go b/src/pkg/encoding/gob/doc.go index 96885f8de..821d9a3fe 100644 --- a/src/pkg/encoding/gob/doc.go +++ b/src/pkg/encoding/gob/doc.go @@ -116,7 +116,7 @@ uninterpreted bytes of the value. All other slices and arrays are sent as an unsigned count followed by that many elements using the standard gob encoding for their type, recursively. -Maps are sent as an unsigned count followed by that man key, element +Maps are sent as an unsigned count followed by that many key, element pairs. Empty but non-nil maps are sent, so if the sender has allocated a map, the receiver will allocate a map even no elements are transmitted. diff --git a/src/pkg/encoding/json/decode.go b/src/pkg/encoding/json/decode.go index 110c6fd62..d61f88706 100644 --- a/src/pkg/encoding/json/decode.go +++ b/src/pkg/encoding/json/decode.go @@ -273,9 +273,14 @@ func (d *decodeState) indirect(v reflect.Value, decodingNull bool) (Unmarshaler, _, isUnmarshaler = v.Interface().(Unmarshaler) } + // Load value from interface, but only if the result will be + // usefully addressable. if iv := v; iv.Kind() == reflect.Interface && !iv.IsNil() { - v = iv.Elem() - continue + e := iv.Elem() + if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) { + v = e + continue + } } pv := v @@ -588,6 +593,11 @@ func (d *decodeState) literal(v reflect.Value) { // produce more helpful error messages. func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) { // Check for unmarshaler. + if len(item) == 0 { + //Empty string given + d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) + return + } wantptr := item[0] == 'n' // null unmarshaler, pv := d.indirect(v, wantptr) if unmarshaler != nil { diff --git a/src/pkg/encoding/json/decode_test.go b/src/pkg/encoding/json/decode_test.go index d758758d9..6fac22c4a 100644 --- a/src/pkg/encoding/json/decode_test.go +++ b/src/pkg/encoding/json/decode_test.go @@ -638,3 +638,68 @@ func TestAnonymous(t *testing.T) { t.Fatal("Unmarshal: did set T.Y") } } + +// Test that the empty string doesn't panic decoding when ,string is specified +// Issue 3450 +func TestEmptyString(t *testing.T) { + type T2 struct { + Number1 int `json:",string"` + Number2 int `json:",string"` + } + data := `{"Number1":"1", "Number2":""}` + dec := NewDecoder(strings.NewReader(data)) + var t2 T2 + err := dec.Decode(&t2) + if err == nil { + t.Fatal("Decode: did not return error") + } + if t2.Number1 != 1 { + t.Fatal("Decode: did not set Number1") + } +} + +func intp(x int) *int { + p := new(int) + *p = x + return p +} + +func intpp(x *int) **int { + pp := new(*int) + *pp = x + return pp +} + +var interfaceSetTests = []struct { + pre interface{} + json string + post interface{} +}{ + {"foo", `"bar"`, "bar"}, + {"foo", `2`, 2.0}, + {"foo", `true`, true}, + {"foo", `null`, nil}, + + {nil, `null`, nil}, + {new(int), `null`, nil}, + {(*int)(nil), `null`, nil}, + {new(*int), `null`, new(*int)}, + {(**int)(nil), `null`, nil}, + {intp(1), `null`, nil}, + {intpp(nil), `null`, intpp(nil)}, + {intpp(intp(1)), `null`, intpp(nil)}, +} + +func TestInterfaceSet(t *testing.T) { + for _, tt := range interfaceSetTests { + b := struct{ X interface{} }{tt.pre} + blob := `{"X":` + tt.json + `}` + if err := Unmarshal([]byte(blob), &b); err != nil { + t.Errorf("Unmarshal %#q: %v", blob, err) + continue + } + if !reflect.DeepEqual(b.X, tt.post) { + t.Errorf("Unmarshal %#q into %#v: X=%#v, want %#v", blob, tt.pre, b.X, tt.post) + } + } +} diff --git a/src/pkg/encoding/json/encode.go b/src/pkg/encoding/json/encode.go index 842672c39..b6e1cb16e 100644 --- a/src/pkg/encoding/json/encode.go +++ b/src/pkg/encoding/json/encode.go @@ -96,7 +96,7 @@ import ( // // Channel, complex, and function values cannot be encoded in JSON. // Attempting to encode such a value causes Marshal to return -// an InvalidTypeError. +// an UnsupportedTypeError. // // JSON cannot represent cyclic data structures and Marshal does not // handle them. Passing cyclic structures to Marshal will result in @@ -157,6 +157,8 @@ type Marshaler interface { MarshalJSON() ([]byte, error) } +// An UnsupportedTypeError is returned by Marshal when attempting +// to encode an unsupported value type. type UnsupportedTypeError struct { Type reflect.Type } |