Attachment "b964b98d-9.1.0.patch" to
ticket [b964b98d]
added by
anonymous
2026-10-04 13:25:57.
--- a/generic/ttk/ttkManager.c
+++ b/generic/ttk/ttkManager.c
@@ -39,6 +39,23 @@
* in this case Tk does _not_ call the lostContentProc (documented behavior).
* Tk doesn't handle case (3) either; to account for that we
* register an event handler on the content window to track <Destroy> events.
+ *
+ * +++ Re-entrancy <<NOTE-REENTRANCY>>.
+ *
+ * Calls into Tk made while placing or (un)mapping content windows may
+ * service events (on Aqua, window operations pump the Cocoa run loop, which
+ * can service the Tcl event queue and idle calls mid-operation). Such a
+ * re-entrant event can destroy a content window, running RemoveContent()
+ * in the middle of a placement: the Ttk_Content record (and possibly the
+ * manager's data record for it) is freed and content[] is compacted while
+ * a caller still holds pointers into it. Dereferencing them afterwards is
+ * a use-after-free (see ticket [b964b98d1b]). To guard against this,
+ * mgr->epoch is bumped on every mutation of content[], and routines that
+ * call into Tk while holding a Ttk_Content pointer re-check the epoch
+ * after each such call, bailing out if the content list changed. Bailing
+ * out early is safe: every mutation path (InsertContent, RemoveContent,
+ * Ttk_ReorderContent) schedules a fresh relayout, so a consistent layout
+ * pass always follows.
*/
/* ++ Data structures.
@@ -63,6 +80,13 @@
unsigned flags;
Tcl_Size nContent;
Ttk_Content **content;
+ size_t epoch; /* Bumped on every mutation of content[].
+ * Calls into Tk (e.g. Tk_MaintainGeometry,
+ * Tk_MapWindow) may service events and
+ * re-enter RemoveContent/InsertContent,
+ * freeing Ttk_Content records out from under
+ * an iteration; comparing epochs detects
+ * this. See <<NOTE-REENTRANCY>>. */
};
/* manager->flags bits:
@@ -118,6 +142,10 @@
Ttk_Manager *mgr = (Ttk_Manager *)clientData;
mgr->flags &= ~MGR_UPDATE_PENDING;
+ if (((TkWindow *)mgr->window)->flags & TK_ALREADY_DEAD) {
+ /* The container is being torn down; don't touch the layout. */
+ return;
+ }
if (mgr->flags & MGR_RESIZE_REQUIRED) {
RecomputeSize(mgr);
}
@@ -152,15 +180,27 @@
case MapNotify:
for (i = 0; i < mgr->nContent; ++i) {
Ttk_Content *content = mgr->content[i];
- if (content->flags & CONTENT_MAPPED) {
+ if ((content->flags & CONTENT_MAPPED)
+ && !(((TkWindow *)content->window)->flags
+ & TK_ALREADY_DEAD)) {
+ size_t epoch = mgr->epoch;
Tk_MapWindow(content->window);
+ if (mgr->epoch != epoch) {
+ /* <<NOTE-REENTRANCY>> */
+ break;
+ }
}
}
break;
case UnmapNotify:
for (i = 0; i < mgr->nContent; ++i) {
Ttk_Content *content = mgr->content[i];
+ size_t epoch = mgr->epoch;
Tk_UnmapWindow(content->window);
+ if (mgr->epoch != epoch) {
+ /* <<NOTE-REENTRANCY>> */
+ break;
+ }
}
break;
}
@@ -216,6 +256,7 @@
mgr->nContent = 0;
mgr->content = NULL;
mgr->flags = 0;
+ mgr->epoch = 0;
Tk_CreateEventHandler(
mgr->window, ManagerEventMask, ManagerEventHandler, mgr);
@@ -250,6 +291,7 @@
static void InsertContent(Ttk_Manager *mgr, Ttk_Content *content, Tcl_Size index)
{
Tcl_Size endIndex = mgr->nContent++;
+ ++mgr->epoch;
mgr->content = (Ttk_Content **)Tcl_Realloc(mgr->content, mgr->nContent * sizeof(Ttk_Content *));
while (endIndex > index) {
@@ -281,6 +323,8 @@
Ttk_Content *content = mgr->content[index];
Tcl_Size i;
+ ++mgr->epoch;
+
/* Notify manager:
*/
mgr->managerSpec->ContentRemoved(mgr->managerData, index);
@@ -303,7 +347,13 @@
DeleteContent(content);
- ScheduleUpdate(mgr, MGR_RESIZE_REQUIRED);
+ if (!(((TkWindow *)mgr->window)->flags & TK_ALREADY_DEAD)) {
+ /* Don't re-arm the relayout idle while the container itself is
+ * being torn down; it could fire mid-teardown. Ttk_DeleteManager
+ * cancels any pending call anyway.
+ */
+ ScheduleUpdate(mgr, MGR_RESIZE_REQUIRED);
+ }
}
/*------------------------------------------------------------------------
@@ -368,10 +418,27 @@
void Ttk_PlaceContent(
Ttk_Manager *mgr, Tcl_Size index, int x, int y, int width, int height)
{
- Ttk_Content *content = mgr->content[index];
+ Ttk_Content *content;
+ size_t epoch = mgr->epoch;
+
+ if (index >= mgr->nContent) {
+ return;
+ }
+ content = mgr->content[index];
+ if (((TkWindow *)content->window)->flags & TK_ALREADY_DEAD) {
+ /* The content window is being torn down; its DestroyNotify will
+ * remove it from the manager.
+ */
+ return;
+ }
Tk_MaintainGeometry(content->window,mgr->window,x,y,width,height);
+ if (mgr->epoch != epoch) {
+ /* <<NOTE-REENTRANCY>>: 'content' may have been freed. */
+ return;
+ }
content->flags |= CONTENT_MAPPED;
- if (Tk_IsMapped(mgr->window)) {
+ if (Tk_IsMapped(mgr->window)
+ && !(((TkWindow *)content->window)->flags & TK_ALREADY_DEAD)) {
Tk_MapWindow(content->window);
}
}
@@ -381,8 +448,18 @@
*/
void Ttk_UnmapContent(Ttk_Manager *mgr, Tcl_Size index)
{
- Ttk_Content *content = mgr->content[index];
+ Ttk_Content *content;
+ size_t epoch = mgr->epoch;
+
+ if (index >= mgr->nContent) {
+ return;
+ }
+ content = mgr->content[index];
Tk_UnmaintainGeometry(content->window, mgr->window);
+ if (mgr->epoch != epoch) {
+ /* <<NOTE-REENTRANCY>>: 'content' may have been freed. */
+ return;
+ }
content->flags &= ~CONTENT_MAPPED;
/* Contrary to documentation, Tk_UnmaintainGeometry doesn't always
* unmap the content window:
@@ -511,6 +588,8 @@
{
Ttk_Content *moved = mgr->content[fromIndex];
+ ++mgr->epoch;
+
/* Shuffle down: */
while (fromIndex > toIndex) {
mgr->content[fromIndex] = mgr->content[fromIndex - 1];
--- a/generic/tkGeometry.c
+++ b/generic/tkGeometry.c
@@ -432,8 +432,16 @@
* setting up the extra infrastructure for management, just make a
* call to Tk_MoveResizeWindow; the parent/child relationship will
* take care of the rest.
+ *
+ * Tk_MoveResizeWindow delivers <Configure> synchronously
+ * (TkDoConfigureNotify), so a binding may destroy either window
+ * before it returns (ticket [b964b98d1b]). Preserve both windows so
+ * the flag checks below never touch freed memory, and don't map a
+ * window that is being destroyed.
*/
+ Tcl_Preserve(window);
+ Tcl_Preserve(container);
Tk_MoveResizeWindow(window, x, y, width, height);
/*
@@ -442,9 +450,13 @@
* is taken care of elsewhere).
*/
- if (Tk_IsMapped(container)) {
+ if (!(((TkWindow *)window)->flags & TK_ALREADY_DEAD)
+ && !(((TkWindow *)container)->flags & TK_ALREADY_DEAD)
+ && Tk_IsMapped(container)) {
Tk_MapWindow(window);
}
+ Tcl_Release(container);
+ Tcl_Release(window);
return;
}
--- a/generic/ttk/ttkPanedwindow.c
+++ b/generic/ttk/ttkPanedwindow.c
@@ -385,6 +385,11 @@
for (index = 0; index < Ttk_NumberContent(pw->paned.mgr); ++index) {
Pane *pane = (Pane *)Ttk_ContentData(pw->paned.mgr, index);
int size = pane->sashPos - pos;
+ /* Read sashPos before placing: Ttk_PlaceContent/Ttk_UnmapContent may
+ * service events that destroy the pane, freeing 'pane'
+ * (see <<NOTE-REENTRANCY>> in ttkManager.c).
+ */
+ int nextPos = pane->sashPos + sashThickness;
if (size > 0) {
if (horizontal) {
@@ -396,7 +401,7 @@
Ttk_UnmapContent(pw->paned.mgr, index);
}
- pos = pane->sashPos + sashThickness;
+ pos = nextPos;
}
}