Tk Source Code

View Ticket
Login
Ticket UUID: 517e4ef666b40da2c8e0c75d1eccbecb737b94e0
Title: DlineXOfIndex reads an embedded image or window as a CharInfo
Type: Bug Version: trunk
Submitter: nab Created on: 2026-11-02 13:01:18
Subsystem: 18. [text] Assigned To: kevin_walzer
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2026-11-03 14:26:02
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2026-11-03 13:56:52
Description:

In DlineXOfIndex (tkTextDisp.c:9000, trunk 7d90e42b80), when the index follows an embedded image or window on a line with RTL text, the image or window segment is read as a CharInfo. An ASan build at -O0 (macOS, clang) reports a global-buffer-overflow 24 bytes past tkTextEmbImageType:

pack [text .t]
image create photo img -width 16 -height 16
.t insert end "1\nabc \u05e9\u05dc\u05d5\u05dd\n3"
.t image create 2.4 -image img
update
.t index "2.5 + 1 displaylines"

Up/Down reach it too. The values read are never used: the block can only return when chunkPtr->nextPtr is NULL, which next != NULL excludes. Clang drops it from -O1 on. The else branch (9010) makes the same cast without checking displayProc, unlike LineHasRtlChunk.

The block came with 3403347da9 (branch dancing_bidi); 9.1.0 does not have it.

Also, two warnings remain in tkTextDisp.c: textPtr is unused in BidiReorderChunks (1966), and ChunkBidiDir is unused with --disable-bidi (1403).

User Comments: serhiy.storchaka added on 2026-11-03 14:26:02:

textDisp-42.1 on nab-textdisp-42.1 ([92e84a1d58]), with Tcl and Tk built with --enable-symbols=all: no growth any more, but 5 times in 100 runs the result is -40.


nab added on 2026-11-03 14:09:38:

As Jan found, 42.1 has failed with the whole of textDisp.test since it came in; run alone, it passes, and I had only checked it that way. It is not a leak: textDisp.test turns debug on, and textDisp-35.1 leaves 1500002 lines in .t, whose heights are still being computed in the background when 42.1 runs. Each line appends to tk_textHeightCalc and tk_textInvalidateLine, and 42.1 counted these entries (the same 8806 on macOS, at a50ed0439c with --enable-symbols=mem).

92e84a1d58 on branch nab-textdisp-42.1 turns debug off during 42.1, as 35.1 does, and drops the knownBug constraint of dab199c0c6. With it the whole file passes, and 42.1 still fails when the char info of a dependent chunk is not freed.


jan.nijtmans added on 2026-11-03 13:56:52:

Actually, I did a bisect. This testcase is failing since commit [7d90e42b80f79199], which is the commit introducing this testcase. So, it appears to be failing since the beginning, and has no relation to this ticket.

Closing


jan.nijtmans added on 2026-11-03 13:08:23:

In an enable-symbols=all build on Github, I see:

textDisp.test
==== textDisp-42.1 the char info of a chunk after a tag boundary is freed FAILED
==== Contents of test case:
    growth {
	.t1 tag add x 1.20 1.25
	update
	.t1 tag remove x 1.20 1.25
	update
	# The widget appends to this variable at each layout.
	unset -nocomplain ::tkBidiLog
    }
---- Result was:
8806
---- Result should have been (exact matching):
0
==== textDisp-42.1 FAILED

I suspect this ticket is the cause of this. Can someone have another look?


kevin_walzer added on 2026-11-03 01:49:30:
Merged to main, thank you Nicolas and Serhiy for the report and fix.

serhiy.storchaka added on 2026-11-02 18:46:53:

Fixed on branch text-dlinexofindex-image, [98e29d0083]: DlineXOfIndex reads the CharInfo only of a character chunk, and the block which could never return is removed.


nab added on 2026-11-02 18:25:13:

No: 4cb0789f36 only changes BidiReorderChunks and textDisp.test, and DlineXOfIndex is the same on text-rtl-highlight as on trunk. On an ASan build of 4cb0789f36 at -O0 (macOS), the script above still reports the global-buffer-overflow 24 bytes past tkTextEmbImageType, in DlineXOfIndex (tkTextDisp.c:9046 on the branch).

Of the two warnings, the one on textPtr is gone on trunk (24e38ba03b, 5f2637b957); ChunkBidiDir is still unused with --disable-bidi.


kevin_walzer added on 2026-11-02 17:33:28:
Does https://core.tcl-lang.org/tk/info/4cb0789f36 address this?