Tk Source Code

View Ticket
Login
2024-10-10
19:50 • Closed ticket [eb332846]: "grid info" with half-dead argument can cause hangup or even crash plus 5 other changes artifact: fc370d4d user: fvogel
19:43 • Ticket [eb332846]: 4 changes artifact: 28fe6561 user: nemethi
15:23 • Pending ticket [eb332846]. artifact: cb2cf8f7 user: jan.nijtmans
2024-10-09
20:24 • Closed ticket [eb332846]. artifact: 333943f8 user: fvogel
20:18
Fix [eb332846f3]: [grid] and [pack] with half-dead argument can cause hangup or even crash. check-in: ddfb9848 user: fvogel tags: trunk, main
20:06
Fix [eb332846f3]: [grid] and [pack] with half-dead argument can cause hangup or even crash. check-in: eb220923 user: fvogel tags: core-8-6-branch
16:57 • Ticket [eb332846] "grid info" with half-dead argument can cause hangup or even crash status still Open with 3 other changes artifact: 0ade959e user: jan.nijtmans
10:05 • Ticket [eb332846]: 4 changes artifact: b4a0588f user: nemethi
2024-10-08
21:27 • Ticket [eb332846]: 4 changes artifact: e9a3d08a user: fvogel
2024-09-30
18:30 • Ticket [eb332846]: 4 changes artifact: c5def4ff user: nemethi
17:36 • Ticket [eb332846]: 4 changes artifact: 876a2662 user: nemethi
2024-09-29
20:48 • Add attachment placetest.tcl to ticket [eb332846] artifact: 608d8b2f user: fvogel
20:47 • Ticket [eb332846] "grid info" with half-dead argument can cause hangup or even crash status still Open with 3 other changes artifact: a88ad72e user: fvogel
20:27
Fix [eb332846f3]: 'grid info' with half-dead argument can cause hangup or even crash. check-in: f42bf16a user: fvogel tags: bug-eb332846f3
20:25
Revert [48a605ac]. This does not belong to bug [eb332846f3] but to [3f5867c997]. check-in: 9ed55d77 user: fvogel tags: bug-eb332846f3
09:08 • Ticket [eb332846] "grid info" with half-dead argument can cause hangup or even crash status still Open with 3 other changes artifact: 2db5c7da user: fvogel
2024-09-28
16:22 • Ticket [eb332846]: 4 changes artifact: 7faa24b1 user: nemethi
12:53 • Ticket [eb332846]: 3 changes artifact: df59bbca user: fvogel
10:42 • Ticket [eb332846]: 3 changes artifact: b3d69776 user: fvogel
10:25 • Add attachment packtest.tcl to ticket [eb332846] artifact: e788882f user: nemethi
10:24 • Add attachment gridtest2.tcl to ticket [eb332846] artifact: b51a1d14 user: nemethi
10:23 • Ticket [eb332846] "grid info" with half-dead argument can cause hangup or even crash status still Open with 4 other changes artifact: 7de5a3a0 user: nemethi
2024-09-27
20:35 • Ticket [eb332846]: 4 changes artifact: e4423b38 user: fvogel
20:34
Fix [eb332846f3]: 'grid info' with half-dead argument can cause hangup or even crash check-in: 48a605ac user: fvogel tags: bug-eb332846f3
20:14 • Ticket [eb332846] "grid info" with half-dead argument can cause hangup or even crash status still Open with 3 other changes artifact: 18a880d6 user: nab
19:44 • Add attachment gridtest.tcl to ticket [eb332846] artifact: 8519ed5d user: nemethi
19:43 • New ticket [eb332846] "grid info" with half-dead argument can cause hangup or even crash. artifact: d9e5e986 user: nemethi

Ticket UUID: eb332846f3e6965d0b11303af60c7c3e8dbd0eda
Title: "grid info" with half-dead argument can cause hangup or even crash
Type: Bug Version: all
Submitter: nemethi Created on: 2024-09-27 19:43:24
Subsystem: 49. [grid] Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2024-10-10 19:50:55
Resolution: Fixed Closed By: fvogel
    Closed on: 2024-10-10 19:50:55
Description:

From man n bind:

When the Destroy event is delivered to a widget, it is in a “half-dead” state: the widget still exists, but operations that involve it may return invalid results, or return an error.

The attached script shows that  grid info  with a half-dead argument can cause a hangup or even a crash.

  • Test on Ubuntu 24.04, with Tk 9.0:  After the 7th click on the "New Windows" button, the GUI freezes for about 18 seconds and its right half becomes invisible.
  • Test on Ubuntu 24.04, with Tk 8.6.15:  The 11th click on the button causes a segmentation fault.
  • Test on Windows 11, with Tk 9.0:  After the 4th click on the "New Windows" button, the GUI freezes forever.
  • Test on Windows 11, with Tk 8.6.15:  The 4th click on the button causes a crash.

In all these tests, before the hangup or crash happens, the GUI is already in a corrupted state (parts of it are missing).

User Comments: fvogel added on 2024-10-10 19:50:55:
I know, I have just fixed it. Thanks!

nemethi (claiming to be Csaba Nemethi) added on 2024-10-10 19:43:28:

The culprit is the "!" operator in line 487 of the new version of the file generic/tkPack.c. After removing the "!", all the "oldpack" tests pass.


jan.nijtmans added on 2024-10-10 15:23:04:

Problem, the CI builds https://github.com/tcltk/tk/actions/runs/11268950741 are failing with Tk 8.6, but not Tk 8.7 and 9.0. Reason: the "oldpack" testcases were removed in Tk 8.7/9.0, because they are deprecated, but those are the ones failing.

Any idea why those tests are failing on all platforms (Windows, UNIX and macOS)?


fvogel added on 2024-10-09 20:24:46:
Now merged, thanks.

For the records:

fvogel> What still puzzles me is that no fix is needed for [place].

This is due to function FindContent() (in tkPlace.c) not creating a content structure if it does not already exist for the given window. In contrast, GetGrid() and GetPacker() do create a new grid /packer structure when it doesn't already exist for the given window. This grid/packer structure creation must not happen when the window is being destroyed (i.e. is already dead).

jan.nijtmans added on 2024-10-09 16:57:26:

+1


nemethi (claiming to be Csaba Nemethi) added on 2024-10-09 10:05:36:

I find your improved solution quite OK and ready for merge.


fvogel added on 2024-10-08 21:27:33:

I have committed a somewhat better (does not spread TK_ALREADY_DEAD flags everywhere) and more complete (checks were missing) solution for both [grid] and [pack] in [56fbd8fe]. Could you please review?


nemethi (claiming to be Csaba Nemethi) added on 2024-09-30 18:30:45:

I meant the "bind" man page, sorry!


nemethi (claiming to be Csaba Nemethi) added on 2024-09-30 17:36:11:

Maybe there is a better fix, but I don't know enough about the interna of the geometry management to be able to provide any ideas. Your changes prevent a hangup or crash, and the "event" man page suggests clearly enough that one shouldn't rely on anything that involves widgets in “half-dead” state.


fvogel added on 2024-09-29 20:47:43:

Fix now proposed in branch bug-eb332846f3. This deals with [grid] and [pack].

What still puzzles me is that no fix is needed for [place]. This can be verified by running the attached placetest.tcl

I tend to conclude that a better fix might exist. Any idea?


fvogel added on 2024-09-29 09:08:14:

Another patch could have been:

Index: generic/tkEvent.c
==================================================================
--- generic/tkEvent.c
+++ generic/tkEvent.c
@@ -1286,11 +1286,11 @@
 	 * handle CreateNotify events, so we gotta pass 'em through.
 	 */
 
 	if ((ip.winPtr != NULL)
 		&& ((mask != SubstructureNotifyMask)
-		|| (eventPtr->type == CreateNotify))) {
+		|| (eventPtr->type == CreateNotify)) && !(winPtr->flags & TK_ALREADY_DEAD)) {
 	    TkBindEventProc(winPtr, eventPtr);
 	}
     }
     tsdPtr->pendingPtr = ip.nextPtr;

but this would completely prevent the bind mechanism to be called for dead windows (windows in the process of being deleted), but this would change the behavior drastically, as confirmed by new errors in the (bind and event) test suite.

I'm afraid I'll have to sprinkle tests on the TK_ALREADY_DEAD flag at quite many places, but this is more or less already what we have in the code.


nemethi (claiming to be Csaba Nemethi) added on 2024-09-28 16:22:53:

I can confirm that your patch works with both scripts gridtest2.tcl and gridtest.tcl. Good work!


fvogel added on 2024-09-28 12:53:43:

As a matter of principle, the following patch works (with your script gridtest2.tcl):

Index: generic/tkGrid.c
==================================================================
--- generic/tkGrid.c
+++ generic/tkGrid.c
@@ -729,10 +729,13 @@
 	Tcl_WrongNumArgs(interp, 2, objv, "window");
 	return TCL_ERROR;
     }
     if (TkGetWindowFromObj(interp, tkwin, objv[2], &content) != TCL_OK) {
 	return TCL_ERROR;
+    }
+    if (((TkWindow*) content)->flags & TK_ALREADY_DEAD) {
+	return TCL_OK;
     }
     contentPtr = GetGrid(content);
     if (contentPtr->containerPtr == NULL) {
 	Tcl_SetObjResult(interp, Tcl_ObjPrintf(
 	    "window \"%s\" isn't gridded", Tcl_GetString(objv[2])));

But it's only the basic idea. Of course it will all break again when something else than [grid info] is called (e.g. [grid bbox]) in the <Destroy> binding script.


fvogel added on 2024-09-28 10:42:10:
OK. Back to the drawing board.

nemethi (claiming to be Csaba Nemethi) added on 2024-09-28 10:23:58:

@Nicolas:  You might be right that the severity should be set to "Severe".  I just thought that, as suggested by the manual, it is no good idea to do anything with a “half-dead” widget, and this includes the invocation of  grid info,  too.  OTOH, even not recommended operations shouldn't result in hangups or crashes.

@Francois:  Your fix proposal merely makes sure that, when the the C code thinks the widget isn't gridded, it throws an error instead of returning a empty string.  While this change is OK for compatibility with pack, it doesn't solve the reported problems.  I have tested your fix proposal with the attached script gridtest2.tcl, in which I had to catch the error message.  Just like before, on Ubuntu 24.04 the 11th click on the button caused a segmentation fault.

I have repeated the test with pack instead of grid, using the attached script packtest.tcl.  While the GUI got corrupted with this test, too, at least there was no segmentation fault.


fvogel added on 2024-09-27 20:35:18:

Fix proposal: [48a605ace9].

This is how [pack] deals with this. What do you think?


nab added on 2024-09-27 20:14:06:
Hi,
IMHO this is a 'severe' issue

++

Attachments: