Skip to content

Commit a9e447d

Browse files
dmitshurgopherbot
authored andcommitted
x509roots/fallback: add //go:build go1.20 to bundle.go
Package fallback has no API; its only purpose is to automatically call x509.SetFallbackRoots with a set of fallback roots. That API was added in Go 1.20, hence the go1.20 build constraint in fallback.go. Add that constraint to bundle.go too, so that it fails to build rather than quietly being a no-op in Go 1.19. Also simplify Write(fmt.Sprintf()) into fmt.Fprintf while here. Add a temporary workaround for go.dev/issue/52287. It has no effect on the public API in this module. For golang/go#57792. For golang/go#52287. Change-Id: I1fe13f7d54b07b0b031e8bae685cffd7a8160165 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/505578 Auto-Submit: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]>
1 parent 64c3993 commit a9e447d

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

x509roots/fallback/bundle.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x509roots/fallback/fallback.go

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// license that can be found in the LICENSE file.
44

55
//go:build go1.20
6-
// +build go1.20
76

87
// Package fallback embeds a set of fallback X.509 trusted roots in the
98
// application by automatically invoking [x509.SetFallbackRoots]. This allows
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package goissue52287 is an empty internal package.
6+
// It exists only to work around go.dev/issue/52287 and
7+
// can be removed after Go 1.19 stops being supported.
8+
package goissue52287

x509roots/gen_fallback_bundle.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727

2828
const tmpl = `// Code generated by gen_fallback_bundle.go; DO NOT EDIT.
2929
30+
//go:build go1.20
31+
3032
package fallback
3133
3234
import "crypto/x509"
@@ -41,7 +43,7 @@ func mustParse(b []byte) []*x509.Certificate {
4143
break
4244
}
4345
if block.Type != "CERTIFICATE" {
44-
panic("unexpected PEM block type: "+block.Type)
46+
panic("unexpected PEM block type: " + block.Type)
4547
}
4648
cert, err := x509.ParseCertificate(block.Bytes)
4749
if err != nil {
@@ -97,9 +99,9 @@ func main() {
9799
return string(certs[i].X509.RawSubjectPublicKeyInfo) < string(certs[j].X509.RawSubjectPublicKeyInfo)
98100
})
99101

100-
b := bytes.NewBuffer(nil)
101-
b.Write([]byte(tmpl))
102-
b.Write([]byte("const pemRoots = `\n"))
102+
b := new(bytes.Buffer)
103+
b.WriteString(tmpl)
104+
fmt.Fprintln(b, "const pemRoots = `")
103105
for _, c := range certs {
104106
if len(c.Constraints) > 0 {
105107
// Until the constrained roots API lands, skip anything that has any
@@ -108,10 +110,10 @@ func main() {
108110
// new version.
109111
continue
110112
}
111-
b.Write([]byte(fmt.Sprintf("# %s\n# %x\n", c.X509.Subject.String(), sha256.Sum256(c.X509.Raw))))
113+
fmt.Fprintf(b, "# %s\n# %x\n", c.X509.Subject.String(), sha256.Sum256(c.X509.Raw))
112114
pem.Encode(b, &pem.Block{Type: "CERTIFICATE", Bytes: c.X509.Raw})
113115
}
114-
b.Write([]byte("`\n"))
116+
fmt.Fprintln(b, "`")
115117

116118
formatted, err := format.Source(b.Bytes())
117119
if err != nil {

0 commit comments

Comments
 (0)