Artifact eb41a2424a574463ea5f400ef906dbd5b9f70b596980d3d0c75e1e070e440260:
- File typeparse/noyau/lset-reference.patch — part of check-in [d03339047e] at 2026-10-23 11:38:18 on branch tip-764_tcl90 — Addressing "lset leak via TclLsetList (Claude)" Shared a file, used Claude Docs, executed 29 commands. The `lset` leak has been fixed (v15o, in `typeparse.zip`). Along the way, I also corrected a type-loss issue in `lpop` and a minor leak occurring during the management of typed variables. The leak was more extensive than anticipated. It affected any `lset` operation targeting a single index within a typed list—such as the common case `lset L 0 9`, whether compiled or not—resulting in a leak of approximately 170 bytes per call (17 MB over 100,000 calls in v15). The root cause lies within Tcl 9.0 itself. The core lacks a consistent convention regarding the reference returned by an abstract list’s `setElementProc`: * `TclLsetList` (used for `lset L 0 9` and `lset L {0} 9`) as well as uncompiled `lset` operations with multiple indices add a reference; therefore, the returned value must not carry an additional reference. * `INST_LSET_FLAT` (for compiled `lset M 0 1 9`) and `lpop L i` use the value as-is, implying it must already carry a reference. Our `AnnotListSetElement` followed the second convention, which caused the leak. I initially attempted the reverse—aligning the core with our approach—but Tcl’s own test abstract list follows the first convention, causing `abstractlist.test` to crash. The fix: adopting a single convention—the first one. * `AnnotListSetElement` now returns the value without an added reference. * `INST_LSET_FLAT` and `lpop` (with indices) now add the reference they expect to be present. Also discovered (and already present in v15): * `lpop L` (without an index) caused the variable to lose its type; `type get $L` would return empty. It would modify the value of a typed list in place; the variable would then hold an untyped value, effectively losing its contract. A typed list is now copied before any modification, consistent with the behavior of `lset`. There was a minor memory leak during the handling of a retyped value (`tclVar.c`): when a value—arriving without a reference—was replaced by its retyped version, the original value was never freed. Additionally, an improperly targeted cleanup operation occurred in the same section: if a contract was rejected, the code would target the intermediate value instead of the caller’s original value. Tests against v15: The new test `t68_lset_reference` measures memory usage (Linux only; it reports "no /proc" on other systems). In v15, it showed a leak in four scenarios, as well as a type loss during `lpop`; now, there are no leaks or type losses. The project's tests and examples show no new differences compared to v15, and all `.out` outputs pass. The Tcl test suites—`lset*`, `lpop`, `list*`, `cmdIL`, `abstractlist`, `lseq`, `set*`, `var`, `upvar`, `append*`, `incr*`, and `trace`—result in 1,903 passes, 84 skips, and zero failures, exactly as in v15. `valgrind` shows no issues. (user: Florentis, size: 2461) [annotate] [blame] [check-ins using]
--- a/generic/tclCmdIL.c 2026-10-23 13:16:40.984802414 +0200 +++ b/generic/tclCmdIL.c 2026-10-23 13:16:40.986669838 +0200 @@ -2664,7 +2664,10 @@ */ if (objc == 2) { - if (Tcl_IsShared(listPtr)) { + if (Tcl_IsShared(listPtr) || TclObjTypeHasProc(listPtr, replaceProc)) { + /* Une liste abstraite (une liste typée) n'est jamais modifiée en + * place : la valeur de la variable doit rester intacte jusqu'au + * rangement, qui la revalide (et garde son contrat). */ listPtr = TclListObjCopy(NULL, listPtr); copied = 1; } @@ -2679,7 +2682,13 @@ Tcl_Obj *newListPtr; Tcl_ObjTypeSetElement *proc = TclObjTypeHasProc(listPtr, setElementProc); if (proc) { + /* setElementProc rend une valeur SANS référence propre (comme + * TclLsetList et lset l'attendent) ; TclUndoRefCount ci-dessous + * en suppose une, comme pour TclLsetFlat. */ newListPtr = proc(interp, listPtr, objc-2, objv+2, NULL); + if (newListPtr != NULL) { + Tcl_IncrRefCount(newListPtr); + } } else { newListPtr = TclLsetFlat(interp, listPtr, objc-2, objv+2, NULL); } --- a/generic/tclExecute.c 2026-10-23 13:16:40.988478668 +0200 +++ b/generic/tclExecute.c 2026-10-23 13:16:40.990618475 +0200 @@ -5083,6 +5083,11 @@ objResultPtr = TclObjTypeSetElement(interp, valuePtr, numIndices, &OBJ_AT_DEPTH(numIndices), OBJ_AT_TOS); + /* Sans référence propre (convention de TclLsetList) ; la suite + * en attend une, comme pour TclLsetFlat. */ + if (objResultPtr != NULL) { + Tcl_IncrRefCount(objResultPtr); + } } else { objResultPtr = TclLsetFlat(interp, valuePtr, numIndices, &OBJ_AT_DEPTH(numIndices), OBJ_AT_TOS); --- a/generic/tclVar.c 2026-10-23 13:16:40.993661968 +0200 +++ b/generic/tclVar.c 2026-10-23 13:16:40.997203315 +0200 @@ -2147,10 +2147,20 @@ */ if ((varPtr->flags & VAR_TYPED) || TclIsAnnotated(newValuePtr)) { + Tcl_Obj *given = newValuePtr; + if (TclTypedVarPrepareWrite(interp, varPtr, &newValuePtr, &flags, &typedTmp) != TCL_OK) { + newValuePtr = given; /* c'est elle que l'appelant a confiée */ goto earlyError; } + if (newValuePtr != given && cleanupOnEarlyError) { + /* + * La valeur rangée est une autre (retypée) : celle que l'appelant + * a confiée sans référence (lpop, lset...) serait perdue. + */ + Tcl_BounceRefCount(given); + } } oldValuePtr = varPtr->value.objPtr;