Nested lset can modify list on error
My expectation is that when lset throws an error, it won't modify the list. However, the following script shows otherwise:
% set foo {}
% lset foo 0 0 x
x
% lset foo 0 1 x
{x x}
% lset foo 1 0 x
{x x} x
% lset foo 1 1 x
{x x} {x x}
% lset foo 2 1 x
list index out of range
% set foo
{x x} {x x} {}
% lset foo 3 0 x
{x x} {x x} {} x
% lset foo 3 1 x
{x x} {x x} {} {x x}
The command [lset foo 2 1 x] throws an error but also adds an empty element to the end of the list.
c4114a5e1fFix [3019348fff]: a nested [lset] which failed after appending an empty sublist for an end+1 index left that sublist in the list. Remove it on error. check-in2026-10-17 09:15:16
Still present in 8.6, 9.0 and 9.1: since TIP 331 ([41ad4c4b67]) TclLsetFlat() appends an empty sublist for an index equal to the list length before checking the remaining indices, and does not remove it when one of them fails. Inside a procedure the literal list is shared and copied, which hides the problem. Proposed fix in branch lset-undo-append-on-error: remove the appended sublist on error. Tests lset-17.1 to lset-17.3.
@serhiy.storchaka Looks fine but I've made some modifications in branch apn-lset-undo-append-on-error to eliminate some redundant/unnecessary checks. If you are ok with those changes, you can commit that branch to trunk and 9.0. Or I can, if you prefer.
Your changes look fine to me. The branch conflicted with trunk, where the fix for [526b01c0b9] holds a reference to the sublist around TclListObjSetElement(); I merged trunk into apn-lset-undo-append-on-error and kept that hold in your replace branch (lset-19.1 crashes without it). Please merge it to trunk and 9.0.
Fixed for 9.1 in [6a23978bf0]. Pending 9.0 merge.
Fixed for 9.0 in [27d3ea715e]. Closing. Thanks!