Tk Source Code

View Ticket
Login
Ticket UUID: 3981091ed336044f569b9b7db01d6fbfa27c7ef5
Title: Segmentation fault with bogus resource value (X11)
Type: Bug Version: 9.0
Submitter: emiliano Created on: 2024-09-19 13:37:28
Subsystem: (unused) Assigned To: jan.nijtmans
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2024-09-20 17:01:41
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2024-09-20 17:01:41
Description:
Installing a bogus value on the resource manager on X11 results in a segmentation fault in 8.7/9.0 

~$ xrdb -edit tmp.txt; echo "*background: BOGUS" | xrdb -merge -
~$ wish8.7
Segmentation fault (core dumped)
~$ wish8.6
application-specific initialization failed: unknown color name "BOGUS"

Same bug on 9.0 confirmed by Schelte on the chat.
User Comments: jan.nijtmans added on 2024-09-20 17:01:41:

Fixed [efcd1b3dfa9d3aa2|here] (and in core-8-6-branch and core-8-branch as well)

Thanks for the report and the fix!


jan.nijtmans added on 2024-09-20 13:22:16:

Now in a [4bc56d6eb48db622|branch] (slightly modified)


emiliano added on 2024-09-20 12:52:26:

The 8.6 version tested by me is 8.6.12, provided by ubuntu 22.04 LTS.

The faulty code was introduced here to fix this bug.

A slightly better patch is attached.


erikleunissen added on 2024-09-20 07:56:29:
I invoked wish from various releases, using "wishx.x -use foo". Results:

8.5.19, 8.6.12 and 8.6.13:
application-specific initialization failed: expected integer but got "foo"

8.6.14, 8.6.15 and 9.0.0:
Segmentation fault      (core dumped)

emiliano added on 2024-09-20 00:35:31:
I realized that anything that makes the creation of the main window fail while initializing Tk fires the same bug. In particular (no xrdb needed)

$ wish9.0 -use FOO
Segmentation fault (core dumped)

Here is the stack trace:

Program received signal SIGSEGV, Segmentation fault.
0xb7f3124b in Ttk_TkDestroyedHandler () from /usr/local/lib/libtcl9tk9.0.so
(gdb) bt
#0  0xb7f3124b in Ttk_TkDestroyedHandler () from /usr/local/lib/libtcl9tk9.0.so
#1  0xb7e7b8ff in Tk_DestroyWindow (tkwin=0x4660d8)
    at /home/carla/src/tk/unix/../generic/tkWindow.c:1692
#2  0xb7e8634a in TkCreateFrame (dummy443=0x0, interp=0x423e38, objc=6, 
    objv=0x46d3cc, type=1, appName=0x451958 "wish")
    at /home/carla/src/tk/unix/../generic/tkFrame.c:677
#3  0xb7e7ddb4 in Initialize (interp=0x423e38)
    at /home/carla/src/tk/unix/../generic/tkWindow.c:3435
#4  0xb7e7d07f in Tk_Init (interp=0x423e38)
    at /home/carla/src/tk/unix/../generic/tkWindow.c:3080
#5  0x0040091c in Tcl_AppInit ()
#6  0xb7e6b03d in Tk_MainEx () from /usr/local/lib/libtcl9tk9.0.so
#7  0x0040079c in main ()

My analysis is this (line numbers are trunk's, case is "wish9.0 -use FOO"):

1) During intialization, Initialize() (generic/tkWindow.c:3187) is called by Tk_Init.
2) Initialize() calls TkCreateFrame() (generic/tkWindow.c:3435) , which errors out on generic/tkFrame.c:573 (-use expects an integer). Below the 'error:' label, it calls Tk_DestroyWindow().
3) Tk_DestroyWindow() (generic/tkWindow.c:1382) on line 1639 decrements the refcount of the main window, ans since this is the last one (and the first), Ttk_TkDestroyedHandler() is called on line 1692.
4) At this point, Ttk has not yet been initialized!. It is done *after* TkCreateFrame() on point 2), in Initialize(), line 3495, calling Ttk_Init().
5) Since it's not initialized (initialization path is Ttk_Init() on generic/ttk/ttkInit.c -> Ttk_StylePkgInit() in generic/ttk/ttkTheme.c ) , Ttk hasn't called Tcl_SetAssocData (generic/ttk/ttkTheme.c:1768) yet.
6) When called in point 3) from Ttk_TkDestroyedHandler() (generic/ttk/ttkTheme.c:533), GetStylePackageData() (generic/ttk/ttkTheme.c:463) calls Tcl_GetAssocData(). Since there's no matching Tcl_SetAssocData, it returns NULL as described in the docs.

In summary, anything that makes the call to TkCreateFrame() in point 2 fail will fire the bug.

fvogel added on 2024-09-19 20:44:47:
Two questions:

- You have reported this for 9.0 and have shown that is does not happen for 8.6. Did you identify what changed in 9.0 that created this bug? I gladly admit that your proposed patch fixes the issue, but I would be more comfortable if we understood why this patch is not needed in 8.6 (the code of Ttk_TkDestroyedHandler() is the same in 8.6 and 9.0). In other words, I'm wondering whether the proposed fix is the right one.

- Can you show the stack trace from gdb?

emiliano added on 2024-09-19 14:01:58:
Gdb points out at https://core.tcl-lang.org/tk/file?ci=tip&name=generic/ttk/ttkTheme.c&ln=541 . Further inspection shows that pkgPtr is NULL.
Patch attached solves the issue.

Attachments: