Skip to content

Commit bf220c0

Browse files
committed
Fix slice bounds error in Command methods for empty command arrays
- Add empty command handling in both Linux and macOS Command implementations - Return nil for empty command arrays instead of causing slice bounds panic - Update test expectation for empty command to expect false (nil command) - Fixes CI failure on macOS where empty command caused panic in command[0] access - Consistent behavior across platforms for edge case handling
1 parent 8ec3ad6 commit bf220c0

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

jail_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func TestJail_Command(t *testing.T) {
220220
{
221221
name: "empty command",
222222
args: []string{},
223-
want: true, // Should still return a command object
223+
want: false, // Empty command should return nil or be invalid
224224
},
225225
{
226226
name: "multiple args",

namespace/linux.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ func (l *Linux) Start() error {
108108
func (l *Linux) Command(command []string) *exec.Cmd {
109109
l.logger.Debug("Command called", "command", command)
110110

111+
// Handle empty command
112+
if len(command) == 0 {
113+
l.logger.Debug("Empty command provided, returning nil")
114+
return nil // Return nil for empty command
115+
}
116+
111117
// Create command with ip netns exec
112118
l.logger.Debug("Creating command with namespace", "namespace", l.namespace)
113119
cmdArgs := []string{"ip", "netns", "exec", l.namespace}

namespace/macos.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ func (m *MacOSNetJail) Start() error {
112112
func (m *MacOSNetJail) Command(command []string) *exec.Cmd {
113113
m.logger.Debug("Command called", "command", command)
114114

115+
// Handle empty command
116+
if len(command) == 0 {
117+
m.logger.Debug("Empty command provided, returning nil")
118+
return nil // Return nil for empty command
119+
}
120+
115121
// Create command directly (no sg wrapper needed)
116122
m.logger.Debug("Creating command with group membership", "groupID", m.restrictedGid)
117123
cmd := exec.Command(command[0], command[1:]...)

0 commit comments

Comments
 (0)