Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: c6632dc898c9aae70d083892ccf0aa906925c863
Ticket: 494348ffffffffffffffffffffffffffffffffff
foreach crashes in recursion
User & Date: nobody on 2001-12-17 22:06:29
Changes

  1. assignee changed to: "dkf"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    - Tcl Version: 8.x+;
    - OS: Solaris 5.x (Any);
    - Compiler: Sun CC 6.0 Upd 1;
    - Problem behavior:
    Tcl may freely convert the type of objects. 
    Foreach command stores the pointers to objects of loop 
    variables at the begin of the loop:
      for (i = 0;  i < numLists;  i++) {
         result = Tcl_ListObjGetElements(interp,
                        argObjv[1+i*2],
          &varcList[i], &varvList[i]);
    -> Var list is in varvList.
    Later it only checks if the variable list object has
    the list type: 
         if (argObjv[1+i*2]->typePtr != &tclListType) 
             result = Tcl_ListObjGetElements(interp,
                              argObjv[1+i*2],
    -> conditionally recreates the list
    and if the type is "list", it considers the list
    correct. Now suppose the loop body calls the
    function with the same name as the loop variable.
    This will convert the var list
    object to "cmdName" type and lose the pointers to all
    loop variable objects. Then, in recursion, we wil
    recreate the var list conversting "cmdName" to "list".
    After return from recursion to the first loop we will
    have the correct list but with another objects. 
    After checking  the type of var list object (it is
    "list"
    now) we'll continue
    to use stored pointers (varvlist) and will crash on the
    second  iteration. You will not crash if nobody used
    that freed memory, but it does not change anything.
       It's a real critical bug for us. To fix it I made
    the second list parsing (Tcl_ListObjGetElements)
    unconditional.
       Short testcase (I know it might be shorter):
    --------------
    proc inst {args} {
    }
    
    proc Something {} {
        inst
    }
    
    proc Test {level} {
        incr level
    
        puts "Test: level = $level"
    
        #inst "a"
        Something ;# you may call inst directly
    
        if {$level == 1} {
    set instlist {1 2}
        } else {
    set instlist {}
        }
        puts "instlist = /$instlist/"
    
        foreach inst $instlist {
    puts "inst = /$inst/"
    Test $level
        }
    }
    
    Test 0
    --------------------
    Regards,
    Yevgen Ryazanov
    
  4. foundin changed to: "obsolete: 8.3"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "nobody"
  9. subsystem changed to: "16. Commands A-H"
  10. title changed to: "foreach crashes in recursion"
  11. type changed to: "Bug"