Tk Source Code

View Ticket
Login
Ticket UUID: edd1a68d6ec3b578d0af12786975e3ab68580516
Title: Lack of bounds checking in argument parsing for some widgets
Type: Bug Version: 9.0.3
Submitter: makefile_dot_in Created on: 2025-12-04 22:33:44
Subsystem: 23. Option Parsing Assigned To: jan.nijtmans
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2026-09-19 11:19:30
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2026-09-18 16:00:53
Description:

For some options in certain widgets (e.g. frame or toplevel), trying to create them without providing a value causes a segfault. Example:

% frame .f -class
Segmentation fault     wish

This appears to be caused by the custom argument parsing logic in generic/tkFrame.c having no bounds checks for this case. Most other options seem to cope fine with this kind of malformed command.

User Comments: jan.nijtmans added on 2026-09-18 16:00:53:

Now merged to all branches. Including new testcases!

Many thanks! @serhiy.storchaka!


serhiy.storchaka added on 2026-09-18 10:03:04:

The fix [c289f69486] is on core-8-6-branch only; main and core-9-0-branch still crash. Branch frame-option-parity ([0f5f1d83f6]) applies the same check on main and adds tests for frame, toplevel and labelframe.


chrstphrchvz added on 2025-12-17 17:48:28:

Jan, thanks for committing the fix. Would it be a good idea to add a test like the following?

test frame-3.1b {TkCreateFrame procedure, bug edd1a6} -returnCodes error -body {
    frame .f -class
} -result {wrong # args: should be "frame pathName ?-option value ...?"}

erikleunissen added on 2025-12-07 13:57:55:
If the purpose of (objc % 2 == 1) is to determine whether the argument count is odd, then I believe that it's inefficient to perform a division followed by determining the remainder. A check on the last bit of objc is much more simple/efficient:

    (objc & 1)

chrstphrchvz added on 2025-12-05 20:32:33:

Given how simple it is to trigger this bug, I would suggest making a test case for it.

I think Tk typically avoids this issue elsewhere by checking objc % 2. Would the following change work?

--- generic/tkFrame.c.orig
+++ generic/tkFrame.c
@@ -494,7 +494,7 @@ CreateFrame(
     Colormap colormap;
     Visual *visual;
 
-    if (objc < 2) {
+    if (objc < 2 || objc % 2 == 1) {
 	Tcl_WrongNumArgs(interp, 1, objv, "pathName ?-option value ...?");
 	return TCL_ERROR;
     }


marc_culler (claiming to be Marc Culler) added on 2025-12-05 16:14:53:
That also causes a segfault on macOS with Tk 9.1.

oehhar added on 2025-12-05 10:41:55:
Can confirm the crash on 9.0.2 and windows.
Harald