On Windows, deleting cascade entries of a menu that has clones (for example a menu of the menubar) from its -postcommand reads freed memory (heap-use-after-free found with AddressSanitizer). The code is the same in 8.6.13 (where we found it), core-8-6-branch, core-9-0-branch and main.
Cause
DeleteMenuCloneEntries() frees the entries with Tcl_EventuallyFree(..., DestroyMenuEntry) before it removes them from menuPtr->entries and updates numEntries. Freeing a cascade entry of a clone destroys the clone of its submenu (DestroyMenuEntry → TkDestroyMenu → Tk_DestroyWindow → DestroyWindow). DestroyWindow sends messages to TkWinChildProc, which calls Tcl_ServiceAll(). During WM_INITMENU the service mode is TCL_SERVICE_ALL, so idle handlers run there, among them the ReconfigureWindowsMenu that TkpDestroyMenuEntry scheduled for this same menu when the previous entry was freed. ReconfigureWindowsMenu loops over menuPtr->entries[0..numEntries-1], which still contains the entries freed a moment before.
How to reproduce
Run this with wish on Windows and click the "File" menu:
package require Tk
menu .mb
. configure -menu .mb
menu .mb.file -tearoff 0 -postcommand FillFileMenu
.mb add cascade -label File -menu .mb.file
proc FillFileMenu {} {
.mb.file delete 0 end
foreach i {1 2 3 4} {
set sub .mb.file.sub$i
if {![winfo exists $sub]} {
menu $sub -tearoff 0
$sub add command -label "Item $i"
}
.mb.file add cascade -label "Submenu $i" -menu $sub
}
}
FillFileMenu
With Tcl and Tk built with /fsanitize=address (nmake option tclalloc, so that ckalloc uses malloc) the first click reports:
ERROR: AddressSanitizer: heap-use-after-free
READ of size 4
GetEntryText win/tkWinMenu.c:494
ReconfigureWindowsMenu win/tkWinMenu.c:611
TclServiceIdle (tcl) generic/tclTimer.c:751
Tcl_ServiceAll (tcl) generic/tclNotify.c:1082
TkWinChildProc win/tkWinX.c:844
... DestroyWindow
TkWmDeadWindow win/tkWinWm.c:2713
Tk_DestroyWindow generic/tkWindow.c:1515
DestroyMenuInstance generic/tkMenu.c:1204
TkDestroyMenu generic/tkMenu.c:1280
DestroyMenuEntry generic/tkMenu.c:1442
Tcl_EventuallyFree (tcl) generic/tclPreserve.c:296
DeleteMenuCloneEntries generic/tkMenu.c:3531
MenuWidgetObjCmd generic/tkMenu.c:751 (.mb.file delete 0 end)
...
TkPostCommand generic/tkMenu.c:2658
PreprocessMenu generic/tkMacWinMenu.c:56
TkPreprocessMenu generic/tkMacWinMenu.c:137
TkWinHandleMenuEvent win/tkWinMenu.c:1186 (WM_INITMENU)
freed by:
DestroyMenuEntry generic/tkMenu.c:1466 (ckfree(mePtr))
Tcl_EventuallyFree
DeleteMenuCloneEntries generic/tkMenu.c:3531
(line numbers of 8.6.13; the full report is attached.)
Without ASan the freed memory is usually still readable and nothing visible happens (also with 9.0.3), but in our application (GiD, large menus rebuilt from -postcommand) it ended in an access violation inside Tk (Tk_MakeWindowExist from MenuSelectEvent) when the user clicked the menubar.
Proposed fix
Remove the entries from the menu (compact entries, update numEntries and active) before freeing them, so that anything that runs while they are freed sees a consistent menu. The entries are still freed from last to first, as before.
Patches attached (unified diffs of generic/tkMenu.c): tkMenu-main.patch for main (it also applies to core-9-0-branch) and tkMenu-core-8-6-branch.patch, the same change for 8.6 (int, ckalloc, masterMenuPtr). The full ASan report is in asan_report.txt.
Tested on Windows x64 with Tk 8.6.13 built with ASan: the script above reports the heap-use-after-free on every run without the patch and runs clean with it, and our application no longer crashes. The main and 8.6 patches apply cleanly but were not built there.
Possibly related, but not the same bug: ticket [afda09b594], also a crash in ReconfigureWindowsMenu, but there the freed object was the whole menu, fixed in check-in [c08e58fcfd].