Skip to content

Commit bf71145

Browse files
Copilotfriggeri
andcommitted
Fix Go CLIArgs to create defensive copy and add test for slice isolation
Co-authored-by: friggeri <106686+friggeri@users.noreply.github.com>
1 parent 73f43b1 commit bf71145

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

go/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func NewClient(options *ClientOptions) *Client {
144144
opts.CLIPath = options.CLIPath
145145
}
146146
if options.CLIArgs != nil {
147-
opts.CLIArgs = options.CLIArgs
147+
opts.CLIArgs = append([]string{}, options.CLIArgs...)
148148
}
149149
if options.Cwd != "" {
150150
opts.Cwd = options.Cwd

go/client_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,24 @@ func TestClient_CLIArgs(t *testing.T) {
362362
t.Errorf("Expected 0 CLI args, got %d", len(client.options.CLIArgs))
363363
}
364364
})
365+
366+
t.Run("should store a copy of CLIArgs slice", func(t *testing.T) {
367+
args := []string{"--custom-flag"}
368+
client := NewClient(&ClientOptions{
369+
CLIArgs: args,
370+
})
371+
372+
// Modify the original slice
373+
args = append(args, "--another-flag")
374+
375+
// The client's copy should not be affected
376+
if len(client.options.CLIArgs) != 1 {
377+
t.Errorf("Expected 1 CLI arg, got %d", len(client.options.CLIArgs))
378+
}
379+
if client.options.CLIArgs[0] != "--custom-flag" {
380+
t.Errorf("Expected first arg to be '--custom-flag', got %q", client.options.CLIArgs[0])
381+
}
382+
})
365383
}
366384

367385
func TestClient_EnvOptions(t *testing.T) {

0 commit comments

Comments
 (0)