Skip to content

Commit

Permalink
correctly set PIPESTATUS when err-raise option enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
sekiguchi-nagisa committed Apr 13, 2024
1 parent 66e3a77 commit fc161cd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
### Fixed

- bugfix code generation of catch block with nested blocks (fix potential resource leak)
- correctly set ``PIPESTATUS`` when throw error from last-pipe

## [0.33.2] - 2024-04-11

Expand Down
4 changes: 2 additions & 2 deletions src/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,10 +913,10 @@ bool VM::callCommand(ARState &state, const ResolvedCmd &cmd, ObjPtr<ArrayObject>
if (state.hasError()) {
return false;
}
pushExitStatus(state, status); // set exit status before check ERR_RAISE
if (!checkCmdExecError(state, attr, status)) {
return false;
}
pushExitStatus(state, status);
return true;
}
case ResolvedCmd::MODULE:
Expand Down Expand Up @@ -1175,10 +1175,10 @@ int VM::builtinExec(ARState &state, ArrayObject &argvObj, Value &&redir) {
bool VM::returnFromUserDefinedCommand(ARState &state, int64_t status) {
const auto attr = static_cast<CmdCallAttr>(state.stack.getLocal(UDC_PARAM_ATTR).asNum());
state.stack.unwind();
pushExitStatus(state, status); // set exit status before check ERR_RAISE
if (!checkCmdExecError(state, attr, status)) {
return false;
}
pushExitStatus(state, status);
assert(!state.stack.checkVMReturn());
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions test/exec/cases/base/errraise2.ds
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ function assertStatus($s : Int, $e : Any, $ln : Int) {
var lineno = ($e as ExecError).lineno()
var r = $/\d+/.match($m)
var actual = $r!.group(0)!.toInt()!
assert $s == $actual : "mismatch status, expect: $s, actual: $actual"
assert $lineno == $ln : "mismatch lineno, expect: $ln, actual: $lineno"
assert $s == $actual
assert $lineno == $ln
}


Expand Down Expand Up @@ -173,7 +173,7 @@ try {
} catch $e { $ex = $e; }
$assertStatus(167, $ex, 171)

## backgorund pipeline
## background pipeline
var jj = { sh -c "exit 156"; true; } | false &
assert $jj.wait() == 1
assert $jj.status(0)! == 156
Expand Down
41 changes: 41 additions & 0 deletions test/exec/cases/base/errraise5.ds
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

# for lastpipe
shctl set errraise

## raise in last-pipe (external)
var ex = 345 as Any
try {
ls | for a in $STDIN { sh -c 'exit 56'; echo $a; }
} catch e { $ex = $e; }
assert ($ex as ExecError).message() == "command exits with non-zero status: \`56'"

assert $PIPESTATUS.size() == 2
assert $PIPESTATUS[0] == 0
assert $PIPESTATUS[1] == 56

## raise in last-pipe (builtin)
$ex = 45643652
try {
ls | for a in $STDIN { sh -c 'exit 156'; echo $a; } | { false; }
} catch e { $ex = $e; }
assert ($ex as ExecError).message() == "command exits with non-zero status: \`1'"

assert $PIPESTATUS.size() == 3
assert $PIPESTATUS[0] == 0
assert $PIPESTATUS[1] == 156
assert $PIPESTATUS[2] == 1

## raise in last-pipe (user-defined)
ff() { return $1.toInt() ?? 99; }
$ex = 45643652
try {
ls | for a in $STDIN { ff 67; echo $a; } | { ff 145; }
} catch e { $ex = $e; }
assert ($ex as ExecError).message() == "command exits with non-zero status: \`145'"

assert $PIPESTATUS.size() == 3
assert $PIPESTATUS[0] == 0
assert $PIPESTATUS[1] == 67
assert $PIPESTATUS[2] == 145

true

0 comments on commit fc161cd

Please sign in to comment.