aboutsummaryrefslogtreecommitdiff
path: root/src/debug
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2022-02-08 12:23:50 -0500
committerBryan Mills <bcmills@google.com>2022-02-09 19:44:03 +0000
commit9cec77ac11b012283e654b423cf85cf9976bedd9 (patch)
tree196b294e0c4979c7c1f7b4c6bd950080e05aef7f /src/debug
parentbe0d049a42ee4b07bfb71acb5e8f7c3d2735049a (diff)
downloadgo-9cec77ac11b012283e654b423cf85cf9976bedd9.tar.xz
runtime/debug: replace (*BuildInfo).Marshal methods with Parse and String
Since a String method cannot return an error, escape fields that may contain unsanitized values, and unescape them during parsing. Add a fuzz test to verify that calling the String method on any BuildInfo returned by Parse produces a string that parses to the same BuildInfo. (Note that this doesn't ensure that String always produces a parseable input: we assume that a user constructing a BuildInfo provides valid paths and versions, so we don't bother to escape those. It also doesn't ensure that ParseBuildInfo accepts all inputs that ought to be valid.) Fixes #51026 Change-Id: Ida18010ce47622cfedb1494060f32bd7705df014 Reviewed-on: https://go-review.googlesource.com/c/go/+/384154 Trust: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org>
Diffstat (limited to 'src/debug')
-rw-r--r--src/debug/buildinfo/buildinfo.go4
-rw-r--r--src/debug/buildinfo/buildinfo_test.go10
2 files changed, 6 insertions, 8 deletions
diff --git a/src/debug/buildinfo/buildinfo.go b/src/debug/buildinfo/buildinfo.go
index 2c0200e8dc..8de03ff106 100644
--- a/src/debug/buildinfo/buildinfo.go
+++ b/src/debug/buildinfo/buildinfo.go
@@ -75,8 +75,8 @@ func Read(r io.ReaderAt) (*BuildInfo, error) {
if err != nil {
return nil, err
}
- bi := &BuildInfo{}
- if err := bi.UnmarshalText([]byte(mod)); err != nil {
+ bi, err := debug.ParseBuildInfo(mod)
+ if err != nil {
return nil, err
}
bi.GoVersion = vers
diff --git a/src/debug/buildinfo/buildinfo_test.go b/src/debug/buildinfo/buildinfo_test.go
index 8346be0109..ac71626fda 100644
--- a/src/debug/buildinfo/buildinfo_test.go
+++ b/src/debug/buildinfo/buildinfo_test.go
@@ -212,12 +212,10 @@ func TestReadFile(t *testing.T) {
} else {
if tc.wantErr != "" {
t.Fatalf("unexpected success; want error containing %q", tc.wantErr)
- } else if got, err := info.MarshalText(); err != nil {
- t.Fatalf("unexpected error marshaling BuildInfo: %v", err)
- } else if got := cleanOutputForComparison(string(got)); got != tc.want {
- if got != tc.want {
- t.Fatalf("got:\n%s\nwant:\n%s", got, tc.want)
- }
+ }
+ got := info.String()
+ if clean := cleanOutputForComparison(string(got)); got != tc.want && clean != tc.want {
+ t.Fatalf("got:\n%s\nwant:\n%s", got, tc.want)
}
}
})