Tk Source Code

View Ticket
Login
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: