From f57379a647490c17a85c64c32e27a0eba7a6597d Mon Sep 17 00:00:00 2001 From: InfiniteLoopSpace <35842605+InfiniteLoopSpace@users.noreply.github.com> Date: Wed, 14 Nov 2018 13:24:33 +0100 Subject: [PATCH] Fix package tests. --- asn1/asn1.go | 6 +++++- asn1/asn1_test.go | 6 +++--- asn1/marshal.go | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/asn1/asn1.go b/asn1/asn1.go index a027e11..105a7f7 100644 --- a/asn1/asn1.go +++ b/asn1/asn1.go @@ -559,6 +559,10 @@ func parseTagAndLength(bytes []byte, initOffset int) (ret tagAndLength, offset i } return } + if len(bytes) <= reoffset+reTag.length { + err = asn1.StructuralError{"indefinete lenght: length too large"} + return + } reTag, reoffset, _ = parseTagAndLength(bytes, reoffset+reTag.length) } log.Println("indefinite length found (not DER)") @@ -928,7 +932,7 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam for i := 0; i < structType.NumField(); i++ { if structType.Field(i).PkgPath != "" { - err = asn1.StructuralError{"struct contains unexported fields"} + err = asn1.StructuralError{Msg: "struct contains unexported fields"} return } } diff --git a/asn1/asn1_test.go b/asn1/asn1_test.go index 6dbc83b..ebbc4b1 100644 --- a/asn1/asn1_test.go +++ b/asn1/asn1_test.go @@ -179,7 +179,7 @@ func TestBitString(t *testing.T) { } func TestBitStringAt(t *testing.T) { - bs := asn1.BitString{[]byte{0x82, 0x40}, 16} + bs := asn1.BitString{Bytes: []byte{0x82, 0x40}, BitLength: 16} if bs.At(0) != 1 { t.Error("#1: Failed") } @@ -217,7 +217,7 @@ var bitStringRightAlignTests = []bitStringRightAlignTest{ func TestBitStringRightAlign(t *testing.T) { for i, test := range bitStringRightAlignTests { - bs := asn1.BitString{test.in, test.inlen} + bs := asn1.BitString{Bytes: test.in, BitLength: test.inlen} out := bs.RightAlign() if !bytes.Equal(out, test.out) { t.Errorf("#%d got: %x want: %x", i, out, test.out) @@ -997,7 +997,7 @@ type exported struct { } func TestUnexportedStructField(t *testing.T) { - want := asn1.StructuralError{"struct contains unexported fields"} + want := asn1.StructuralError{Msg: "struct contains unexported fields"} _, err := Marshal(unexported{X: 5, y: 1}) if err != want { diff --git a/asn1/marshal.go b/asn1/marshal.go index e396706..0b30e63 100644 --- a/asn1/marshal.go +++ b/asn1/marshal.go @@ -446,7 +446,7 @@ func makeBody(value reflect.Value, params fieldParameters) (e encoder, err error for i := 0; i < t.NumField(); i++ { if t.Field(i).PkgPath != "" { - return nil, asn1.StructuralError{"struct contains unexported fields"} + return nil, asn1.StructuralError{Msg: "struct contains unexported fields"} } }