Skip to content

Commit 24c5d0d

Browse files
author
Enda Phelan
committed
fix: check if http.Response is nil before closing body
1 parent cd7a643 commit 24c5d0d

File tree

15 files changed

+122
-29
lines changed

15 files changed

+122
-29
lines changed

pkg/cmd/kafka/consumergroup/delete/delete.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ func runCmd(opts *Options) error {
9292
ctx := context.Background()
9393

9494
_, httpRes, err := api.GroupsApi.GetConsumerGroupById(ctx, opts.id).Execute()
95-
defer httpRes.Body.Close()
95+
if httpRes != nil {
96+
defer httpRes.Body.Close()
97+
}
9698

9799
cgIDPair := localize.NewEntry("ID", opts.id)
98100
kafkaNameTmplPair := localize.NewEntry("InstanceName", kafkaInstance.GetName())
@@ -122,14 +124,15 @@ func runCmd(opts *Options) error {
122124
}
123125

124126
httpRes, err = api.GroupsApi.DeleteConsumerGroupById(ctx, opts.id).Execute()
127+
if httpRes != nil {
128+
defer httpRes.Body.Close()
129+
}
125130

126131
if err != nil {
127132
if httpRes == nil {
128133
return err
129134
}
130135

131-
defer httpRes.Body.Close()
132-
133136
operationTmplPair := localize.NewEntry("Operation", "delete")
134137

135138
switch httpRes.StatusCode {

pkg/cmd/kafka/consumergroup/describe/describe.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ func runCmd(opts *Options) error {
114114
ctx := context.Background()
115115

116116
consumerGroupData, httpRes, err := api.GroupsApi.GetConsumerGroupById(ctx, opts.id).Execute()
117-
defer httpRes.Body.Close()
117+
if httpRes != nil {
118+
defer httpRes.Body.Close()
119+
}
120+
118121
if err != nil {
119122
if httpRes == nil {
120123
return err

pkg/cmd/kafka/consumergroup/list/list.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ func runList(opts *Options) (err error) {
133133
req = req.Page(opts.page)
134134

135135
consumerGroupData, httpRes, err := req.Execute()
136-
defer httpRes.Body.Close()
136+
if httpRes != nil {
137+
defer httpRes.Body.Close()
138+
}
137139
if err != nil {
138140
if httpRes == nil {
139141
return err

pkg/cmd/kafka/create/create.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ func runCreate(opts *Options) error {
180180
a = a.KafkaRequestPayload(*payload)
181181
a = a.Async(true)
182182
response, httpRes, err := a.Execute()
183-
defer httpRes.Body.Close()
183+
if httpRes != nil {
184+
defer httpRes.Body.Close()
185+
}
184186

185187
if httpRes.StatusCode == http.StatusBadRequest {
186188
return errors.New(opts.localizer.MustLocalize("kafka.create.error.conflictError", localize.NewEntry("Name", payload.Name)))
@@ -295,7 +297,10 @@ func promptKafkaPayload(opts *Options) (payload *kafkamgmtclient.KafkaRequestPay
295297

296298
// fetch all cloud available providers
297299
cloudProviderResponse, httpRes, err := api.Kafka().GetCloudProviders(context.Background()).Execute()
298-
defer httpRes.Body.Close()
300+
if httpRes != nil {
301+
defer httpRes.Body.Close()
302+
}
303+
299304
if err != nil {
300305
return nil, err
301306
}

pkg/cmd/kafka/describe/describe.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"errors"
7+
"net/http"
78

89
flagutil "github.com/redhat-developer/app-services-cli/pkg/cmdutil/flags"
910
"github.com/redhat-developer/app-services-cli/pkg/connection"
@@ -103,14 +104,21 @@ func runDescribe(opts *Options) error {
103104
api := connection.API()
104105

105106
var kafkaInstance *kafkamgmtclient.KafkaRequest
107+
var httpRes *http.Response
106108
ctx := context.Background()
107109
if opts.name != "" {
108-
kafkaInstance, _, err = kafka.GetKafkaByName(ctx, api.Kafka(), opts.name)
110+
kafkaInstance, httpRes, err = kafka.GetKafkaByName(ctx, api.Kafka(), opts.name)
111+
if httpRes != nil {
112+
defer httpRes.Body.Close()
113+
}
109114
if err != nil {
110115
return err
111116
}
112117
} else {
113-
kafkaInstance, _, err = kafka.GetKafkaByID(ctx, api.Kafka(), opts.id)
118+
kafkaInstance, httpRes, err = kafka.GetKafkaByID(ctx, api.Kafka(), opts.id)
119+
if httpRes != nil {
120+
defer httpRes.Body.Close()
121+
}
114122
if err != nil {
115123
return err
116124
}

pkg/cmd/kafka/topic/create/create.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ func runCmd(opts *Options) error {
172172
createTopicReq = createTopicReq.NewTopicInput(topicInput)
173173

174174
response, httpRes, err := createTopicReq.Execute()
175-
defer httpRes.Body.Close()
175+
if httpRes != nil {
176+
defer httpRes.Body.Close()
177+
}
176178

177179
if err != nil {
178180
if httpRes == nil {

pkg/cmd/kafka/topic/delete/delete.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ func runCmd(opts *Options) error {
9797

9898
// perform delete topic API request
9999
_, httpRes, err := api.TopicsApi.GetTopic(context.Background(), opts.topicName).Execute()
100-
defer httpRes.Body.Close()
100+
if httpRes != nil {
101+
defer httpRes.Body.Close()
102+
}
101103

102104
topicNameTmplPair := localize.NewEntry("TopicName", opts.topicName)
103105
kafkaNameTmplPair := localize.NewEntry("InstanceName", kafkaInstance.GetName())
@@ -126,6 +128,9 @@ func runCmd(opts *Options) error {
126128

127129
// perform delete topic API request
128130
httpRes, err = api.TopicsApi.DeleteTopic(context.Background(), opts.topicName).Execute()
131+
if httpRes != nil {
132+
defer httpRes.Body.Close()
133+
}
129134
if err != nil {
130135
if httpRes == nil {
131136
return err
@@ -147,7 +152,6 @@ func runCmd(opts *Options) error {
147152
return err
148153
}
149154
}
150-
defer httpRes.Body.Close()
151155

152156
opts.Logger.Info(opts.localizer.MustLocalize("kafka.topic.delete.log.info.topicDeleted", topicNameTmplPair, kafkaNameTmplPair))
153157

pkg/cmd/kafka/topic/describe/describe.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ func runCmd(opts *Options) error {
105105

106106
// fetch the topic
107107
topicResponse, httpRes, err := api.TopicsApi.GetTopic(context.Background(), opts.name).Execute()
108-
defer httpRes.Body.Close()
108+
if httpRes != nil {
109+
defer httpRes.Body.Close()
110+
}
109111

110112
if err != nil {
111113
if httpRes == nil {

pkg/cmd/kafka/topic/list/list.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ func runCmd(opts *Options) error {
137137
a = a.Page(opts.page)
138138

139139
topicData, httpRes, err := a.Execute()
140+
if httpRes != nil {
141+
defer httpRes.Body.Close()
142+
}
140143
if err != nil {
141144
if httpRes == nil {
142145
return err
@@ -158,8 +161,6 @@ func runCmd(opts *Options) error {
158161
}
159162
}
160163

161-
defer httpRes.Body.Close()
162-
163164
if topicData.GetTotal() == 0 && opts.output == "" {
164165
opts.Logger.Info(opts.localizer.MustLocalize("kafka.topic.list.log.info.noTopics", localize.NewEntry("InstanceName", kafkaInstance.GetName())))
165166

pkg/cmd/kafka/topic/update/update.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ func runCmd(opts *Options) error {
220220
var needsUpdate bool
221221

222222
topic, httpRes, err := api.TopicsApi.GetTopic(context.Background(), opts.name).Execute()
223-
defer httpRes.Body.Close()
223+
if httpRes != nil {
224+
defer httpRes.Body.Close()
225+
}
224226

225227
topicNameTmplPair := localize.NewEntry("TopicName", opts.name)
226228
kafkaNameTmplPair := localize.NewEntry("InstanceName", kafkaInstance.GetName())
@@ -274,7 +276,9 @@ func runCmd(opts *Options) error {
274276

275277
// update the topic
276278
response, httpRes, err := updateTopicReq.Execute()
277-
defer httpRes.Body.Close()
279+
if httpRes != nil {
280+
defer httpRes.Body.Close()
281+
}
278282

279283
// handle error
280284
if err != nil {
@@ -326,7 +330,9 @@ func runInteractivePrompt(opts *Options) (err error) {
326330

327331
// check if topic exists
328332
topic, httpRes, err := api.TopicsApi.GetTopic(context.Background(), opts.name).Execute()
329-
defer httpRes.Body.Close()
333+
if httpRes != nil {
334+
defer httpRes.Body.Close()
335+
}
330336

331337
topicNameTmplPair := localize.NewEntry("TopicName", opts.name)
332338
kafkaNameTmplPair := localize.NewEntry("InstanceName", kafkaInstance.GetName())

0 commit comments

Comments
 (0)