Tk Source Code

View Ticket
Login
Ticket UUID: f0b3fa9f66c74f7b9356f73d291d585106681536
Title: crash with invalid data in [image create photo]
Type: Bug Version: trunk
Submitter: emiliano Created on: 2026-02-16 18:28:49
Subsystem: 41. Photo Images Assigned To: jan.nijtmans
Priority: 5 Medium Severity: Critical
Status: Closed Last Modified: 2026-03-03 17:50:07
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2026-03-03 17:50:07
Description:
[image create photo] crashes when feed invalid data. The following script crashes Tk

$ wish9.1 
% image create photo -data \u0100
Segmentation fault (core dumped)


The source of the crash if the changed behavior of Tcl_GetByteArrayFromObj(), which on 9.X returns NULL if the passed string contains codepoints > 0xff.

Reported by the user wusspuss on the chat
User Comments: jan.nijtmans added on 2026-03-03 17:50:07:

Fixed [891c0545035ac390|here]

The error-message "invalid color name "Ā"" can be explained: the data can also be a list of colors like "crème red". For this "list of colors" format, \u0100 looks valid, except there is no color \u0100. Letting it crash is worse anyway.

If anyone has a better suggestion, please re-open this ticket.


obermeier added on 2026-02-16 19:37:25:

Note, that the crash also happens with Tk 9.0.3.

tkImg is not affected, as it uses a single function for reading ByteArrays, which checks the return value:

int tkimg_ReadInitString(
        tkimg_Stream *handle,
        Tcl_Obj *dataObj
) {
    Tcl_Size length;

    handle->data = Tcl_GetByteArrayFromObj(dataObj, &length);
    if (! handle->data) {
        return 0;
    }
    ...
}

emiliano added on 2026-02-16 18:55:05:

In the branch bug-f0b3fa9f66 I've commited a fix for the script firing the crash, but I suspect the same problem could affect other parts of the code. Also, TkImg is a candidate to be an affected extension.

A strange bit is this difference in the error message between 8 and 9 (after patching):

$ wish9.1 
% image create photo -data \u0100   
invalid color name "Ā"
% 
$ wish8.6 
% image create photo -data \u0100
couldn't recognize image data
%