From e7cceb85e45e01d58d740de1adc111c08dd9d9de Mon Sep 17 00:00:00 2001 From: Adam Langley Date: Mon, 11 Jan 2010 18:54:13 -0800 Subject: [PATCH] asn1: fix marshaling of structures with >1 elements. Fixes #515. R=rsc CC=golang-dev https://golang.org/cl/184079 --- src/pkg/asn1/marshal.go | 9 +++++++-- 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 eec0ee84943..40a52b6624b 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 2bb8a28d9cc..da4a03c0414 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"},