From 4b59c948083cd711b6a8aac8f32721b164899f57 Mon Sep 17 00:00:00 2001 From: Grant Murphy Date: Tue, 9 Jul 2019 13:36:09 +1000 Subject: [PATCH] Prevent null pointer exception in Sonarqube (#334) * fix(formatters) null value causes npe in sonarqube the json encoding of uninitialized arrays is null. this causes a npe in sonarqube tool. we should return an empty array rather than a null value here. relates to: #333 --- output/formatter.go | 4 ++-- output/formatter_test.go | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/output/formatter.go b/output/formatter.go index e66015b..428661a 100644 --- a/output/formatter.go +++ b/output/formatter.go @@ -117,8 +117,8 @@ func reportSonarqube(rootPaths []string, w io.Writer, data *reportInfo) error { return err } -func convertToSonarIssues(rootPaths []string, data *reportInfo) (sonarIssues, error) { - var si sonarIssues +func convertToSonarIssues(rootPaths []string, data *reportInfo) (*sonarIssues, error) { + si := &sonarIssues{[]sonarIssue{}} for _, issue := range data.Issues { var sonarFilePath string for _, rootPath := range rootPaths { diff --git a/output/formatter_test.go b/output/formatter_test.go index 339d166..d196087 100644 --- a/output/formatter_test.go +++ b/output/formatter_test.go @@ -32,7 +32,7 @@ var _ = Describe("Formatter", func() { NumFound: 0, }, } - want := sonarIssues{ + want := &sonarIssues{ SonarIssues: []sonarIssue{ { EngineID: "gosec", @@ -56,7 +56,7 @@ var _ = Describe("Formatter", func() { issues, err := convertToSonarIssues([]string{rootPath}, data) Expect(err).ShouldNot(HaveOccurred()) - Expect(issues).To(Equal(want)) + Expect(*issues).To(Equal(*want)) }) It("it should parse the report info with files in subfolders", func() { @@ -80,7 +80,7 @@ var _ = Describe("Formatter", func() { NumFound: 0, }, } - want := sonarIssues{ + want := &sonarIssues{ SonarIssues: []sonarIssue{ { EngineID: "gosec", @@ -104,7 +104,7 @@ var _ = Describe("Formatter", func() { issues, err := convertToSonarIssues([]string{rootPath}, data) Expect(err).ShouldNot(HaveOccurred()) - Expect(issues).To(Equal(want)) + Expect(*issues).To(Equal(*want)) }) It("it should not parse the report info for files from other projects", func() { data := &reportInfo{ @@ -127,15 +127,15 @@ var _ = Describe("Formatter", func() { NumFound: 0, }, } - want := sonarIssues{ - SonarIssues: nil, + want := &sonarIssues{ + SonarIssues: []sonarIssue{}, } rootPath := "/home/src/project2" issues, err := convertToSonarIssues([]string{rootPath}, data) Expect(err).ShouldNot(HaveOccurred()) - Expect(issues).To(Equal(want)) + Expect(*issues).To(Equal(*want)) }) It("it should parse the report info for multiple projects projects", func() { @@ -168,7 +168,7 @@ var _ = Describe("Formatter", func() { NumFound: 0, }, } - want := sonarIssues{ + want := &sonarIssues{ SonarIssues: []sonarIssue{ { EngineID: "gosec", @@ -207,7 +207,7 @@ var _ = Describe("Formatter", func() { issues, err := convertToSonarIssues(rootPaths, data) Expect(err).ShouldNot(HaveOccurred()) - Expect(issues).To(Equal(want)) + Expect(*issues).To(Equal(*want)) }) }) })