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
This commit is contained in:
Grant Murphy 2019-07-09 13:36:09 +10:00 committed by GitHub
parent 39f7e7b9e0
commit 4b59c94808
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 11 deletions

View file

@ -117,8 +117,8 @@ func reportSonarqube(rootPaths []string, w io.Writer, data *reportInfo) error {
return err return err
} }
func convertToSonarIssues(rootPaths []string, data *reportInfo) (sonarIssues, error) { func convertToSonarIssues(rootPaths []string, data *reportInfo) (*sonarIssues, error) {
var si sonarIssues si := &sonarIssues{[]sonarIssue{}}
for _, issue := range data.Issues { for _, issue := range data.Issues {
var sonarFilePath string var sonarFilePath string
for _, rootPath := range rootPaths { for _, rootPath := range rootPaths {

View file

@ -32,7 +32,7 @@ var _ = Describe("Formatter", func() {
NumFound: 0, NumFound: 0,
}, },
} }
want := sonarIssues{ want := &sonarIssues{
SonarIssues: []sonarIssue{ SonarIssues: []sonarIssue{
{ {
EngineID: "gosec", EngineID: "gosec",
@ -56,7 +56,7 @@ var _ = Describe("Formatter", func() {
issues, err := convertToSonarIssues([]string{rootPath}, data) issues, err := convertToSonarIssues([]string{rootPath}, data)
Expect(err).ShouldNot(HaveOccurred()) 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() { It("it should parse the report info with files in subfolders", func() {
@ -80,7 +80,7 @@ var _ = Describe("Formatter", func() {
NumFound: 0, NumFound: 0,
}, },
} }
want := sonarIssues{ want := &sonarIssues{
SonarIssues: []sonarIssue{ SonarIssues: []sonarIssue{
{ {
EngineID: "gosec", EngineID: "gosec",
@ -104,7 +104,7 @@ var _ = Describe("Formatter", func() {
issues, err := convertToSonarIssues([]string{rootPath}, data) issues, err := convertToSonarIssues([]string{rootPath}, data)
Expect(err).ShouldNot(HaveOccurred()) 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() { It("it should not parse the report info for files from other projects", func() {
data := &reportInfo{ data := &reportInfo{
@ -127,15 +127,15 @@ var _ = Describe("Formatter", func() {
NumFound: 0, NumFound: 0,
}, },
} }
want := sonarIssues{ want := &sonarIssues{
SonarIssues: nil, SonarIssues: []sonarIssue{},
} }
rootPath := "/home/src/project2" rootPath := "/home/src/project2"
issues, err := convertToSonarIssues([]string{rootPath}, data) issues, err := convertToSonarIssues([]string{rootPath}, data)
Expect(err).ShouldNot(HaveOccurred()) 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() { It("it should parse the report info for multiple projects projects", func() {
@ -168,7 +168,7 @@ var _ = Describe("Formatter", func() {
NumFound: 0, NumFound: 0,
}, },
} }
want := sonarIssues{ want := &sonarIssues{
SonarIssues: []sonarIssue{ SonarIssues: []sonarIssue{
{ {
EngineID: "gosec", EngineID: "gosec",
@ -207,7 +207,7 @@ var _ = Describe("Formatter", func() {
issues, err := convertToSonarIssues(rootPaths, data) issues, err := convertToSonarIssues(rootPaths, data)
Expect(err).ShouldNot(HaveOccurred()) Expect(err).ShouldNot(HaveOccurred())
Expect(issues).To(Equal(want)) Expect(*issues).To(Equal(*want))
}) })
}) })
}) })