Skip to content

Commit 7c3e665

Browse files
committed
fix: avoid nil pointer dereference in shellAction
The shellAction function was calling cmd.Flags() directly multiple times throughout the function instead of using the flags variable that was already captured at the beginning (line 72). In certain execution paths, cmd.Flags() could return nil, causing a panic: panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x2 addr=0x98 pc=0x...] This fix changes three locations to consistently use the flags variable: - Line 164: workDir flag access - Line 200: shell flag access - Line 211: preserve-env flag access Additionally, adds a nil check for inst.Config before accessing it to prevent similar panics and provide clearer error messages when instances have configuration errors. Signed-off-by: Christian Stewart <[email protected]>
1 parent b18eed7 commit 7c3e665

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

cmd/limactl/shell.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ func shellAction(cmd *cobra.Command, args []string) error {
9494
}
9595
return err
9696
}
97+
if inst.Config == nil {
98+
if len(inst.Errors) > 0 {
99+
return fmt.Errorf("instance %q has configuration errors: %w", instName, errors.Join(inst.Errors...))
100+
}
101+
return fmt.Errorf("instance %q has no configuration", instName)
102+
}
97103
if inst.Status == limatype.StatusStopped {
98104
startNow, err := flags.GetBool("start")
99105
if err != nil {
@@ -155,14 +161,14 @@ func shellAction(cmd *cobra.Command, args []string) error {
155161
// changeDirCmd := "cd workDir || exit 1" if workDir != ""
156162
// := "cd hostCurrentDir || cd hostHomeDir" if workDir == ""
157163
var changeDirCmd string
158-
workDir, err := cmd.Flags().GetString("workdir")
164+
workDir, err := flags.GetString("workdir")
159165
if err != nil {
160166
return err
161167
}
162168
if workDir != "" {
163169
changeDirCmd = fmt.Sprintf("cd %s || exit 1", shellescape.Quote(workDir))
164170
// FIXME: check whether y.Mounts contains the home, not just len > 0
165-
} else if len(inst.Config.Mounts) > 0 || inst.VMType == limatype.WSL2 {
171+
} else if inst.Config != nil && (len(inst.Config.Mounts) > 0 || inst.VMType == limatype.WSL2) {
166172
hostCurrentDir, err := os.Getwd()
167173
if err == nil && runtime.GOOS == "windows" {
168174
hostCurrentDir, err = mountDirFromWindowsDir(ctx, inst, hostCurrentDir)
@@ -191,7 +197,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
191197
}
192198
logrus.Debugf("changeDirCmd=%q", changeDirCmd)
193199

194-
shell, err := cmd.Flags().GetString("shell")
200+
shell, err := flags.GetString("shell")
195201
if err != nil {
196202
return err
197203
}
@@ -202,7 +208,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
202208
}
203209
// Handle environment variable propagation
204210
var envPrefix string
205-
preserveEnv, err := cmd.Flags().GetBool("preserve-env")
211+
preserveEnv, err := flags.GetBool("preserve-env")
206212
if err != nil {
207213
return err
208214
}

0 commit comments

Comments
 (0)