Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2024-08-25
11:11
Merge [519deeadfc87b383], "Fix [86b3c15f0c]: ::unknown has infinite recursion in a corner case", and... check-in: 666c5ebdd5 user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
2024-02-06
15:08 • Closed ticket [86b3c15f0c]: ::unknown has infinite recursion in a corner case</i... artifact: ee85950534 user: jan.nijtmans
14:58
Fix [86b3c15f0c]: ::unknown has infinite recursion in a corner case check-in: 524d68410a user: jan.nijtmans tags: core-8-6-branch
14:16
Proposed fix for [86b3c15f0c]: ::unknown has infinite recursion in a corner case check-in: 13fc1a9f95 user: jan.nijtmans tags: bug-86b3c15f0c
2024-02-02
19:15 • Add attachment init-unknown-86.patch to ticket [86b3c15f0c99a64f|86b3c1... artifact: a689e2b553 user: kjnash
19:14 • New ticket [86b3c15f0c] ::unknown has infinite recursion in a corner case. artifact: ec92617e78 user: kjnash

::unknown has infinite recursion in a corner case

Closed / Fixed86b3c15f0c99a64f3aeb09eba009bccc1b726148Created 3 years agoUpdated 3 years ago
Descriptionplain2024-02-02 19:14:38
The command ::unknown is careful to evaluate commands in the namespace of the caller - with one exception, which in a corner case can lead to infinite recursion.

The corner case is unusual but not perverse - it occurs when ::unknown is renamed into a different namespace.

Sometimes a package renames ::unknown - e.g. TclReadLine (a pure-Tcl replacement for GNU readline, https://wiki.tcl-lang.org/20215 ) renames ::unknown to ::_unknown and replaces it with its own ::unknown command, which calls ::_unknown.

While experimenting with improvements to TclReadLine, I decided to rename ::unknown into the ::TclReadLine namespace, in order to avoid pollution of the global namespace.  This exposes the bug. If the user types a command that exists in the ::TclReadLine namespace but not in the global namespace, the renamed ::unknown goes into infinite recursion: it believes it has found the command, but when it tries to execute it, it is not there.

A simple demonstration is to type these commands in an interactive shell:

  namespace eval ::foo {}
  rename unknown ::foo::unknown
  interp alias {} unknown {} ::foo::unknown
  proc ::foo::bar {} {}

  bar


The fix is simple, and is in the patch attached.  The bug is found in all versions of Tcl.

In view of the importance of ::unknown, I would appreciate review before committing this change.

diff -Naur original/library/init.tcl patched/library/init.tcl
--- original/library/init.tcl   2023-08-21 02:45:35.424433637 +0100
+++ patched/library/init.tcl    2024-02-02 18:41:18.049045928 +0000
@@ -371,7 +371,10 @@
            return -options $::tcl::UnknownOptions $::tcl::UnknownResult
        }
 
-       set ret [catch {set candidates [info commands $name*]} msg]
+       set ret [catch [list uplevel 1 [list info commands $name*]] msg]
+       if {$ret == 0} {
+           set candidates $msg
+       }
        if {$name eq "::"} {
            set name ""
        }
Referenced by3
jan.nijtmanswiki2024-02-06 15:08:08

Fixed [here].

Thanks for the report and the patch!

Log in (anonymous is fine) to add a comment.

Attachments: