Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: ec92617e78b434d65d684990790f9715be81bed185fcceda89ed1b1b2c2219b7
Ticket: 86b3c15f0c99a64f3aeb09eba009bccc1b726148
::unknown has infinite recursion in a corner case
User & Date: kjnash on 2024-02-02 19:14:38
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-plain"
  4. comment changed to:
    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 ""
            }
    
  5. foundin changed to: "all"
  6. is_private changed to: "0"
  7. login: "kjnash"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "a774b1f5653dc3360e29ea7d635a64e94c9f1dab"
  10. resolution changed to: "None"
  11. severity changed to: "Minor"
  12. status changed to: "Open"
  13. submitter changed to: "kjnash"
  14. subsystem changed to: "18. Commands M-Z"
  15. title changed to: "::unknown has infinite recursion in a corner case"
  16. type changed to: "Patch"