]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/asn1: return error for unexported fields in Marshal, Unmarshal
authorHiroshi Ioka <hirochachacha@gmail.com>
Fri, 21 Oct 2016 00:00:07 +0000 (09:00 +0900)
committerRuss Cox <rsc@golang.org>
Thu, 3 Nov 2016 03:44:05 +0000 (03:44 +0000)
The old code cannot handle unexported fields, it panics.
The new code returns error instead.

Fixes #17462

Change-Id: I927fc46b21d60e86cb52e84c65f2122f9159b21d
Reviewed-on: https://go-review.googlesource.com/31540
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/encoding/asn1/asn1.go
src/encoding/asn1/asn1_test.go
src/encoding/asn1/marshal.go

index 2b5ad08551ec71692fc2a287c9df37aaa4fe7a3f..a442995e92dab3f0ef0ac1b038f157bc70b6cd91 100644 (file)
@@ -841,6 +841,13 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
        case reflect.Struct:
                structType := fieldType
 
+               for i := 0; i < structType.NumField(); i++ {
+                       if structType.Field(i).PkgPath != "" {
+                               err = StructuralError{"struct contains unexported fields"}
+                               return
+                       }
+               }
+
                if structType.NumField() > 0 &&
                        structType.Field(0).Type == rawContentsType {
                        bytes := bytes[initOffset:offset]
index 8ee46d4565300901d7d7f8092fb89897a960f4b4..9976656df8938aeb374b263f383dbd80a6b280c8 100644 (file)
@@ -967,7 +967,7 @@ func TestUnmarshalInvalidUTF8(t *testing.T) {
 func TestMarshalNilValue(t *testing.T) {
        nilValueTestData := []interface{}{
                nil,
-               struct{ v interface{} }{},
+               struct{ V interface{} }{},
        }
        for i, test := range nilValueTestData {
                if _, err := Marshal(test); err == nil {
@@ -975,3 +975,32 @@ func TestMarshalNilValue(t *testing.T) {
                }
        }
 }
+
+type unexported struct {
+       X int
+       y int
+}
+
+type exported struct {
+       X int
+       Y int
+}
+
+func TestUnexportedStructField(t *testing.T) {
+       want := StructuralError{"struct contains unexported fields"}
+
+       _, err := Marshal(unexported{X: 5, y: 1})
+       if err != want {
+               t.Errorf("got %v, want %v", err, want)
+       }
+
+       bs, err := Marshal(exported{X: 5, Y: 1})
+       if err != nil {
+               t.Fatal(err)
+       }
+       var u unexported
+       _, err = Unmarshal(bs, &u)
+       if err != want {
+               t.Errorf("got %v, want %v", err, want)
+       }
+}
index 444c7f364209ba397a4b5204b78b2baf7a3c652d..76d0b0c825f76b732eb92ffded8b6100e4c458f5 100644 (file)
@@ -427,6 +427,12 @@ func makeBody(value reflect.Value, params fieldParameters) (e encoder, err error
        case reflect.Struct:
                t := v.Type()
 
+               for i := 0; i < t.NumField(); i++ {
+                       if t.Field(i).PkgPath != "" {
+                               return nil, StructuralError{"struct contains unexported fields"}
+                       }
+               }
+
                startingField := 0
 
                n := t.NumField()