Artifacts Associated With Ticket 526b01
Ticket change [4c46fb4eca] (rid 258967) by rocketship98 on 2026-10-18 09:48:35:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-markdown"
- comment initialized to:
set l [lseq 5] lset l 1 0 X for {set i 0} {$i < 2000} {incr i} {lappend z [list $i]} On an -O2 build the loop segfaults (TclThreadAllocObj, object pool corrupted). On a `--enable-symbols=mem` build it panics at once: `Tcl_DbIncrRefCount: Tcl_Obj allocated in another thread`, from TclListObjSetElement (tclListObj.c:3289) called by TclLsetFlat (tclListObj.c:3108). Same with `lset l {1 0} X`, with `lreverse [lseq 5]` and `lrange [lseq 10] 2 6` as the list, and after a `lindex $l 1` has run on the lseq list. Not affected: single-index lset, native lists, lrepeat. Cause: in TclLsetFlat, `elemPtrs` comes from Tcl_ListObjGetElements on the arithseries list, i.e. its cached element array, with no reference added. `subListObj = elemPtrs[index]` is then passed to TclListObjSetElement(parentList, index, subListObj). That call converts the abstract list to a native list (SetListFromAny, building new elements through indexProc) and frees the arithseries internal rep, including the cache, so `subListObj` (refCount 1, owned only by the cache) is freed before it is stored. A fix could be to take a reference on subListObj around the conversion, or to convert parentList to a native list before reading elemPtrs. Found by mutation testing of the abstract-list code (using claude, so can't say i understand his reasoning). But it crashes my windows wish91.exe - foundin initialized to: "trunk 2fa2d8bbd9"
- is_private initialized to: "0"
- login: "rocketship98"
- priority initialized to: "5 Medium"
- private_contact initialized to: "b5d6d6e631648ab8145961ecf2e3fa064603ac0a"
- resolution initialized to: "None"
- severity initialized to: "Severe"
- status initialized to: "Open"
- submitter initialized to: "rocketship98"
- subsystem initialized to: "14. List Object"
- title initialized to:
lset with 2+ indices on an unshared lseq list is a use-after-free (crash)
- type initialized to: "Bug"
Ticket change [7b6aa86534] (rid 258983) by serhiy.storchaka on 2026-10-18 10:19:50:
- foundin changed to: "9.0.5, 9.1.0"
- icomment:
Confirmed on trunk and 9.0.5, and the analysis is right. Proposed fix in branch [/timeline?r=lset-abstract-list-uaf|lset-abstract-list-uaf]: <tt>TclLsetFlat()</tt> holds a reference to the sublist while storing it, the first option suggested here. Tests lset-19.1 and 19.2.
- login: "serhiy.storchaka"
- mimetype: "text/x-fossil-wiki"
Ticket change [d0d924c0dc] (rid 258988) by juliannoble2 on 2026-10-18 11:03:01:
- icomment:
Verified on one of APN's tclsfe9.1 builds and my own Tcl 9.0 builds (using non-standard zig build system) Can crash even without the for loop in an interactive tclsh set l [lseq 5] lset l 1 0 X or sometimes repeat the lset command. - login: "juliannoble2"
- mimetype: "text/plain"
- priority changed to: "7 High"
- icomment:
Ticket change [807eb50aec] (rid 259255) by serhiy.storchaka on 2026-10-19 11:58:16:
- login: "serhiy.storchaka"
- resolution changed to: "Fixed"
- status changed to: "Closed"
Ticket change [f74589ea78] (rid 259959) by serhiy.storchaka on 2026-10-22 17:19:27:
- closedate changed to: "2461332.9988056715"
- closer changed to: "serhiy.storchaka"
- login: "serhiy.storchaka"
Ticket change [3d3b82ea23] (rid 260820) by apnadkarni on 2026-10-30 04:23:30:
- closer changed to: "nobody"
- icomment:
Reopening as I **think** the fix is both incorrect and incomplete. In particular, the [lines](https://core.tcl-lang.org/tcl/file?ci=1b4052be16f50511&name=generic%2FtclListObj.c&ln=3219-3222) ``` /* It may be held only by a cache that is freed. [Bug 526b01c0b9] */ Tcl_IncrRefCount(subListObj); TclListObjSetElement(NULL, parentList, index, subListObj); Tcl_DecrRefCount(subListObj); ``` can land up calling TclListObjSetElement with a shared object which will cause a panic. Is there something that guarantees subListObj will only have a refcount of 0 before the IncrRefCount? However, an attempt to demonstrate is unexpected behavior: ``` % set l [lseq 5] 0 1 2 3 4 % set x X X % lset l 1 0 $x 0 X 2 3 4 % set m [lseq 5] 0 1 2 3 4 % lset m {2 0} $x -> crash ``` The fact that the above caused a crash and not a panic leaves me wondering if this is one bug or two. - login: "apnadkarni"
- mimetype: "text/x-markdown"
- resolution changed to: "Accepted"
- status changed to: "Open"
Ticket change [7f92289b87] (rid 260839) by serhiy.storchaka on 2026-10-30 09:12:21:
- icomment:
<tt>TclListObjSetElement()</tt> panics only if the list it modifies (<tt>parentList</tt>) is shared, and it is unshared at this point. The element may be shared: <tt>TclListObjSetElement()</tt> takes its own reference, and the code right after the call makes a copy if <tt>subListObj</tt> became shared. The added <tt>Tcl_IncrRefCount()</tt>/<tt>Tcl_DecrRefCount()</tt> only keeps the old element alive while it is stored back, since it may be held only by the cache of the abstract list, which is freed during the call. I cannot reproduce the crash with the current main (1436d27450) or 9.0 (336b75d084), on Linux (also under valgrind) and on Windows 11 (MSVC): <verbatim> % set m [lseq 5] 0 1 2 3 4 % lset m {2 0} X 0 1 X 3 4 </verbatim> What build and platform did you use? - login: "serhiy.storchaka"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [1fa9891c1f] (rid 260841) by apnadkarni on 2026-10-30 09:23:15:
- icomment:
Sorry, mea culpa. Was testing a branch, not trunk, but the wrong branch, abstract-list-hook-fixes instead of lset-abstract-list-uaf. Confused between the various lset/abstract list tickets and branches. Apologies for the waste of time.
- login: "apnadkarni"
- mimetype: "text/x-markdown"
- icomment:
Ticket change [a98ae8e267] (rid 260842) by apnadkarni on 2026-10-30 09:23:40:
- closedate changed to: "2461343.8914353242"
- closer changed to: "apnadkarni"
- icomment: "Changed back to closed."
- login: "apnadkarni"
- mimetype: "text/plain"
- resolution changed to: "Fixed"
- status changed to: "Closed"