Tcl Source Code

View Ticket
Login
2023-09-12
13:40 • Ticket [554117edde] Tcl_SaveInterpState/Tcl_RestoreInterpState are not usable to... artifact: 943348dc93 user: jan.nijtmans
13:35
Rebase bug-fix for [554117edde] to latest 8.6. For evaluation leaf check-in: 45213b1bc0 user: jan.nijtmans tags: bug-554117edde
2020-02-10
16:52 • Pending ticket [554117edde]: Tcl_SaveInterpState/Tcl_RestoreInterpState are not ... artifact: 4427912eac user: sebres
16:50
closes [554117edde]: Tcl_SaveInterpState/Tcl_RestoreInterpState considering values of legacy variabl... check-in: 9cace6cc78 user: sebres tags: bug-554117edde
16:44 • Ticket [554117edde] Tcl_SaveInterpState/Tcl_RestoreInterpState are not usable to... artifact: 733ee1b91d user: sebres
16:38
bug [554117edde]: test cases illustrating it check-in: 95db6756e8 user: sebres tags: bug-554117edde
16:35 • New ticket [554117edde] Tcl_SaveInterpState/Tcl_RestoreInterpState are not usabl... artifact: 5d6b49a2f1 user: sebres

Ticket UUID: 554117eddec7220f1cda04c16cc45a77d6c175e0
Title: Tcl_SaveInterpState/Tcl_RestoreInterpState are not usable to save interpreter state (error info/code)
Type: Bug Created on: 2020-02-10 16:35:07
Submitter: sebres Assigned to: nobody
Subsystem: 45. Parsing and Eval Severity: Minor
Priority: 5 Medium Last modified: 2023-09-12 13:40:37
Status: Pending Closed by: nobody
Resolution: Fixed Closed on:
Version:
Description:

Current implementation of 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.

User Comments:
sebres added on 2020-02-10 16:44:27:

As one can see both 1.9.* test cases fails:

==== 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

==== 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


sebres added on 2020-02-10 16:52:21:

Fixed in [9cace6cc78], tests passed.
I guess it should be cherry-picked to 8.5 as well.


jan.nijtmans added on 2023-09-12 13:40:37:

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