| Ticket UUID: | 011706ec4291a667df0fabc47bebde0440ce60b2 | |||
| Title: | tk::ButtonInvoke safety bug yields Tcl error under simple script pattern | |||
| Type: | Bug | Version: | ||
| Submitter: | anonymous | Created on: | 2016-04-27 02:31:10 | |
| Subsystem: | 03. [*button] and [label] | Assigned To: | fvogel | |
| Priority: | 6 | Severity: | Critical | |
| Status: | Closed | Last Modified: | 2016-05-20 15:37:37 | |
| Resolution: | Fixed | Closed By: | fvogel | |
| Closed on: | 2016-05-20 15:37:37 | |||
| Description: |
Tested Version: Tcl/Tk 8.6.5
Tested OS: Ubuntu
Reproduces 100% of the time
Load the following script:
proc destroy_button {} {if {[winfo exists .b]} {destroy .b}}
button .b -text Foo -command destroy_button
bind .b <space> destroy_button
pack .b
focus .b
Now click on the application window (not the button itself though) and hit the spacebar.
This yields the following Tcl trace:
invalid command name ".b"
invalid command name ".b"
while executing
"$w cget -state"
(procedure "tk::ButtonInvoke" line 2)
invoked from within
"tk::ButtonInvoke .b"
(command bound to event)
This is due to a robustness bug in ::tk::ButtonInvoke (library/button.tcl line 599):
599 proc ::tk::ButtonInvoke w {
600 if {[$w cget -state] ne "disabled"} {
601 set oldRelief [$w cget -relief]
602 set oldState [$w cget -state]
603 $w configure -state active -relief sunken
604 update idletasks
605 after 100
606 $w configure -state $oldState -relief $oldRelief
607 uplevel #0 [list $w invoke]
608 }
609 }
The problem is that after the 'update idletasks' call on line 604, it could be that $w no longer exists, because the idle handlers destroy it.
Note also that it could be that the tk::ButtonInvoke call itself could fire only after the destruction is processed, so that it must do an initial check for $w's existence prior to doing anything at all.
Hence the fix is to add two checks:
599 599 proc ::tk::ButtonInvoke w {
600 - if {[$w cget -state] ne "disabled"} {
600 + if {[winfo exists $w] && [$w cget -state] ne "disabled"} {
601 601 set oldRelief [$w cget -relief]
602 602 set oldState [$w cget -state]
603 603 $w configure -state active -relief sunken
604 604 update idletasks
605 605 after 100
606 - $w configure -state $oldState -relief $oldRelief
607 - uplevel #0 [list $w invoke]
606 + if {[winfo exists $w]} {
607 + $w configure -state $oldState -relief $oldRelief
608 + uplevel #0 [list $w invoke]
609 + }
608 610 }
609 611 }
| |||
| User Comments: |
fvogel added on 2016-05-20 15:37:37:
Merged the fix for the scrollbar to trunk and core-8-6-branch. Closing now. fvogel added on 2016-05-18 10:16:48: Fixed the scrollbar case in [17c4cb5e17]. There were actually two different cases when it could fail, therefore two test cases: scrollbar-11.1 and scrollbar-11.2 Suggest stopping here the work on this. fvogel added on 2016-05-17 21:12:51: I have added a (currently failing) test case scrollbar-11.1 to demonstrate the issue with ::tk::ScrollButton2Down (scrlbar.tcl), and I will fix that one. See branch bug-011706ec42 Regarding: ::tk::PlaceWindow (tk.tcl) ::tk::dialog::file::Update (tkfbox.tcl) so far I'm failing in crafting a test case triggering any problem. If anyone cas build such a script then fine, otherwise I suggest just forgetting about these. fvogel added on 2016-05-17 20:09:31: Eric Rubin-Smith reports on TCLCORE:
"A couple weeks ago I submitted a bug report and patch for a safety bug in the Tk library: https://core.tcl.tk/tk/info/011706ec42
Thank you for improving upon the patch and applying it so quickly.
For the sake of completeness, I should mention that there appear to be similar safety issues in the following procs:
::tk::ScrollButton2Down (scrlbar.tcl)
::tk::PlaceWindow (tk.tcl)
::tk::dialog::file::Update (tkfbox.tcl)
In each case, there is a departure to the event loop ('update idletasks') followed by an assumption that some window still exists.
I have not generated any test case to actually demonstrate the bugs. But, as you know, it is in general an unsafe practice to make such assumptions after 'update idletasks' or any other round-trip with the event loop.
I leave it to the TCT to do with that info what you will. :-)
Cheers and thank you as always for the awesome language,
Eric
"
--> Reopening the ticket to deal with these.
fvogel added on 2016-05-03 17:16:54: Merged to core-8-6-branch and trunk. Thanks for your removal of my leftover puts in the test case! jan.nijtmans added on 2016-05-03 08:01:48: Thanks for the test-case! +1 for merging to core-8-6-branch and trunk (core-8.5.branch would be fine with me as well, since it's a bug) fvogel added on 2016-05-02 20:50:08: Added test button-14.1 to guard against regressions regarding this bug. fvogel added on 2016-05-02 20:08:35: OK, I see. I suggest merging to branch bug-011706ec42, why not? I'll look at creating a non regression test case for this. jan.nijtmans added on 2016-05-01 21:35:34: The point is removal of a busy "after 100", which would block the event loop during 100 ms. Since $w could be destroyed (theoretically, however unlikely) during this 100 ms due to other activity, I think the [winfo exists $w] is still needed. Keeping the event loop running at all times is generally better. fvogel added on 2016-05-01 19:36:35: [52251d00] works for me on Windows. What's the point of this? If the goal is removal of [update idletasks], then why do you keep the test on [winfo exists $w] ? jan.nijtmans added on 2016-04-30 15:55:56: How about: [52251d00] ??? jan.nijtmans added on 2016-04-29 21:49:40: +1 fvogel added on 2016-04-29 19:10:46: Thanks for the very clear bug report and the fix. This fix is now committed to branch bug-011706ec42 for review. | |||
Home
Timeline
Branches
Tags
Forum
Tickets
Download
Wiki