Skip to content
Tcl Source Code History Of Ticket 409a4e5f28
Login

History Of Ticket 409a4e5f28

Artifacts Associated With Ticket 409a4e5f28

  1. Ticket change [7737c2ef13] (rid 258984) by rocketship98 on 2026-10-18 10:23:43:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-markdown"
    4. comment initialized to:
      These were found by Claude doing mutation testing of the Tcl_ObjType v2 hooks. I tested each and got either seg faults or the wrong info as detailed below. The diagnosis is Claudes. 
      
      They use the
      test-only `lstring` type (tclTestABSList.c), so they need a tcltest shell built
      from the Tcl sources (`make tcltest`, then `./tcltest`). Start a fresh shell for
      each snippet, because F1 to F3 crash the shell. No shipped type has a
      setElementProc, so F1 and F3 only affect extensions that supply that hook.
      F1 to F4 were each run on a tcltest 9.1.1 build and reproduce as described.
      
      **F1. lpop segfaults (tclCmdIL.c ~2670-2682).**
      
          set l [lstring abcd]; lpop l 1; set r [list $l]; set l 5; lappend r x; puts $r
      
      Result: segfault. (On another build it printed `{} x` instead of the correct
      `{a c d} x`, so the symptom depends on memory reuse.)
      Cause (from reading the source, not confirmed in a debugger): the
      setElementProc branch calls TclUndoRefCount(listPtr) without a prior
      Tcl_IncrRefCount (tclObj.c: "The caller must have previously incremented the
      refCount"), so the reference count ends up one too low. lset (tclCmdIL.c ~4295)
      does increment.
      
      **F2. join of a one-element list segfaults (tclCmdIL.c ~2169-2196).**
      For a type with getElementsProc but no indexProc (the manual requires only
      lengthProc):
      
          join [lstring -not INDEX a] :
      
      Result: segfault. The `listLen > 1` test skips getElementsProc for length 1, and
      the single-element branch then calls TclObjTypeIndex, which has no proc.
      
      **F3. lset with a list of indices segfaults, compiled and uncompiled.**
      
          set l [lstring abc]; lset l {1 0} X
      
      Result: segfault. Path: Tcl_LsetObjCmd -> TclLsetList -> TclLsetFlat ->
      Tcl_ListObjGetElements -> SetListFromAny -> Tcl_GetStringFromObj.
      `lset l 1 0 X` is refused politely; the one-argument index list is not.
      (Possibly related to ticket 1, as both go through TclLsetFlat.)
      
      **F4. lindex with a non-literal negative index (INST_LIST_INDEX, tclExecute.c ~5037-5047).**
      The negative index is passed to the type's indexProc, unlike the literal-index
      instruction and Tcl_ListObjIndex. lstring's indexProc does not check it either
      and reads the byte before its buffer:
      
          proc idx {l i} {lindex $l $i}
          string length [idx [lstring abcde] -1]
          binary encode hex [encoding convertto utf-8 [idx [lstring abcde] -1]]
      
      Expected: 0 (empty string). Actual: 1, and the hex is 00, a NUL byte read from
      before the buffer. The value of that byte depends on memory contents.
      
      A test file covering these (47 tests) is available on request.
      
    5. foundin initialized to: "9.1.1 trunk (commit 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: "Important"
    12. status initialized to: "Open"
    13. submitter initialized to: "rocketship98"
    14. subsystem initialized to: "14. List Object"
    15. title initialized to:
      Abstract-list hooks: lpop and join/lset crash, lindex reads before buffer with a negative index
      
    16. type initialized to: "Bug"
  2. Ticket change [033780f006] (rid 259071) by serhiy.storchaka on 2026-10-18 18:49:47:

    1. icomment:
      All four reproduced on trunk. Proposed fix in branch [/timeline?r=abstract-list-hook-fixes|abstract-list-hook-fixes]:
        *  F1: <nowiki>[lpop]</nowiki> now takes a reference to the result of <tt>setElementProc</tt>, like <nowiki>[lset]</nowiki>. The compiled multi-index <nowiki>[lset]</nowiki> (<tt>INST_LSET_FLAT</tt>) had the same missing reference.
        *  F2: <nowiki>[join]</nowiki> uses <tt>getElementsProc</tt> for any non-empty list.
        *  F3: the same use-after-free as [526b01c0b9]; its fix in branch [/timeline?r=lset-abstract-list-uaf|lset-abstract-list-uaf] makes <tt>lset l {1 0} X</tt> give <tt>a X c</tt>.
        *  F4: ObjectType.3 requires <tt>indexProc</tt> to accept out-of-range indices, so the core may pass -1; the bug is in lstring's <tt>indexProc</tt>, which now checks <tt>index < 0</tt>.
      
      Also fixed: <tt>lset l X</tt> without indices called <tt>setElementProc</tt> with no indices (lstring then fails with "bad index"). Tests abstractlist-5.1 to 5.4.
      
    2. login: "serhiy.storchaka"
    3. mimetype: "text/x-fossil-wiki"
  3. Ticket change [caa598dd67] (rid 259072) by rocketship98 on 2026-10-18 19:08:01:

    1. icomment:
      Thanks, this caused a crash in my own extension that claude helped me write and he says there's a documention gap:
      
      Addition to F1: doc/ObjectType.3 (SETELEMENT section) is silent on two points, and the core’s callers disagree on the second.
      
      lpop calls setElementProc with valueObj == NULL, meaning “remove this element”. A type written from the manual crashes on lpop l 1. A real extension (a bit-array type) segfaulted this way. The only hint is a comment in TclLsetFlat (“NULL to ‘lpop’”).
      The manual does not say who owns the reference count of the returned Tcl_Obj. lset (tclCmdIL.c ~4295) increments it, while lpop (~2670-2682) calls TclUndoRefCount on it with no prior increment, which is the bug in F1.
      
      Suggest documenting both: that valueObj may be NULL for lpop, and whether the returned object comes with a reference. The fix for F1 then follows from the stated rule.
      
    2. login: "rocketship98"
    3. mimetype: "text/plain"
  4. Ticket change [1cff6701c4] (rid 259508) by apnadkarni on 2026-10-20 16:13:13:

    1. assignee changed to: "apnadkarni"
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
  5. Ticket change [fd11065b6b] (rid 261026) by apnadkarni on 2026-11-01 06:45:06:

    1. icomment:
      @rocketship98, @serhiy.storchaka, I would prefer that rather than having setElementProc be inconsistent with `TclLsetFlat`, we document that it should return a `Tcl_Obj` with the reference count incremented and owned by the caller (as is the case for `TclLsetFlat`). Do you see any issues with that approach?
      
      The documentation is silent but there is a direct mapping between `TclLsetFlat` and `setElementProc` and I suspect the original intent would have been for the latter to be consistent with the former.
      
      Eric, yes the abstract list manpage is sorely lacking. It basically just references the script level commands which is not sufficient for a C API. Fixing it will need some source diving.
      
    2. login: "apnadkarni"
    3. mimetype: "text/x-markdown"
  6. Ticket change [e1d703db32] (rid 261136) by apnadkarni on 2026-11-05 10:49:43:

    1. closedate changed to: "2461349.9512031134"
    2. closer changed to: "apnadkarni"
    3. icomment:
      Fixed for 9.0 in [92c75c9bbc] and for 9.1 in [71ed030c94].
      
      The changes proposed in this branch were applied as is so the assumption moving  forward is that setElementProc will return a Tcl_Obj without its reference count incremented (so potentially 0).
      
    4. login: "apnadkarni"
    5. mimetype: "text/x-fossil-plain"
    6. resolution changed to: "Fixed"
    7. status changed to: "Closed"