From 061c800c9c2dbf46db848f90fff42ad466d3a5fd Mon Sep 17 00:00:00 2001 From: Gordon Bleux <33967640+UiP9AV6Y@users.noreply.github.com> Date: Wed, 13 Mar 2024 21:28:20 +0100 Subject: [PATCH] avoid NPE in BuildInfo.Print create empty data containers when no information is available to prevent a nil pointer access --- buildinfo.go | 21 +++++++++++++----- buildinfo_test.go | 45 ++++++++++++++++++++++++++++++++++++++ testdata/print_full.golden | 6 +++++ testdata/print_nil.golden | 6 +++++ 4 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 testdata/print_full.golden create mode 100644 testdata/print_nil.golden diff --git a/buildinfo.go b/buildinfo.go index fa010d4..0ba0414 100644 --- a/buildinfo.go +++ b/buildinfo.go @@ -123,14 +123,23 @@ func (i *BuildInfo) JSON() ([]byte, error) { // Print returns version- and environment information. func (i *BuildInfo) Print(program string) string { + v := i.VersionInfo + if v == nil { + v = NewVersionInfo() + } + e := i.EnvironmentInfo + if e == nil { + e = NewEnvironmentInfo() + } + return fmt.Sprintf(infoFmt, program, - i.VersionInfo.Version, - i.VersionInfo.Branch, - i.VersionInfo.ShortRevision(), - i.EnvironmentInfo.User, - i.EnvironmentInfo.Host, - i.EnvironmentInfo.Date, + v.Version, + v.Branch, + v.ShortRevision(), + e.User, + e.Host, + e.Date, GoVersion, GoOS+"/"+GoArch, ) diff --git a/buildinfo_test.go b/buildinfo_test.go index 7105c72..3428d9e 100644 --- a/buildinfo_test.go +++ b/buildinfo_test.go @@ -5,6 +5,7 @@ import ( "time" "gotest.tools/v3/assert" + "gotest.tools/v3/golden" ) func TestParse(t *testing.T) { @@ -240,3 +241,47 @@ func TestBuildInfoEqual(t *testing.T) { }) } } + +func TestBuildInfoPrint(t *testing.T) { + type testCase struct { + have *BuildInfo + want string + } + + testCases := map[string]testCase{ + "nil": { + have: &BuildInfo{ + VersionInfo: nil, + EnvironmentInfo: &EnvironmentInfo{ + User: "root", + Host: "example.com", + Date: time.Unix(123456790, 0), + }, + }, + want: "print_nil.golden", + }, + "full": { + have: &BuildInfo{ + VersionInfo: &VersionInfo{ + Version: "1.2.3", + Revision: "deadbeef", + Branch: "unstable", + }, + EnvironmentInfo: &EnvironmentInfo{ + User: "root", + Host: "example.com", + Date: time.Unix(123456790, 0), + }, + }, + want: "print_full.golden", + }, + } + + for ctx, tc := range testCases { + t.Run(ctx, func(t *testing.T) { + got := tc.have.Print("test") + + golden.Assert(t, got, tc.want) + }) + } +} diff --git a/testdata/print_full.golden b/testdata/print_full.golden new file mode 100644 index 0000000..52262c2 --- /dev/null +++ b/testdata/print_full.golden @@ -0,0 +1,6 @@ +test, version 1.2.3 (branch: unstable, revision: deadbeef) + build user: root + build host: example.com + build date: 1973-11-29 21:33:10 +0000 UTC + go version: go1.19.13 + platform: linux/amd64 \ No newline at end of file diff --git a/testdata/print_nil.golden b/testdata/print_nil.golden new file mode 100644 index 0000000..809843b --- /dev/null +++ b/testdata/print_nil.golden @@ -0,0 +1,6 @@ +test, version 0.0.0 (branch: trunk, revision: HEAD) + build user: root + build host: example.com + build date: 1973-11-29 21:33:10 +0000 UTC + go version: go1.19.13 + platform: linux/amd64 \ No newline at end of file