Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 4c46fb4ecac3cb21270ec59949b90002564850f62fa0837c9bf0e83d21145e81
Ticket: 526b01c0b9921b90618f82c398dc2684a60a053a
lset with 2+ indices on an unshared lseq list is a use-after-free (crash)
User & Date: rocketship98 on 2026-10-18 09:48:35
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-markdown"
  4. comment changed 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
    
  5. foundin changed to: "trunk 2fa2d8bbd9"
  6. is_private changed to: "0"
  7. login: "rocketship98"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "b5d6d6e631648ab8145961ecf2e3fa064603ac0a"
  10. resolution changed to: "None"
  11. severity changed to: "Severe"
  12. status changed to: "Open"
  13. submitter changed to: "rocketship98"
  14. subsystem changed to: "14. List Object"
  15. title changed to:
     lset with 2+ indices on an unshared lseq list is a use-after-free (crash)
    
  16. type changed to: "Bug"