1
0
mirror of https://github.com/golang/go synced 2024-11-25 08:07:57 -07:00

asn1: fix marshaling of structures with >1 elements.

Fixes #515.

R=rsc
CC=golang-dev
https://golang.org/cl/184079
This commit is contained in:
Adam Langley 2010-01-11 18:54:13 -08:00
parent 72b97e46a3
commit e7cceb85e4
2 changed files with 13 additions and 2 deletions

View File

@ -29,6 +29,9 @@ func newForkableWriter() *forkableWriter {
} }
func (f *forkableWriter) fork() (pre, post *forkableWriter) { func (f *forkableWriter) fork() (pre, post *forkableWriter) {
if f.pre != nil || f.post != nil {
panic("have already forked")
}
f.pre = newForkableWriter() f.pre = newForkableWriter()
f.post = newForkableWriter() f.post = newForkableWriter()
return f.pre, f.post 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) nn, err = f.post.writeTo(out)
n += nn n += nn
} }
@ -297,7 +300,9 @@ func marshalBody(out *forkableWriter, value reflect.Value, params fieldParameter
case *reflect.StructValue: case *reflect.StructValue:
t := v.Type().(*reflect.StructType) t := v.Type().(*reflect.StructType)
for i := 0; i < t.NumField(); i++ { 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 { if err != nil {
return return
} }

View File

@ -15,6 +15,11 @@ type intStruct struct {
A int A int
} }
type twoIntStruct struct {
A int
B int
}
type nestedStruct struct { type nestedStruct struct {
A intStruct A intStruct
} }
@ -48,6 +53,7 @@ func setPST(t *time.Time) *time.Time {
var marshalTests = []marshalTest{ var marshalTests = []marshalTest{
marshalTest{10, "02010a"}, marshalTest{10, "02010a"},
marshalTest{intStruct{64}, "3003020140"}, marshalTest{intStruct{64}, "3003020140"},
marshalTest{twoIntStruct{64, 65}, "3006020140020141"},
marshalTest{nestedStruct{intStruct{127}}, "3005300302017f"}, marshalTest{nestedStruct{intStruct{127}}, "3005300302017f"},
marshalTest{[]byte{1, 2, 3}, "0403010203"}, marshalTest{[]byte{1, 2, 3}, "0403010203"},
marshalTest{implicitTagTest{64}, "3003850140"}, marshalTest{implicitTagTest{64}, "3003850140"},