Skip to content

Commit 55ebf28

Browse files
Copilotcanack
andcommitted
Fix array/slice bounds checking and nil pointer dereferences
Co-authored-by: canack <[email protected]>
1 parent 1c08157 commit 55ebf28

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

internal/github/usecase/usecase_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ func TestUseCase_TriggerWorkflow(t *testing.T) {
7171
})
7272
if err != nil {
7373
t.Error(err)
74+
return // Exit early if there's an error
75+
}
76+
77+
if workflow == nil || workflow.Workflow == nil {
78+
t.Error("workflow or workflow.Workflow is nil")
79+
return
7480
}
7581

7682
for i, w := range workflow.Workflow.Inputs {

internal/terminal/handler/ghtrigger.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (m *ModelGithubTrigger) View() string {
212212

213213
var selectedRow = m.tableTrigger.SelectedRow()
214214
var selector = m.emptySelector()
215-
if len(m.tableTrigger.Rows()) > 0 {
215+
if len(m.tableTrigger.Rows()) > 0 && len(selectedRow) > 1 {
216216
if selectedRow[1] == "input" {
217217
selector = m.inputSelector()
218218
} else {
@@ -227,6 +227,10 @@ func (m *ModelGithubTrigger) View() string {
227227

228228
func (m *ModelGithubTrigger) switchBetweenInputAndTable() {
229229
var selectedRow = m.tableTrigger.SelectedRow()
230+
231+
if len(selectedRow) < 5 {
232+
return // Insufficient data to operate safely
233+
}
230234

231235
if selectedRow[1] == "input" || selectedRow[1] == "bool" {
232236
m.textInput.Focus()
@@ -235,7 +239,7 @@ func (m *ModelGithubTrigger) switchBetweenInputAndTable() {
235239
m.textInput.Blur()
236240
m.tableTrigger.Focus()
237241
}
238-
m.textInput.SetValue(m.tableTrigger.SelectedRow()[4])
242+
m.textInput.SetValue(selectedRow[4])
239243
m.textInput.SetCursor(len(m.textInput.Value()))
240244
}
241245

@@ -246,8 +250,8 @@ func (m *ModelGithubTrigger) inputController(_ context.Context) {
246250

247251
if len(m.tableTrigger.Rows()) > 0 {
248252
var selectedRow = m.tableTrigger.SelectedRow()
249-
if len(selectedRow) == 0 {
250-
return
253+
if len(selectedRow) < 5 {
254+
return // Need at least 5 elements for safe access
251255
}
252256

253257
switch selectedRow[1] {
@@ -297,14 +301,14 @@ func (m *ModelGithubTrigger) inputController(_ context.Context) {
297301
var selectedRow = m.tableTrigger.SelectedRow()
298302
var rows = m.tableTrigger.Rows()
299303

300-
if len(selectedRow) == 0 || len(rows) == 0 {
304+
if len(selectedRow) < 5 || len(rows) == 0 || m.optionCursor >= len(m.optionValues) {
301305
return
302306
}
303307
if fmt.Sprintf("%d", choice.ID) == selectedRow[0] {
304308
m.workflowContent.Choices[i].SetValue(m.optionValues[m.optionCursor])
305309

306310
for i, row := range rows {
307-
if row[0] == selectedRow[0] {
311+
if len(row) >= 5 && row[0] == selectedRow[0] {
308312
rows[i][4] = m.optionValues[m.optionCursor]
309313
}
310314
}
@@ -317,14 +321,14 @@ func (m *ModelGithubTrigger) inputController(_ context.Context) {
317321
for i, boolean := range m.workflowContent.Boolean {
318322
var selectedRow = m.tableTrigger.SelectedRow()
319323
var rows = m.tableTrigger.Rows()
320-
if len(selectedRow) == 0 || len(rows) == 0 {
324+
if len(selectedRow) < 5 || len(rows) == 0 || m.optionCursor >= len(m.optionValues) {
321325
return
322326
}
323327
if fmt.Sprintf("%d", boolean.ID) == selectedRow[0] {
324328
m.workflowContent.Boolean[i].SetValue(m.optionValues[m.optionCursor])
325329

326330
for i, row := range rows {
327-
if row[0] == selectedRow[0] {
331+
if len(row) >= 5 && row[0] == selectedRow[0] {
328332
rows[i][4] = m.optionValues[m.optionCursor]
329333
}
330334
}
@@ -341,7 +345,7 @@ func (m *ModelGithubTrigger) inputController(_ context.Context) {
341345

342346
var selectedRow = m.tableTrigger.SelectedRow()
343347
var rows = m.tableTrigger.Rows()
344-
if len(selectedRow) == 0 || len(rows) == 0 {
348+
if len(selectedRow) < 5 || len(rows) == 0 {
345349
return
346350
}
347351

@@ -351,7 +355,7 @@ func (m *ModelGithubTrigger) inputController(_ context.Context) {
351355
m.workflowContent.Inputs[i].SetValue(m.textInput.Value())
352356

353357
for i, row := range rows {
354-
if row[0] == selectedRow[0] {
358+
if len(row) >= 5 && row[0] == selectedRow[0] {
355359
rows[i][4] = m.textInput.Value()
356360
}
357361
}
@@ -366,7 +370,7 @@ func (m *ModelGithubTrigger) inputController(_ context.Context) {
366370
m.workflowContent.KeyVals[i].SetValue(m.textInput.Value())
367371

368372
for i, row := range rows {
369-
if row[0] == selectedRow[0] {
373+
if len(row) >= 5 && row[0] == selectedRow[0] {
370374
rows[i][4] = m.textInput.Value()
371375
}
372376
}

internal/terminal/handler/taboptions.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,18 @@ func (o *ModelTabOptions) resetOptionsWithOriginal() {
141141
o.isTabSelected = true
142142
o.timer = 3
143143
for o.timer >= 0 {
144-
o.optionsAction[0] = fmt.Sprintf("> %ds", o.timer)
144+
if len(o.optionsAction) > 0 {
145+
o.optionsAction[0] = fmt.Sprintf("> %ds", o.timer)
146+
}
145147
time.Sleep(1 * time.Second)
146148
o.timer--
147149
o.skeleton.TriggerUpdate()
148150
}
149151
o.modelLock = false
150152
o.switchToPreviousError()
151-
o.optionsAction[0] = string(StatusIdle)
153+
if len(o.optionsAction) > 0 {
154+
o.optionsAction[0] = string(StatusIdle)
155+
}
152156
o.cursor = 0
153157
o.isTabSelected = false
154158
}
@@ -163,8 +167,12 @@ func (o *ModelTabOptions) updateCursor(cursor int) {
163167

164168
func (o *ModelTabOptions) SetStatus(status OptionStatus) {
165169
o.optionStatus = status
166-
o.options[0] = status.String()
167-
o.optionsAction[0] = status.String()
170+
if len(o.options) > 0 {
171+
o.options[0] = status.String()
172+
}
173+
if len(o.optionsAction) > 0 {
174+
o.optionsAction[0] = status.String()
175+
}
168176
}
169177

170178
func (o *ModelTabOptions) AddOption(option string, action func()) {
@@ -177,6 +185,9 @@ func (o *ModelTabOptions) AddOption(option string, action func()) {
177185
}
178186

179187
func (o *ModelTabOptions) getOptionMessage() string {
188+
if o.cursor >= len(o.options) {
189+
return ""
190+
}
180191
option := o.options[o.cursor]
181192
option = strings.TrimPrefix(option, fmt.Sprintf("%d) ", o.cursor))
182193
return option
@@ -206,7 +217,9 @@ func (o *ModelTabOptions) switchToPreviousError() {
206217
}
207218

208219
func (o *ModelTabOptions) executeOption() {
209-
go o.optionsWithFunc[o.cursor]()
220+
if fn, exists := o.optionsWithFunc[o.cursor]; exists {
221+
go fn()
222+
}
210223
o.cursor = 0
211224
o.timer = -1
212225
}

0 commit comments

Comments
 (0)