Tcl_SaveInterpState/Tcl_RestoreInterpState are not usable to save interpreter state (error info/code)
Tcl_SaveInterpState/Tcl_RestoreInterpState does not consider (legacy) copy of interpreter state (errorInfo/errorCode variables), which can produce unexpected behavior overwriting both values like in following example:
if {[catch {
... same code X producing error ...
} msg]} {
log INFO "unexpected error in block X: $msg"
log DEBUG $::errorCode $::errorInfo
}
if routine log (can evaluate something in interp and even generate temporary errors) does "safe" handling to save interp state (uses Tcl_SaveInterpState/Tcl_RestoreInterpState), it can overwrite ::errorInfo and ::errorCode or rather does not restore both properly, if some Tcl_ResetResult (called internally) resets interim iPtr->errorInfo and iPtr->errorCode to NULL.
This causes that system traces observing ::errorInfo and ::errorCode do nothing (because iPtr states are NULL) and both variables remain overwritten values.
Although I know that ::errorInfo and ::errorCode are legacy either and one should rather use option dictionary (second parameter after msg variable in catch), but Tcl_SaveInterpState/Tcl_RestoreInterpState are pretty unusable together with any kind of evaluation (especially because Tcl_ResetResult is involved in-between internally, and it is almost always the case).
The error is initially noticed in GH/sebres/tclclockmod/issues/19#issuecomment-582639778.
But now I have a branch bug-554117edde with test cases [95db6756e8bd9f96] illustrating that.
45213b1bc0Rebase bug-fix for [554117edde] to latest 8.6. For evaluation check-in2023-09-12 13:35:539cace6cc78closes [554117edde]: Tcl_SaveInterpState/Tcl_RestoreInterpState considering values of legacy variables/copy (save and restore ::errorInfo and ::errorCode) check-in2020-02-10 16:50:0695db6756e8bug [554117edde]: test cases illustrating it check-in2020-02-10 16:38:22
As one can see both 1.9.* test cases fails:
==== result-1.9.2 Tcl_SaveInterpState: nested save state does not modify error info/code FAILED
...
---- Result was:
0 {UNEXPERR INVISIBLE} {ERROR in safe block}
---- Result should have been (exact matching):
0 {EXPERR TEST-ERROR} {ERROR expected error}
==== result-1.9.2 FAILED
==== result-1.9.1 Tcl_SaveInterpState: "safe" eval without modifying states FAILED
...
---- Result was:
{UNEXPERR INVISIBLE} {ERROR in safe block}
---- Result should have been (exact matching):
{EXPERR TEST-ERROR} {ERROR expected error}
==== result-1.9.1 FAILED
Fixed in [9cace6cc78], tests passed.
I guess it should be cherry-picked to 8.5 as well.
I'm seeing 3 failing test-cases:
init.test==== init-0.1 no error on initialization phase (init.tcl) FAILED ==== Contents of test case:
child eval { list [set v [info exists ::errorInfo]] [if {$v} {set ::errorInfo}] [set v [info exists ::errorCode]] [if {$v} {set ::errorCode}] }---- Result was: 0 {} 1 {} ---- Result should have been (exact matching): 0 {} 0 {} ==== init-0.1 FAILED
ioCmd.test
==== iocmd-21.8 chan create, initialize failed, bad result, not a list FAILED ==== Contents of test case:
proc foo {args} {return "\{"} catch {chan create {r w} foo} msg rename foo {} set ::errorInfo---- Result was: wrong # args: should be "::foo" while executing "::foo initialize rc3 {read write}" (chan handler subcommand "initialize") invoked from within "chan create {r w} ::foo" invoked from within "chan create {r w} foo" ---- Result should have been (glob matching): chan handler "foo initialize" returned non-list: * ==== iocmd-21.8 FAILED
ioTrans.test
==== iortrans-2.7 chan push, initialize failed, bad result, not a list FAILED ==== Contents of test case:
proc foo {args} {return "\{"} catch {chan push [tempchan] foo} return $::errorInfo---- Result was: wrong # args: should be "::foo" while executing "::foo initialize rt2 {read write}" (chan handler subcommand "initialize") invoked from within "chan push [tempchan] ::foo" ("uplevel" body line 3) invoked from within "uplevel 1 $script" invoked from within "chan push [tempchan] foo" ---- Result should have been (glob matching): chan handler "foo initialize" returned non-list: * ==== iortrans-2.7 FAILED