Skip to content

Commit

Permalink
Prevent null pointer exception in Sonarqube (#334)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gcmurphy committed Jul 9, 2019
1 parent 39f7e7b commit 4b59c94
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions output/formatter.go
Expand Up @@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions output/formatter_test.go
Expand Up @@ -32,7 +32,7 @@ var _ = Describe("Formatter", func() {
NumFound: 0,
},
}
want := sonarIssues{
want := &sonarIssues{
SonarIssues: []sonarIssue{
{
EngineID: "gosec",
Expand All @@ -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() {
Expand All @@ -80,7 +80,7 @@ var _ = Describe("Formatter", func() {
NumFound: 0,
},
}
want := sonarIssues{
want := &sonarIssues{
SonarIssues: []sonarIssue{
{
EngineID: "gosec",
Expand All @@ -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{
Expand All @@ -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() {
Expand Down Expand Up @@ -168,7 +168,7 @@ var _ = Describe("Formatter", func() {
NumFound: 0,
},
}
want := sonarIssues{
want := &sonarIssues{
SonarIssues: []sonarIssue{
{
EngineID: "gosec",
Expand Down Expand Up @@ -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))
})
})
})

0 comments on commit 4b59c94

Please sign in to comment.