Skip to content

Commit a4bdbd1

Browse files
znullCopilot
andcommitted
Guard against empty pipeline in Pipeline.Start()
Pipeline.Start() indexes stageStarters[0] and p.stages[len-1] unconditionally — zero stages causes an index out of bounds panic. Return nil early when no stages have been added. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f0a5065 commit a4bdbd1

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

pipe/pipeline.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ func (p *Pipeline) Start(ctx context.Context) error {
268268
atomic.StoreUint32(&p.started, 1)
269269
ctx, p.cancel = context.WithCancel(ctx)
270270

271+
if len(p.stages) == 0 {
272+
return nil
273+
}
274+
271275
// We need to decide how to start the stages, especially what
272276
// pipes to use to connect adjacent stages (`os.Pipe()` vs.
273277
// `io.Pipe()`) based on the two stages' preferences.

pipe/pipeline_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ func TestMain(m *testing.M) {
2626
goleak.VerifyTestMain(m)
2727
}
2828

29+
func TestPipelineEmpty(t *testing.T) {
30+
t.Parallel()
31+
p := pipe.New()
32+
assert.NoError(t, p.Run(context.Background()))
33+
}
34+
2935
func TestPipelineFirstStageFailsToStart(t *testing.T) {
3036
t.Parallel()
3137
ctx := context.Background()

0 commit comments

Comments
 (0)