From a2ae2a197461d1ba500dcff662b27cd0c8f35c9c Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Fri, 10 Oct 2014 19:06:23 +1000 Subject: namespace: disallow creation of import loops Signed-off-by: Steve Bennett --- nshelper.tcl | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'nshelper.tcl') diff --git a/nshelper.tcl b/nshelper.tcl index d0cb35a..a31b7c0 100644 --- a/nshelper.tcl +++ b/nshelper.tcl @@ -66,7 +66,19 @@ proc {namespace import} {args} { if {[namespace qualifiers $cmd] eq $current} { return -code error "import pattern \"$pattern\" tries to import from namespace \"$current\" into itself" } - alias ${current}::[namespace tail $cmd] $cmd + # What if this alias would create a loop? + # follow the target alias chain to see if we are creating a loop + set newcmd ${current}::[namespace tail $cmd] + + set alias $cmd + while {[exists -alias $alias]} { + set alias [info alias $alias] + if {$alias eq $newcmd} { + return -code error "import pattern \"$pattern\" would create a loop" + } + } + + alias $newcmd $cmd } } } -- cgit v1.1