]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: properly handle extra objects with fixed size arrays
authorRick Arnold <rickarnoldjr@gmail.com>
Wed, 13 Mar 2013 18:53:03 +0000 (14:53 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 13 Mar 2013 18:53:03 +0000 (14:53 -0400)
If a fixed size array is passed in as the decode target and the JSON
to decode has extra array elements that are objects, then previously
the decoder would return a "data changing underfoot" error.

Fixes #3717.

R=golang-dev, adg, rsc
CC=golang-dev
https://golang.org/cl/7490046

src/pkg/encoding/json/decode.go
src/pkg/encoding/json/decode_test.go

index d34e10f838456862927d9c48d6161787b5ceacf3..722037c30941223a01968bcf45c1ca08dbe38564 100644 (file)
@@ -261,6 +261,16 @@ func (d *decodeState) value(v reflect.Value) {
                }
                d.scan.step(&d.scan, '"')
                d.scan.step(&d.scan, '"')
+
+               n := len(d.scan.parseState)
+               if n > 0 && d.scan.parseState[n-1] == parseObjectKey {
+                       // d.scan thinks we just read an object key; finish the object
+                       d.scan.step(&d.scan, ':')
+                       d.scan.step(&d.scan, '"')
+                       d.scan.step(&d.scan, '"')
+                       d.scan.step(&d.scan, '}')
+               }
+
                return
        }
 
index e1bd918dda596b4643d11e5c7f8c775600ea5258..037c5b2368d3ecf1a6eb7ad38b3bc36d463f6568 100644 (file)
@@ -1178,3 +1178,16 @@ func TestUnmarshalJSONLiteralError(t *testing.T) {
                t.Errorf("got err = %v; want out of range error", err)
        }
 }
+
+// Test that extra object elements in an array do not result in a
+// "data changing underfoot" error.
+// Issue 3717
+func TestSkipArrayObjects(t *testing.T) {
+       json := `[{}]`
+       var dest [0]interface{}
+
+       err := Unmarshal([]byte(json), &dest)
+       if err != nil {
+               t.Errorf("got error %q, want nil", err)
+       }
+}