Skip to content
Tcl Source Code View Ticket
Login

View Ticket

arguments to string commands are passed by reference

Closed / Fixed218001ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 00:51:51
OriginalBugID: 684 Bug
Version: 8.0.3
SubmitDate: '1998-10-01'
LastModified: '1999-08-06'
Severity: MED
Status: Closed
Submitter: stanton
ChangedBy: hobbs
OS: All
OSVersion: NA
Machine: NA
FixedDate: '2000-10-25'
FixedInVersion: NA
ClosedDate: '1999-08-06'


Date: Wed, 23 Sep 1998 06:04:38 -0700 (PDT)
To: [email protected]
Subject: Tcl 'C' compiled parameters "argv" are passed by reference, not by value!
From: [email protected]
Content-type: text

Bug report from:  [email protected]

bugrfe:  Bug
Product:  tcl8.0.3
Version:  1.0
priority:  2
app:  in-house extension
interp:  Tclsh
interpv:  8.0.3
os:  Solaris
OSVers:  5.5.1
Synopsis:  Tcl 'C' compiled parameters "argv" are passed by reference, not by value!  
Detail:  A Tcl 'C' routine (Tcl_CreateCommand generated) which alters the "argv" parameter internally within the 'C' routine, has the altered parameter passed back to the Tcl variable.  In addition, all Tcl variables tagged to the initial variable also get altered (all have a common referenct point to data storage).

The following is the "C" code in question :
================================

#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include "tcl.h"

int 
TestCmd ( ClientData  clientData
        , Tcl_Interp *interp
        , int         argc
        , char       *argv[]
        )
{
  int i;  
  printf( "\nargv[1] @ %d .... argv[2] @ %d\n\n", argv[1], argv[2]);
  Tcl_AppendResult( interp
                  , "Before : argv[1] = >>"
                  , argv[1]
                  ,"<< ... argv[2] = >>"
                  , argv[2]
                  ,"<<\n"
                  , (char *)NULL 
                  );
  for (i=0; i<strlen(argv[1]); i++)
    argv[1][i] = toupper(argv[1][i]);                
  for (i=0; i<strlen(argv[2]); i++)
    argv[2][i] = tolower(argv[2][i]);      
  Tcl_AppendResult( interp
                  , "After :  argv[1] = >>"
                  , argv[1]
                  ,"<< ... argv[2] = >>"
                  , argv[2]
                  ,"<<\n"
                  , (char *)NULL 
                  );
  return (0);
}

int 
Test_Init (Tcl_Interp *interp)  
{ 
  Tcl_CreateCommand( interp
                   , "test"
                   , TestCmd
                   , (ClientData *) NULL
                   , (Tcl_CmdDeleteProc *) NULL
                   );  
  return (0);
}

Now considet the following script :
==========================
set T1  "aaaaa"
set T11 $T1
set T2  "BBBBB"
set T22 $T2
puts "Variable Values prior to running the test"
puts "T1 = $T1 .... T11 = $T11"
puts "T2 = $T2 .... T22 = $T22"
puts " "
puts "Running the test on Variables T1 as argv\[1\] and T2 as argv\[2\]"
puts [test $T1 $T2]
puts "Variable Values post to running the test"
puts "T1 = $T1 .... T11 = $T11"
puts "T2 = $T2 .... T22 = $T22"
puts " "

Expected Result :
==============

Variable Values prior to running the test
T1 = aaaaa .... T11 = aaaaa
T2 = BBBBB .... T22 = BBBBB

Running the test on Variables T1 as argv[1] and T2 as argv[2]

argv[1] @ 580472 .... argv[2] @ 580456

Before : argv[1] = >>aaaaa<< ... argv[2] = >>BBBBB<<
After :  argv[1] = >>AAAAA<< ... argv[2] = >>bbbbb<<

Variable Values post to running the test
T1 = aaaaa .... T11 = aaaaa
T2 = BBBBB .... T22 = BBBBB

Result Obtained :
==============

Variable Values prior to running the test
T1 = aaaaa .... T11 = aaaaa
T2 = BBBBB .... T22 = BBBBB

Running the test on Variables T1 as argv[1] and T2 as argv[2]

argv[1] @ 580280 .... argv[2] @ 622680

Before : argv[1] = >>aaaaa<< ... argv[2] = >>BBBBB<<
After :  argv[1] = >>AAAAA<< ... argv[2] = >>bbbbb<<

Variable Values post to running the test
T1 = AAAAA .... T11 = AAAAA
T2 = bbbbb .... T22 = bbbbb

In addition if we should run the following:
===============================

set T1  "aaaaa"
set T11 $T1

puts "Variable Values prior to running the test"
puts "T1 = $T1 .... T11 = $T11"
puts " "

puts "Running the test on Variables T1 as argv\[1\] and T11 as argv\[2\]"
puts [test $T1 $T11]

puts "Variable Values post to running the test"
puts "T1 = $T1 .... T11 = $T11"
puts " "

we find the following (both argv[1] and argv[2] have same reference):
========================================================
Variable Values prior to running the test
T1 = aaaaa .... T11 = aaaaa

Running the test on Variables T1 as argv[1] and T11 as argv[2]

argv[1] @ 580280 .... argv[2] @ 580280

Before : argv[1] = >>aaaaa<< ... argv[2] = >>aaaaa<<
After :  argv[1] = >>aaaaa<< ... argv[2] = >>aaaaa<<

Variable Values post to running the test
T1 = aaaaa .... T11 = aaaaa

I have the "C" code and test scripts available if needed.



The problem is that the argv array is constructed from references to shared strings instead of being copies as they were in 7.6.  This was done to improve the performance of string commands in 8.0, but is not backwards compatible for commands that expected to be able to modify the strings in place.  A workaround is to copy the strings in the commands that need to modify the strings.  It is not clear whether we should consider this a feature change rather than a bug.

The above should be considered a feature, or the expected result,
and not a bug.  The incoming argv should be considered shared, and
not modified in place. 
-- 08/06/1999 hobbs
welch Closed ticket [218001ffff]. [History diff]2000-10-26 08:12:11
Log in (anonymous is fine) to add a comment.