Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-22
17:19 • Ticket [e87bcf819f] array for fails to detect array change made through upvar<... artifact: b08c22c457 user: serhiy.storchaka
2026-10-21
10:03 • Closed ticket [e87bcf819f]. artifact: ef3f21f132 user: serhiy.storchaka
2018-05-07
15:56 • Ticket [e87bcf819f]: 3 changes artifact: 5c8f767629 user: dkf
15:19 • Ticket [e87bcf819f]: 3 changes artifact: d6c5384582 user: dkf
15:11 • Ticket [e87bcf819f]: 4 changes artifact: 0b62fb06fe user: dkf
15:06 • Ticket [e87bcf819f]: 4 changes artifact: cd965326b6 user: dkf
2018-04-30
11:56 • Ticket [e87bcf819f]: 3 changes artifact: 6f591e569c user: dgp
2018-04-22
23:11 • Ticket [e87bcf819f]: 4 changes artifact: e1675ea83d user: dgp
23:09
Candidate fix for [e87bcf819f]. Makes sure the unset of a scalar variable linked to an array element... check-in: 701d26870f user: dgp tags: bug-e87bcf819f
19:13 • Ticket [e87bcf819f] array for fails to detect array change made through upvar<... artifact: da4ded8276 user: dgp
19:13 • Ticket [e87bcf819f]: 3 changes artifact: 81b9d0dc8c user: dgp
19:10 • New ticket [e87bcf819f]. artifact: abebe37771 user: dgp

[array for] fails to detect array change made through upvar

Closed / Fixede87bcf819f03cea4e35dca24cac7228766f53b0fCreated 9 years agoUpdated 41 days ago
Descriptionplain2018-04-22 19:10:03
Contrast these two:

% proc demo {} {
array set a {m 1 n 2 o 3 p 4}
try {                                 
    array for {k v} a {unset a(n)}
} on error {m} {
    puts "ERROR: $m"
} finally {
    puts [array get a]
}
}
% demo
ERROR: array changed during iteration
p 4 m 1 o 3


% proc demo {} {
array set a {m 1 n 2 o 3 p 4}
upvar 0 a(n) l
try {
    array for {k v} a {catch {unset l}}
} on error {m} {
    puts "ERROR:$m"
} finally {
    puts [array get a]
}
}
% demo
p 4 m 1 o 3
Referenced by1
  • 701d26870fCandidate fix for [e87bcf819f]. Makes sure the unset of a scalar variable linked to an array element terminates searches on that array. In order to accomplish this, some deep revisions in Var handling were done and new routine "Var * TclArrayContaining(Var *varPtr)" was created to return the (Var *) of the array that contains the (Var *) varPtr argument when it is an array element. Callers must verify the argument. The only code that was unhappy with these deep revisions was the varname method of TclOO, but revising it to the new situation was not too difficult. Arguably the code is better now, if a bit uglier and fragile to further deep Var tinkering. Second party review would be a good idea, as well as opinions on whether a backport to 8.6.* is warranted to fix corresponding bugs in the array enumeration suite of commands. check-in2018-04-22 23:09:47
dgpplain2018-04-22 19:13:11
Because this evades the protection against using
a Tcl_HashSearch after the hash table has changed,
it is likely this can be used to create segfaults.
dgpplain2018-04-22 19:13:46
The same failure probably exists in the [array startsearch], etc commands.
dgpplain2018-04-22 23:11:51
Candidate fix on branch bug-e87bcf819f.

dkf should comment on the changes made to TclOOBasic.c
dgpplain2018-04-30 11:56:54
Note that the machinery created here might be useful
in fixing the very old bug/RFE

https://core.tcl-lang.org/tcl/tktview?name=572889

as well.
dkfhtml2018-05-07 15:06:16

What I find interesting is that the [unset l] doesn't change the structure of the array. It's because there's still a reference to it (from the l variable) but it's still unexpected to anyone who isn't experienced with Tcl's guts.

dkfhtml2018-05-07 15:11:01
dkfhtml2018-05-07 15:19:26

The changes to tclOOBasic.c look OK to me. They won't interfere with TIP 500.

dkfhtml2018-05-07 15:56:29

And the bug is that the unset isn't killing the searches, and that's because the variable link is meaning that we're arriving at the element (which knows that it is in a TclVarHashTable) without going back to check if this makes things become invalidated. Which it can't do because we don't currently keep a pointer to the containing variable in the TclVarHashTable (the pointer could be NULL for non-array use cases, I guess). I don't know what the consequences of keeping such a pointer would be, apart from a small amount of extra memory usage.

The first option is correct; an error should be thrown by array for on internal modification.

serhiy.storchakawiki2026-10-21 10:03:36

Fixed in 9.0.0 as a side effect of TIP #634 ([a3a43c9363]): element hash tables now point to their array, so unsetting an element through an upvar link ends active searches and [array for] reports the change. Branch bug-e87bcf819f was not needed.

serhiy.storchaka Ticket [e87bcf819f]: 3 changes [History diff]2026-10-22 17:19:29
Log in (anonymous is fine) to add a comment.