diff options
author | Russ Cox <rsc@golang.org> | 2017-09-06 22:01:50 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2017-09-22 20:08:00 +0000 |
commit | 11e083e44b315d3135fbd11691b34fb4ac1377fb (patch) | |
tree | fd1c45b540e078a7d36e7f81206773432e7193e7 | |
parent | 043302c56d8a6f021e097e63ba25e734df1c4cd6 (diff) | |
download | go-11e083e44b315d3135fbd11691b34fb4ac1377fb.tar.gz go-11e083e44b315d3135fbd11691b34fb4ac1377fb.zip |
[dev.boringcrypto.go1.8] cmd/compile: refine BoringCrypto kludge
Did not consider these fields being embedded or adopted
into structs defined in other packages, but that's possible too.
Refine the import path check to account for that.
Fixes 'go test -short golang.org/x/crypto/ssh' but also
adds a new test in internal/boring for the same problem.
Change-Id: Ied2d04fe2b0ac3b0a34f07bc8dfc50fc203abb9f
Reviewed-on: https://go-review.googlesource.com/62152
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Adam Langley <agl@golang.org>
Reviewed-on: https://go-review.googlesource.com/65479
Run-TryBot: Russ Cox <rsc@golang.org>
-rw-r--r-- | src/cmd/compile/internal/gc/reflect.go | 11 | ||||
-rw-r--r-- | src/internal/boringtest/boring.go | 8 | ||||
-rw-r--r-- | src/internal/boringtest/boring_test.go | 47 |
3 files changed, 64 insertions, 2 deletions
diff --git a/src/cmd/compile/internal/gc/reflect.go b/src/cmd/compile/internal/gc/reflect.go index 0341ccabfa..06083cafc7 100644 --- a/src/cmd/compile/internal/gc/reflect.go +++ b/src/cmd/compile/internal/gc/reflect.go @@ -1321,8 +1321,15 @@ ok: // As the name suggests, this is an awful kludge, but it is // limited to the dev.boringcrypto branch and avoids // much more invasive effects elsewhere. - omitFieldForAwfulBoringCryptoKludge := func(t *types.Field) bool { - return strings.HasPrefix(myimportpath, "crypto/") && t.Sym != nil && t.Sym.Name == "boring" + omitFieldForAwfulBoringCryptoKludge := func(t *Field) bool { + if t.Sym == nil || t.Sym.Name != "boring" || t.Sym.Pkg == nil { + return false + } + path := t.Sym.Pkg.Path + if t.Sym.Pkg == localpkg { + path = myimportpath + } + return strings.HasPrefix(path, "crypto/") } n := 0 diff --git a/src/internal/boringtest/boring.go b/src/internal/boringtest/boring.go new file mode 100644 index 0000000000..bea1276e69 --- /dev/null +++ b/src/internal/boringtest/boring.go @@ -0,0 +1,8 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Nothing to see here but the tests. +// This file keeps 'go install internal/...' working. + +package boring diff --git a/src/internal/boringtest/boring_test.go b/src/internal/boringtest/boring_test.go new file mode 100644 index 0000000000..a6b07eda70 --- /dev/null +++ b/src/internal/boringtest/boring_test.go @@ -0,0 +1,47 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Like crypto/rsa/boring_test.go but outside the crypto/ tree. +// Tests what happens if a package outside the crypto/ tree +// "adopts" a struct definition. This happens in golang.org/x/crypto/ssh. + +package boring + +import ( + "crypto/rand" + "crypto/rsa" + "encoding/asn1" + "reflect" + "testing" +) + +type publicKey rsa.PublicKey + +func TestBoringASN1Marshal(t *testing.T) { + k, err := rsa.GenerateKey(rand.Reader, 128) + if err != nil { + t.Fatal(err) + } + pk := (*publicKey)(&k.PublicKey) + // This used to fail, because of the unexported 'boring' field. + // Now the compiler hides it [sic]. + _, err = asn1.Marshal(*pk) + if err != nil { + t.Fatal(err) + } +} + +func TestBoringDeepEqual(t *testing.T) { + k0, err := rsa.GenerateKey(rand.Reader, 128) + if err != nil { + t.Fatal(err) + } + k := (*publicKey)(&k0.PublicKey) + k2 := *k + rsa.EncryptPKCS1v15(rand.Reader, (*rsa.PublicKey)(&k2), []byte("hello")) // initialize hidden boring field + if !reflect.DeepEqual(k, &k2) { + // compiler should be hiding the boring field from reflection + t.Fatalf("DeepEqual compared boring fields") + } +} |