diff options
author | Adam Langley <agl@golang.org> | 2010-01-11 18:54:13 -0800 |
---|---|---|
committer | Adam Langley <agl@golang.org> | 2010-01-11 18:54:13 -0800 |
commit | 32e28a4030cfa931528146f6a0efe0eaba7411e5 (patch) | |
tree | 6ee2a4a01fcb27eef838bf505a5bb70b690f35b3 /src/pkg/asn1 | |
parent | aa26195a61879773d2335f64d9e818236dc5cc9f (diff) | |
download | golang-32e28a4030cfa931528146f6a0efe0eaba7411e5.tar.gz |
asn1: fix marshaling of structures with >1 elements.
Fixes issue 515.
R=rsc
CC=golang-dev
http://codereview.appspot.com/184079
Diffstat (limited to 'src/pkg/asn1')
-rw-r--r-- | src/pkg/asn1/marshal.go | 9 | ||||
-rw-r--r-- | src/pkg/asn1/marshal_test.go | 6 |
2 files changed, 13 insertions, 2 deletions
diff --git a/src/pkg/asn1/marshal.go b/src/pkg/asn1/marshal.go index eec0ee849..40a52b662 100644 --- a/src/pkg/asn1/marshal.go +++ b/src/pkg/asn1/marshal.go @@ -29,6 +29,9 @@ func newForkableWriter() *forkableWriter { } func (f *forkableWriter) fork() (pre, post *forkableWriter) { + if f.pre != nil || f.post != nil { + panic("have already forked") + } f.pre = newForkableWriter() f.post = newForkableWriter() return f.pre, f.post @@ -61,7 +64,7 @@ func (f *forkableWriter) writeTo(out io.Writer) (n int, err os.Error) { } } - if f.pre != nil { + if f.post != nil { nn, err = f.post.writeTo(out) n += nn } @@ -297,7 +300,9 @@ func marshalBody(out *forkableWriter, value reflect.Value, params fieldParameter case *reflect.StructValue: t := v.Type().(*reflect.StructType) for i := 0; i < t.NumField(); i++ { - err = marshalField(out, v.Field(i), parseFieldParameters(t.Field(i).Tag)) + var pre *forkableWriter + pre, out = out.fork() + err = marshalField(pre, v.Field(i), parseFieldParameters(t.Field(i).Tag)) if err != nil { return } diff --git a/src/pkg/asn1/marshal_test.go b/src/pkg/asn1/marshal_test.go index 2bb8a28d9..da4a03c04 100644 --- a/src/pkg/asn1/marshal_test.go +++ b/src/pkg/asn1/marshal_test.go @@ -15,6 +15,11 @@ type intStruct struct { A int } +type twoIntStruct struct { + A int + B int +} + type nestedStruct struct { A intStruct } @@ -48,6 +53,7 @@ func setPST(t *time.Time) *time.Time { var marshalTests = []marshalTest{ marshalTest{10, "02010a"}, marshalTest{intStruct{64}, "3003020140"}, + marshalTest{twoIntStruct{64, 65}, "3006020140020141"}, marshalTest{nestedStruct{intStruct{127}}, "3005300302017f"}, marshalTest{[]byte{1, 2, 3}, "0403010203"}, marshalTest{implicitTagTest{64}, "3003850140"}, |