Skip to content
Tcl Source Code View Ticket
Login

View Ticket

segfault in [unset]

Closed / Fixed735335ffffffffffffffffffffffffffffffffffCreated 24 years agoUpdated 23 years ago
Descriptionplain2003-05-09 17:19:21
This script segfaults in Tcl 8.4.0 and later:

proc A { name } {
    upvar       $name var
    set var [list $name]
}
proc foo {script} {
    uplevel 1 $script
}
namespace eval test {
    foo {A name}
    unset name
}


Looks like something wrong with the new
"varName" Tcl_ObjType.

Key elements: upvar through an uplevel
to a namespace eval context; potential
shimmering/sharing of the $name value.
dgpplain2003-05-10 00:24:04
Logged In: YES 
user_id=80530

even simpler:

proc A { name } {
    upvar $name var
    set var $name
}
proc foo {script} {
    uplevel 1 $script
}
namespace eval test {
    foo {A name}
    unset name
}
msoferplain2003-05-10 09:26:25
Logged In: YES 
user_id=148712

even simpler (look ma, no uplevel!):

  proc A { name } {
      upvar $name var
      set var $name
  }
  namespace eval test A x
  namespace eval test unset x

Interestingly, no segfault if the last line is replaced with
   unset test::x
msoferplain2003-05-13 00:21:50
Logged In: YES 
user_id=148712

Committed temporary fix by disabling usage of
tclNsVarNameType, pending better understanding of the issue.
msoferplain2003-05-13 05:48:17
Logged In: YES 
user_id=148712

This temp fix also fixed a previously undetected bug in 8.4:

[mig@mini unix]$ tclsh
% set x 1
1
% unset x
% namespace eval test set x 0
0
% set x
0

After the temp fix, the result is as it should be:
  can't read "x": no such variable
msoferplain2004-03-28 03:22:50
Logged In: YES 
user_id=148712

Mmhhh ... the bug does not appear in a --enable-symbols=all
build :(
msoferplain2004-03-28 04:39:09
Logged In: YES 
user_id=148712

It is TCL_MEM_DEBUG that shuts the bug off
msoferplain2004-03-28 07:27:40
Logged In: YES 
user_id=148712

Some more puzzling data: I see no segfault now, but a panic:
"malformed bucket chain in Tcl_DeleteHashEntry" - even under
-DPURIFY.
It may be some kind of double-free of the variable structure?
msoferplain2004-05-22 10:47:32
Logged In: YES 
user_id=148712

Bug is now fixed. It was caused by insufficient protection
during unset, in the case where the varName and varValue
were a same Tcl_Obj of tclNsVarNameType.
Resolution (simplified, see TclObjUnsetVar2 for the real
thing): 
  varPtr->refCount++; /* NEW */
  Tcl_DecrRefCount(varPtr->valuePtr);
   varPtr->refCount--; /* NEW */
Log in (anonymous is fine) to add a comment.