Tcl Source Code

View Ticket
Login
Ticket UUID: 1671138
Title: foreach: empty var list -> inf loop
Type: Bug Created on: 2007-02-28 18:58:55
Submitter: dgp Assigned to: dkf
Subsystem: 47. Bytecode Compiler Severity:
Priority: 9 Immediate Last modified: 2007-03-01 23:35:14
Status: Closed Closed by: dkf
Resolution: Fixed Closed on: 2007-03-01 16:35:14
Version: obsolete: 8.5a6
Description:
This script hangs Tcl:

proc demo {} {
    foreach {} x {}
}
demo

By contrast, the non-compiled
[foreach] does this:

% foreach {} x {}
foreach varlist is empty

I'm not convinced that an error
is really the right response, but
infinite loop is definitely wrong.
User Comments:
dkf added on 2007-03-01 16:23:10:
Logged In: YES 
user_id=79902
Originator: NO

There is a way of detecting the difference without a loop:
  % proc foo {} {foreach {} x {error bar}}; foo
  bar
  % foreach {} x {error bar}
  foreach varlist is empty
Proposed fix: detect the empty-var-list case and don't compile.

dkf added on 2007-03-01 17:09:17:
Logged In: YES 
user_id=79902
Originator: NO

Fixed in HEAD. Backport needed.

dkf added on 2007-03-01 17:16:49:
Logged In: YES 
user_id=79902
Originator: NO

Backported.

dgp added on 2007-03-01 23:02:20:
Logged In: YES 
user_id=80530
Originator: YES


New test fails in 8.4.15:

==== foreach-9.1 compiled empty var list FAILED
==== Contents of test case:

    proc foo {} {
        foreach {} x {
            error "reached body"
        }
    }
    list [catch { foo } msg] $msg

---- Result was:
1 {}
---- Result should have been (exact matching):
1 {foreach varlist is empty}
==== foreach-9.1 FAILED