Skip to content

Commit

Permalink
encoding/asn1: fix panic when Marshaling nil.
Browse files Browse the repository at this point in the history
Fixes #11127.

Change-Id: Ibcfc3a05e91fa4260d70b04bee2bbba2376bd313
Reviewed-on: https://go-review.googlesource.com/13923
Reviewed-by: Adam Langley <[email protected]>
  • Loading branch information
mibk authored and agl committed Aug 29, 2015
1 parent 805e56e commit fac1039
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/encoding/asn1/asn1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -940,3 +940,15 @@ func TestUnmarshalInvalidUTF8(t *testing.T) {
t.Fatalf("Expected error to mention %q but error was %q", expectedSubstring, err.Error())
}
}

func TestMarshalNilValue(t *testing.T) {
nilValueTestData := []interface{}{
nil,
struct{ v interface{} }{},
}
for i, test := range nilValueTestData {
if _, err := Marshal(test); err == nil {
t.Fatal("#%d: successfully marshaled nil value", i)
}
}
}
3 changes: 3 additions & 0 deletions src/encoding/asn1/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,9 @@ func marshalBody(out *forkableWriter, value reflect.Value, params fieldParameter
}

func marshalField(out *forkableWriter, v reflect.Value, params fieldParameters) (err error) {
if !v.IsValid() {
return fmt.Errorf("asn1: cannot marshal nil value")
}
// If the field is an interface{} then recurse into it.
if v.Kind() == reflect.Interface && v.Type().NumMethod() == 0 {
return marshalField(out, v.Elem(), params)
Expand Down

0 comments on commit fac1039

Please sign in to comment.