| Ticket UUID: | 775ee885603d3d09b5984b1579630e9786f78928 | ||
| Title: | upvar (used with wrong level) causes a segfault | ||
| Type: | Bug | Created on: | 2019-09-06 13:33:22 |
| Submitter: | sebres | Assigned to: | sebres |
| Subsystem: | 07. Variables | Severity: | Critical |
| Priority: | 5 Medium | Last modified: | 2019-09-07 16:25:08 |
| Status: | Closed | Closed by: | jan.nijtmans |
| Resolution: | Fixed | Closed on: | 2019-09-07 16:25:08 |
| Version: | >=8.7 | ||
| Description: | ||||
|
Folowing code executed at top-level would segfault in 8.7 (and trunk):
upvar error error; lappend error BOOM
Initially found (mistakenly) trying to check an idea in trace:
trace add variable r read {upvar error error; lappend error BOOM}
set r
or here (test executed in top-level, so it is bad level too):
proc test {} {uplevel {upvar error error; lappend error BOOM}}
test
Also following error is a bit unexpected:
% uplevel {lappend error BOOM}; # expecting bad level "1"
bad level "lappend error BOOM"
Tcl 8.6 seems to be not affected by both.
| ||||
| User Comments: | ||||
dgp added on 2019-09-06 14:38:06:
Looks like a bug in the TIP 515 implementation. dgp added on 2019-09-06 14:46:03:
Fix committed to bug branch. Pls review and merge. sebres added on 2019-09-06 15:03:57:
Crossed... exactly my fix for upvar, Don (just without space before semicolon :) I'd like to fix uplevel too - new test-cases (uplevel-4.0.*) still failing. I've also a solution for that, but then it would conflict with uplevel-4.4 now: ==== uplevel-4.4 error: not enough args FAILED ==== Contents of test case:I'd expect that ?level? is optional where command is don't, so I'd really read it an intention to execute "1" here. Or am I wrong?apply {{} { uplevel 1 }} BTW, several upvar tests failing if executed directly ( sebres added on 2019-09-06 16:25:18:
Furthermore I think TIP 515 has also some conceptual errors: It intends to write syntax error in case of:
% apply {{} { uplevel 1 }}
wrong # args: should be "uplevel ?level? command ?arg ...?"
But would "fail" with bad level if given (or relative calculated as current - 1) levels are really wrong:
% uplevel 1
bad level "1"
% uplevel -1
bad level "-1"
% uplevel {lappend error BOOM}
bad level "lappend error BOOM"
Just noone from that is level at all.
I don't think it is possible to provide simple fix at all using current handling of I'd prefer totally without a TIP 515 as with implementation like this. uplevel ?level? command ?arg ...?It is pretty clear that the command (in opposite to level) is not optional, so in my opinion better to catch invalid command "1" instead of usage, as bad level "body" like now.
dgp added on 2019-09-06 16:36:21:
Can we please fix the clear problem with the clear fix first, and open the other proposed improvements as a separate ticket? sebres added on 2019-09-06 16:56:16:
Too late Don :) In [3a85616c61] I pushed "proper" implementation for that. But we should anyway cherry-pick the test cases (>= 8.5) to cover all this corner cases across all versions. dgp added on 2019-09-06 17:04:00:
The history and dependencies on this syntax are a mess. At least once before the conclusion was "Won't Fix". https://core.tcl-lang.org/tcl/tktview/1017884 I'm willing to take another look at the issue, but it is not a matter where the right thing to do is obviously clear to every viewer. It is a mistake to entangle a clearly correct segfault fix with a longstanding controversial topic. sebres added on 2019-09-06 17:52:13:
Test cases are cherry-picked everywhere since 8.5, bad level fixed in 8.5 (same as in 8.6), 8.7 got your fix (for segfault) applied. In 8.5 and 8.6 all tests should pass. In 8.7 (and trunk if merged) the tests "uplevel-4.0.*" would fail, so they have to get a KnownBug constraint or my branch can be merged (just the internal TclObjGetFrame will be not compatible to 8.6 due to new parameter), or it could be rewriten using new function TclObjGetFrameEx (with new parameter flags), or even something else. I'm done here. You (=TCT) can decide about what would be correct here. sebres added on 2019-09-06 21:06:14:
I found another solution for second part (invalid level used in "bad level" message, if relative level out of frames) as in branch "bug-775ee88560", which is simplier, also TIP#515-conform as well as binary compatible. BTW. I fixed another wrong case across all branches (so TclGetFrame and Tcl_UpVar2 are more consistent to TclObjGetFrame now, as regards the error message).
If frameName (actual level) does not contain a real level (#0 or 1) historically TclGetFrame and Tcl_UpVar2 use current level - 1, so to put supplied name in case of bad level (error at top - 1) was wrong but also not consistent with TclObjGetFrame. Just I don't think scenarios covered with "upvar-9.1.1", "upvar-9.3" etc as well as following are good at all:
apply {{} {testupvar "this is a bad level" a {} x local; puts OK}}
In my opinion reasonable would be to allow Tcl_UpVar2 only "#0", "1", NULL (and may be "") as argument frameName, and bad level in other case, but at the moment (as one can see above and in mentioned test cases) it allows any bad value if called from level > 0, and fails only if called at top-level (so all values were invalid).
jan.nijtmans added on 2019-09-07 16:25:08:
I'm thinking: Can't we think of a better error-message than:
bad level "1"
???
I agree that "bad level xyz" is confusing, but 'bad level "1"' is just as confusing because the command causing this error-message doesn't contain "1" anywhere.
> I'm done here.
> You (=TCT) can decide about what would be correct here.
Looks like you're not done yet ;-) Although this ticket is fixed now (the segfault is gone, thanks!), there still is room for better error-messages. I don't think changing the error-messages is against TIP #515: This TIP only changes the interpretation on what syntax is valid and what not, changing the error-message in case of invalid syntax is always a good thing to consider. As long as the only thing changing is the error-message, that can be discussed here without a TIP
| ||||
