| Ticket UUID: | 1797555 | |||
| Title: | DestroyMenuEntry writes into freed memory | |||
| Type: | Bug | Version: | obsolete: 8.4.16 | |
| Submitter: | gregcouch | Created on: | 2007-09-19 00:10:08 | |
| Subsystem: | 10. Generic Menus | Assigned To: | fvogel | |
| Priority: | 8 | Severity: | Minor | |
| Status: | Closed | Last Modified: | 2023-09-01 19:25:51 | |
| Resolution: | Fixed | Closed By: | fvogel | |
| Closed on: | 2023-09-01 19:25:51 | |||
| Description: |
Our application has a couple of semi-dynamic menus that are updated (destroyed and recreated) when the user opens and closes data files. When ever the menu is updated, I get the enclosed error that is shown by valgrind. We're using Tcl/Tk via Python, so my next step is to come up with a Tcl-only test case, but in case I don't, here's a record of the bug (the line numbers refer to the 8.4.15 release): Invalid read of size 4 at 0x5449FA2: DestroyMenuEntry (tkMenu.c:1469) by 0x5584B77: Tcl_EventuallyFree (tclPreserve.c:302) by 0x544D548: DeleteMenuCloneEntries (tkMenu.c:3498) by 0x5448DC4: MenuWidgetObjCmd (tkMenu.c:813) by 0x5513DEB: TclEvalObjvInternal (tclBasic.c:3190) by 0x5514049: Tcl_EvalObjv (tclBasic.c:3306) by 0x484F0C5: Tkapp_Call (_tkinter.c:1314) by 0x810221C: PyCFunction_Call (methodobject.c:108) by 0x80B453E: PyEval_EvalFrame (ceval.c:3563) by 0x80B59D4: PyEval_EvalCodeEx (ceval.c:2736) by 0x80B36B5: PyEval_EvalFrame (ceval.c:3656) by 0x80B59D4: PyEval_EvalCodeEx (ceval.c:2736) Address 0x6142DF8 is 0 bytes inside a block of size 16 free'd at 0x40050FF: free (vg_replace_malloc.c:233) by 0x5510BCB: TclpFree (tclAlloc.c:702) by 0x55195AC: Tcl_Free (tclCkalloc.c:1160) by 0x544D4CA: TkFreeMenuReferences (tkMenu.c:3460) by 0x5449E55: UnhookCascadeEntry (tkMenu.c:1380) by 0x5449F98: DestroyMenuEntry (tkMenu.c:1467) by 0x5584B77: Tcl_EventuallyFree (tclPreserve.c:302) by 0x544D548: DeleteMenuCloneEntries (tkMenu.c:3498) by 0x5448DC4: MenuWidgetObjCmd (tkMenu.c:813) by 0x5513DEB: TclEvalObjvInternal (tclBasic.c:3190) by 0x5514049: Tcl_EvalObjv (tclBasic.c:3306) by 0x484F0C5: Tkapp_Call (_tkinter.c:1314) Invalid write of size 4 at 0x5449FAC: DestroyMenuEntry (tkMenu.c:1470) by 0x5584B77: Tcl_EventuallyFree (tclPreserve.c:302) by 0x544D548: DeleteMenuCloneEntries (tkMenu.c:3498) by 0x5448DC4: MenuWidgetObjCmd (tkMenu.c:813) by 0x5513DEB: TclEvalObjvInternal (tclBasic.c:3190) by 0x5514049: Tcl_EvalObjv (tclBasic.c:3306) by 0x484F0C5: Tkapp_Call (_tkinter.c:1314) by 0x810221C: PyCFunction_Call (methodobject.c:108) by 0x80B453E: PyEval_EvalFrame (ceval.c:3563) by 0x80B59D4: PyEval_EvalCodeEx (ceval.c:2736) by 0x80B36B5: PyEval_EvalFrame (ceval.c:3656) by 0x80B59D4: PyEval_EvalCodeEx (ceval.c:2736) Address 0x6142DF8 is 0 bytes inside a block of size 16 free'd at 0x40050FF: free (vg_replace_malloc.c:233) by 0x5510BCB: TclpFree (tclAlloc.c:702) by 0x55195AC: Tcl_Free (tclCkalloc.c:1160) by 0x544D4CA: TkFreeMenuReferences (tkMenu.c:3460) by 0x5449E55: UnhookCascadeEntry (tkMenu.c:1380) by 0x5449F98: DestroyMenuEntry (tkMenu.c:1467) by 0x5584B77: Tcl_EventuallyFree (tclPreserve.c:302) by 0x544D548: DeleteMenuCloneEntries (tkMenu.c:3498) by 0x5448DC4: MenuWidgetObjCmd (tkMenu.c:813) by 0x5513DEB: TclEvalObjvInternal (tclBasic.c:3190) by 0x5514049: Tcl_EvalObjv (tclBasic.c:3306) by 0x484F0C5: Tkapp_Call (_tkinter.c:1314) | |||
| User Comments: |
fvogel added on 2023-09-01 19:25:51:
Yes, done. Test is menu-39.2 in all branches. chrstphrchvz added on 2023-08-31 21:44:51: For consistency between Tk versions, can the new test be renumbered on 8.6 as well (maybe with a comment reserving menu-40.*)? fvogel added on 2023-08-29 19:22:40: Thanks, I have merged the added test into core-8-6-branch and trunk. chrstphrchvz added on 2023-08-29 00:58:29: Thanks, François. I agree that the test is good enough. fvogel added on 2023-08-28 19:56:57: OK I see and I can reproduce the use-after-free with valgrind indeed after reverting [32a43619cd]. I have added a test in [1035e4f5], triggering the use-after-free without the fix and no longer triggering it with the fix. This test is derived from the menubug.tcl script provided by the OP. It is probably not very minimal, but I believe it's good enough. Let me know if you think a better version is desirable. chrstphrchvz added on 2023-08-28 09:44:59: Sorry, I should not have called this a crash. It is a use-after-free which does not reliably lead to a crash, even though it is caught by e.g. Valgrind or AddressSanitizer for a -DPURIFY build. fvogel added on 2023-08-27 10:25:02: I would happily design a test case based on "menubug.tcl" but would like to be sure it crashes without the fix. I could not reproduce the crash using the attached script "menubug.tcl" after reverting [32a43619cd]. chrstphrchvz added on 2023-08-22 18:16:07: This was fixed by [32a43619cd], as reverting that makes this crash possible again. But there appears to be nothing in the test suite which would trigger the same crash. Can a test please be added? gregcouch added on 2007-10-20 02:18:42: File Added - 250544: menubug.tcl Logged In: YES user_id=131838 Originator: YES The bug is still there in 8.4.16. The bug occurs when a submenu is destroyed before the menu entry that points to it is deleted. I've attached a tcl script that reproduces the bug. To observe the bug use a memory debugger like purify on Windows or valgrind on Linux. File Added: menubug.tcl | |||
Attachments:
- menubug.tcl [download] added by gregcouch on 2007-10-20 02:18:42. [details]
