Skip to content

Commit 4b59c94

Browse files
authored
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
1 parent 39f7e7b commit 4b59c94

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

output/formatter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ func reportSonarqube(rootPaths []string, w io.Writer, data *reportInfo) error {
117117
return err
118118
}
119119

120-
func convertToSonarIssues(rootPaths []string, data *reportInfo) (sonarIssues, error) {
121-
var si sonarIssues
120+
func convertToSonarIssues(rootPaths []string, data *reportInfo) (*sonarIssues, error) {
121+
si := &sonarIssues{[]sonarIssue{}}
122122
for _, issue := range data.Issues {
123123
var sonarFilePath string
124124
for _, rootPath := range rootPaths {

output/formatter_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var _ = Describe("Formatter", func() {
3232
NumFound: 0,
3333
},
3434
}
35-
want := sonarIssues{
35+
want := &sonarIssues{
3636
SonarIssues: []sonarIssue{
3737
{
3838
EngineID: "gosec",
@@ -56,7 +56,7 @@ var _ = Describe("Formatter", func() {
5656

5757
issues, err := convertToSonarIssues([]string{rootPath}, data)
5858
Expect(err).ShouldNot(HaveOccurred())
59-
Expect(issues).To(Equal(want))
59+
Expect(*issues).To(Equal(*want))
6060
})
6161

6262
It("it should parse the report info with files in subfolders", func() {
@@ -80,7 +80,7 @@ var _ = Describe("Formatter", func() {
8080
NumFound: 0,
8181
},
8282
}
83-
want := sonarIssues{
83+
want := &sonarIssues{
8484
SonarIssues: []sonarIssue{
8585
{
8686
EngineID: "gosec",
@@ -104,7 +104,7 @@ var _ = Describe("Formatter", func() {
104104

105105
issues, err := convertToSonarIssues([]string{rootPath}, data)
106106
Expect(err).ShouldNot(HaveOccurred())
107-
Expect(issues).To(Equal(want))
107+
Expect(*issues).To(Equal(*want))
108108
})
109109
It("it should not parse the report info for files from other projects", func() {
110110
data := &reportInfo{
@@ -127,15 +127,15 @@ var _ = Describe("Formatter", func() {
127127
NumFound: 0,
128128
},
129129
}
130-
want := sonarIssues{
131-
SonarIssues: nil,
130+
want := &sonarIssues{
131+
SonarIssues: []sonarIssue{},
132132
}
133133

134134
rootPath := "/home/src/project2"
135135

136136
issues, err := convertToSonarIssues([]string{rootPath}, data)
137137
Expect(err).ShouldNot(HaveOccurred())
138-
Expect(issues).To(Equal(want))
138+
Expect(*issues).To(Equal(*want))
139139
})
140140

141141
It("it should parse the report info for multiple projects projects", func() {
@@ -168,7 +168,7 @@ var _ = Describe("Formatter", func() {
168168
NumFound: 0,
169169
},
170170
}
171-
want := sonarIssues{
171+
want := &sonarIssues{
172172
SonarIssues: []sonarIssue{
173173
{
174174
EngineID: "gosec",
@@ -207,7 +207,7 @@ var _ = Describe("Formatter", func() {
207207

208208
issues, err := convertToSonarIssues(rootPaths, data)
209209
Expect(err).ShouldNot(HaveOccurred())
210-
Expect(issues).To(Equal(want))
210+
Expect(*issues).To(Equal(*want))
211211
})
212212
})
213213
})

0 commit comments

Comments
 (0)