Skip to content
Tcl Source Code History Of Ticket 526b01
Login

History Of Ticket 526b01

Artifacts Associated With Ticket 526b01

  1. Ticket change [4c46fb4eca] (rid 258967) by rocketship98 on 2026-10-18 09:48:35:

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

    1. foundin changed to: "9.0.5, 9.1.0"
    2. 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.
      
    3. login: "serhiy.storchaka"
    4. mimetype: "text/x-fossil-wiki"
  3. Ticket change [d0d924c0dc] (rid 258988) by juliannoble2 on 2026-10-18 11:03:01:

    1. 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.
      
    2. login: "juliannoble2"
    3. mimetype: "text/plain"
    4. priority changed to: "7 High"
  4. Ticket change [807eb50aec] (rid 259255) by serhiy.storchaka on 2026-10-19 11:58:16:

    1. login: "serhiy.storchaka"
    2. resolution changed to: "Fixed"
    3. status changed to: "Closed"
  5. Ticket change [f74589ea78] (rid 259959) by serhiy.storchaka on 2026-10-22 17:19:27:

    1. closedate changed to: "2461332.9988056715"
    2. closer changed to: "serhiy.storchaka"
    3. login: "serhiy.storchaka"
  6. Ticket change [3d3b82ea23] (rid 260820) by apnadkarni on 2026-10-30 04:23:30:

    1. closer changed to: "nobody"
    2. 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.
      
    3. login: "apnadkarni"
    4. mimetype: "text/x-markdown"
    5. resolution changed to: "Accepted"
    6. status changed to: "Open"
  7. Ticket change [7f92289b87] (rid 260839) by serhiy.storchaka on 2026-10-30 09:12:21:

    1. 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?
      
    2. login: "serhiy.storchaka"
    3. mimetype: "text/x-fossil-wiki"
  8. Ticket change [1fa9891c1f] (rid 260841) by apnadkarni on 2026-10-30 09:23:15:

    1. 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.
      
    2. login: "apnadkarni"
    3. mimetype: "text/x-markdown"
  9. Ticket change [a98ae8e267] (rid 260842) by apnadkarni on 2026-10-30 09:23:40:

    1. closedate changed to: "2461343.8914353242"
    2. closer changed to: "apnadkarni"
    3. icomment: "Changed back to closed."
    4. login: "apnadkarni"
    5. mimetype: "text/plain"
    6. resolution changed to: "Fixed"
    7. status changed to: "Closed"