lset with 2+ indices on an unshared lseq list is a use-after-free (crash)
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
468e9b0813Ticket [3019348fff] Nested lset can modify list on error status still Open with 3 other changes ticket2026-10-19 06:29:22479483454fMerge trunk: keep the [526b01c0b9] reference hold in the replace branch of TclLsetFlat. check-in2026-10-19 06:28:08033780f006Ticket [409a4e5f28] Abstract-list hooks: lpop and join/lset crash, lindex reads before buffer with a negative index status still Open with 3 other changes ticket2026-10-18 18:49:471198f16e50Fix [526b01c0b9]: lset with several indices on an abstract list (lseq) used a freed sublist: it was held only by the cached elements of the list, which were freed when the list was converted to a native list. Hold a reference while storing it. check-in2026-10-18 10:17:10
Confirmed on trunk and 9.0.5, and the analysis is right. Proposed fix in branch lset-abstract-list-uaf: TclLsetFlat() holds a reference to the sublist while storing it, the first option suggested here. Tests lset-19.1 and 19.2.
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.
Reopening as I think the fix is both incorrect and incomplete.
In particular, the lines
/* 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.
TclListObjSetElement() panics only if the list it modifies (parentList) is shared, and it is unshared at this point. The element may be shared: TclListObjSetElement() takes its own reference, and the code right after the call makes a copy if subListObj became shared. The added Tcl_IncrRefCount()/Tcl_DecrRefCount() 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):
% set m [lseq 5]
0 1 2 3 4
% lset m {2 0} X
0 1 X 3 4
What build and platform did you use?
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.
Changed back to closed.