]> Cypherpunks repositories - gostls13.git/commitdiff
json: fix addressing of slice indexes that are multiples of 8.
authorAdam Langley <agl@golang.org>
Fri, 13 Nov 2009 19:29:13 +0000 (11:29 -0800)
committerAdam Langley <agl@golang.org>
Fri, 13 Nov 2009 19:29:13 +0000 (11:29 -0800)
Fixes #147.

R=rsc
CC=golang-dev
https://golang.org/cl/152123

src/pkg/json/struct.go
src/pkg/json/struct_test.go

index 3fcf00e89754935352aca60e189f314094e97f70..d94988b6470b299c7ca2342529192e012ff8eaa3 100644 (file)
@@ -154,7 +154,7 @@ func (b *structBuilder) Elem(i int) Builder {
                        return &structBuilder{val: v.Elem(i)}
                }
        case *reflect.SliceValue:
-               if i > v.Cap() {
+               if i >= v.Cap() {
                        n := v.Cap();
                        if n < 8 {
                                n = 8
index 95a3eb2a4e78bc32721a06e57bbf4646263cdc18..15446961a8cc871fde96e286eb1b68c3ae73e4fe 100644 (file)
@@ -6,6 +6,7 @@ package json
 
 import (
        "reflect";
+       "strconv";
        "testing";
 )
 
@@ -101,3 +102,33 @@ func TestUnmarshal(t *testing.T) {
        check(t, reflect.DeepEqual(m.MapStruct, decodedMapStruct), "mapstruct", m.MapStruct);
        check(t, reflect.DeepEqual(m.MapPtrStruct, decodedMapPtrStruct), "mapptrstruct", m.MapPtrStruct);
 }
+
+type Issue147Text struct {
+       Text string;
+}
+
+type Issue147 struct {
+       Test []Issue147Text;
+}
+
+const issue147Input = `{"test": [{"text":"0"},{"text":"1"},{"text":"2"},
+{"text":"3"},{"text":"4"},{"text":"5"},
+{"text":"6"},{"text":"7"},{"text":"8"},
+{"text":"9"},{"text":"10"},{"text":"11"},
+{"text":"12"},{"text":"13"},{"text":"14"},
+{"text":"15"},{"text":"16"},{"text":"17"},
+{"text":"18"},{"text":"19"},{"text":"20"},
+{"text":"21"},{"text":"22"},{"text":"23"},
+{"text":"24"},{"text":"25"},{"text":"26"},
+{"text":"27"},{"text":"28"},{"text":"29"}]}`
+
+func TestIssue147(t *testing.T) {
+       var timeline Issue147;
+       Unmarshal(issue147Input, &timeline);
+
+       for i, e := range timeline.Test {
+               if e.Text != strconv.Itoa(i) {
+                       t.Errorf("index: %d got: %s want: %d", i, e.Text, i)
+               }
+       }
+}