Skip to content

Commit abce025

Browse files
committed
fix: shell catch nil pointer dereference
When a command expression itself panics (not a shell exit code failure), RadPanic.ShellResult is nil. The catch handler was unconditionally dereferencing it, causing a nil pointer crash. Now checks ShellResult != nil before dereferencing. When nil, assigns the error value to target variables (matching how assignment catch handlers work) so the catch block can still access the error.
1 parent 6837175 commit abce025

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

core/shell.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,17 @@ func (i *Interpreter) executeShellStmt(shell *rl.Shell) EvalResult {
5151
}
5252

5353
return i.withCatch(shell.Catch, func(rp *RadPanic) EvalResult {
54-
result := rp.ShellResult
55-
assignResults(*result)
54+
if rp.ShellResult != nil {
55+
assignResults(*rp.ShellResult)
56+
} else if len(targets) > 0 {
57+
// The panic came from the command expression itself (not a shell exit code),
58+
// so there's no shell result. Assign the error to the first target and null to the rest,
59+
// matching how assignment catch handlers work.
60+
i.doVarPathAssign(targets[0], rp.ErrV, false)
61+
for j := 1; j < len(targets); j++ {
62+
i.doVarPathAssign(targets[j], RAD_NULL_VAL, false)
63+
}
64+
}
5665

5766
res := i.runBlock(shell.Catch.Stmts)
5867
if res.Ctrl != CtrlNormal {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
### TITLE ###
2+
Shell catch with expression panic
3+
### DESCRIPTION ###
4+
When the shell command expression itself causes a RadPanic (e.g., a function
5+
call returns an error), ShellResult is nil. The catch handler should not crash
6+
and should assign the error to target variables.
7+
### INPUT ###
8+
fn fail():
9+
return error("cmd expression failed")
10+
11+
code = `{fail()}` catch:
12+
print("caught: {code}")
13+
### STDOUT ###
14+
caught: cmd expression failed
15+

0 commit comments

Comments
 (0)