Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Tcl_AppendStringsToObj -> abort

Closed / Fixed456892ffffffffffffffffffffffffffffffffffCreated 25 years agoUpdated 25 years ago
Descriptionplain2001-08-30 15:37:45
[belinda 6]--> tclsh8.4
% parray tcl_platform
tcl_platform(byteOrder) = bigEndian
tcl_platform(machine)   = sun4u
tcl_platform(os)        = SunOS
tcl_platform(osVersion) = 5.7
tcl_platform(platform)  = unix
tcl_platform(threaded)  = 1
tcl_platform(user)      = chall
% puts $tcl_patchLevel
8.4a3
% proc mt_lslice {l s} {
    set r [list]    
    foreach i $l {
        lappend r [lindex $l $i]
    }   
    return $r
}
mt_lslice [list a b c d e f] [list 1 3 5]
Tcl_AppendStringsToObj called with shared object
Abort (core dumped)

Of course, my proc is wrong and should say:

proc mt_lslice {l s} {
    set r [list]    
    foreach i $s {
        lappend r [lindex $l $i]
    }   
    return $r
}
% mt_lslice [list a b c d e f] [list 1 3 5]
b d f
msoferplain2001-08-30 22:47:34
Logged In: YES 
user_id=148712

This is a bug in the new bytecompiled implementation of
lappend; the crash does not occur in tcl8.3, nor when you

eval lappend r [lindex $l $i]
dgpplain2001-08-30 23:09:37
Logged In: YES 
user_id=80530

Assigning to Kevin Kenny.  His TIP 22/33 changes
should take care of this.  Make sure to add this
to the test suite!
dgpplain2001-08-30 23:21:03
Logged In: YES 
user_id=80530

&(@W#%&@Y SF not managing changes crossing in
mid-stream *U#*Y$
msoferplain2001-08-31 03:00:27
Logged In: YES 
user_id=148712

Weird ... 

[mig@mini generic]$ tclsh8.4
% info patch
8.4a3
% proc tst0 {} {
    set r [list]    
    lindex a a
}
% proc tst1 {} {
    set r {}    
    lindex a a
}
% tst1
bad index "a": must be integer or end?-integer?
% tst0
Tcl_AppendStringsToObj called with shared object
Aborted (core dumped)
msoferplain2001-08-31 04:06:39
Logged In: YES 
user_id=148712

The symptom goes away if you do
    Tcl_ResetResult(interp)
in TclGetIntForIndex (generic/tclUtil.c, line 2185) before
appending to the result.

This does not yet leave me utterly satisfied that the
disease is gone:
(a) I'm not sure this is otherwise OK and doesn't introduce
other bugs
(b) I still do not understand *why* the interp's result was
shared in one case and not in the other ...
hobbsplain2001-08-31 06:23:45
Logged In: YES 
user_id=72656

This has something special to do with [list] in 8.4a4, but 
also in relation to TclGetIntForIndex, because trying with 
string index (now compiled in 8.4 as well), I also get the 
crash:

lorax [~/cvs/tcl/tcl8.4/unix/linuxd] 772 > tclsh8.4
% proc tst0 {} { set r [list] ; string index a b }
% tst0
Tcl_AppendStringsToObj called with shared object
Abort
lorax [~/cvs/tcl/tcl8.4/unix/linuxd] 773 > tclsh8.4
% proc tst0 {} { set r {} ; string index a b }
% tst0
bad index "b": must be integer or end?-integer?

I suspect as well that the Tcl_ResetResult is a good thing 
one way or the other, but I'm going to look for a possible 
alternative to what may be causing it (related to [list]).
hobbsplain2001-08-31 06:31:15
Logged In: YES 
user_id=72656

Getting a little more specific, it looks like the failure 
really is in result handling in the bcc with respect to 
appending to the result.  Note this error result:

% proc t0 {} { linsert a 0 b ; lindex a b }
% t0
b abad index "b": must be integer or end?-integer?

If you do that in 8.3 (where lindex isn't compiled),
you don't have the "b a" in the result.
msoferplain2001-08-31 08:33:55
Logged In: YES 
user_id=148712

This seems to be an unfortunate combination of two semi-bugs
(aka
optimisations?): 
  
1 - TclCompileScript (tclCompile.c, ll. 838) just POPs the
previous
command result from the stack but does not reset the
interp's
result. This is OK as (in principle) bcc'ed commands
communicate their  
results on the stack and do not set the interp's result.
Compare this
to clearing the result before each invocation of a command
or
evaluation of a script.

2 - Some error messages are *appended* to the interp's
result. This is
necessary for the buildup of the error call stack, but I
think the
interp's result should be reset by the error *originators*
(in these
examples, TclGetIntForIndex) before appending.

I would say that #2 should be hunted down, #1 is valuable.
As a
general rule, commands and functions should not assume that
they get
an empty result when they are called; they should set the
result, not
append to it.

-----
Additionally:
"set r [list]" triggers a core dump because it leaves a
shared object
in the interp's result: the same object is the value of r.
The
following function behaves correctly

proc tst2 {} {
    set r r[list]
    lindex a a
}

Note that if you *force* other commands to communicate via
the interp's 
result, you reproduce the behaviour: the following dumps
core too

proc tst3 {} {
    set set set
    $set r 5
    lindex a a
}

----
IN CONCLUSION:

(a) this particular bug is fixed by resetting the interp's
result in
TclGetIntForIndex. 

(b) there may be other similar bugs lurking in the
background, waiting
to trip the unwary; a hunt for functions abusingly appending
to the
result without first resetting it first should be done.
hobbsplain2001-09-01 04:20:51
Logged In: YES 
user_id=72656

More particularly, we have to watch utility functions that 
don't reset the result, because if it is simply a non-bcc 
command that would follow the 'set r [list]', then the 
invocation instruction does reset the result.

After a look (not exhaustive, but not brief) through the 
sources, it seems like most other calls were preceded by 
the appropriate Tcl_ResetResult.

Added TclGetIntForIndex fix and tests to 8.4a4cvs.
msoferplain2002-04-29 23:30:30
Logged In: YES 
user_id=148712

I think this is a symptom of the same "disease" described in
bugs #533364 and #550142.
Log in (anonymous is fine) to add a comment.