Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2024-08-25
11:24
Merge [d9131bf7c80a970c]: Fix [8e666d7c95]: Redefining proc ::history locks the interpreter into a t... check-in: 47f278d801 user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
2024-02-07
16:32 • New ticket [af0e43c96e] some tailcall operations evade recursion limit checks</i... artifact: 71b6bfd95b user: dgp
13:06 • Closed ticket [8e666d7c95]: Redefining proc ::history locks the interpreter into... artifact: cfc2d914c3 user: jan.nijtmans
13:03
Fix [8e666d7c95]: Redefining proc ::history locks the interpreter into a tight loop check-in: 3fe9d2e419 user: jan.nijtmans tags: core-8-6-branch
12:09 • Ticket [8e666d7c95] Redefining proc ::history locks the interpreter into a tight... artifact: 3dae0ee9ed user: kjnash
2024-02-06
13:41 • Ticket [8e666d7c95]: 3 changes artifact: bc9d57009f user: jan.nijtmans
13:32
Proposed fix for [8e666d7c95]: Redefining proc ::history locks the interpreter into a tight loop closed check-in: 02f90739a1 user: jan.nijtmans tags: bug-8e666d7c95
2024-02-03
17:19 • Add attachment history-tclIndex-86.patch to ticket [8e666d7c956372d0|8e... artifact: 6bb42325e9 user: kjnash
17:18 • Ticket [8e666d7c95] Redefining proc ::history locks the interpreter into a tight... artifact: 9b0edc8751 user: kjnash
14:36 • New ticket [8e666d7c95]. artifact: 99a5c2cd51 user: kjnash

Redefining proc ::history locks the interpreter into a tight loop

Closed / Fixed8e666d7c956372d0df195e27d36a53fdbad9164dCreated 3 years agoUpdated 3 years ago
Descriptionplain2024-02-03 14:36:50
While investigating [fe4d7305a8], which has been fixed, I found another bug.

::history is a proc that calls the namespace ensemble ::tcl::history whose components are procs ::tcl::Hist*.  All these commands are defined in file library/history.tcl, are listed (except ::tcl::history) in library/tclIndex, and are loaded on demand by the autoloader.

File library/history.tcl also sets a trace on command ::history, so that if it is deleted, all the related commands ::tcl::* are deleted, and the array ::tcl::history that stores the command history is unset.

If ::history is renamed or deleted, the autoloader reloads it when the next command is entered into the interactive interpreter.

The user should be able to redefine proc ::history, to provide an alternative to the Tcl built-in command.  However, the attempt may cause the interpreter to cease functioning, notably when the new proc is identical to the old.  The interpreter enters a tight CPU-consuming loop, and never reaches the point where it can accept another command.

What seems to be happening is that the tailcall in ::history attempts to evaluate "history" in namespace ::tcl, expecting to evaluate ::tcl::history, but because that command is absent it evaluates ::history instead, leading to infinite recursion.  A fix is:

1. To list ::tcl::history in library/tclIndex

2. To request ::tcl::history explicitly in the tailcall

The fix is in the attached patch, which is listed below.

Other matters:

(a) Does the trace on ::history (deleting all dependencies ::tcl::*) serve any useful purpose, apart from saving memory?  I suggest removing it.

(b) The interpreter usually catches infinite recursion.  In this case it does not.

In view of the importance of ::history, I would appreciate review before committing this change.  If nobody objects I will also remove the trace mentioned above.


diff -Naur original/library/history.tcl patched/library/history.tcl
--- original/library/history.tcl        2024-01-18 17:18:18.000000000 +0000
+++ patched/library/history.tcl 2024-02-03 16:53:40.144116588 +0000
@@ -53,7 +53,7 @@
     }
 
     # Tricky stuff needed to make stack and errors come out right!
-    tailcall apply {arglist {tailcall history {*}$arglist} ::tcl} $args
+    tailcall apply {arglist {tailcall ::tcl::history {*}$arglist} ::tcl} $args
 }
 

 # (unnamed) --
diff -Naur original/library/tclIndex patched/library/tclIndex
--- original/library/tclIndex   2024-01-18 17:18:18.000000000 +0000
+++ patched/library/tclIndex    2024-02-03 16:45:46.591373179 +0000
@@ -20,6 +20,7 @@
 set auto_index(::auto_mkindex_parser::commandInit) [list source [file join $dir auto.tcl]]
 set auto_index(::auto_mkindex_parser::fullname) [list source [file join $dir auto.tcl]]
 set auto_index(history) [list source [file join $dir history.tcl]]
+set auto_index(::tcl::history) [list source [file join $dir history.tcl]]
 set auto_index(::tcl::HistAdd) [list source [file join $dir history.tcl]]
 set auto_index(::tcl::HistKeep) [list source [file join $dir history.tcl]]
 set auto_index(::tcl::HistClear) [list source [file join $dir history.tcl]]
Referenced by4
kjnash Ticket [8e666d7c95]: 4 changes [History diff]2024-02-03 17:18:58
jan.nijtmanswiki2024-02-06 13:41:11

> If nobody objects I will also remove the trace mentioned above.

It looks like this trace was added on purpose: https://core.tcl-lang.org/tcl/info/3135d5681f5949c8

Therefore, I think removing it is a bad idea (but better ask @dkf, as he did this commit)

Patch looks OK to me. Committed to a branch now.

kjnashplain2024-02-07 12:09:53
(a) OK, let's keep the trace, it was just a bit confusing when trying to understand the bug!

(b) A separate issue really.  Is it expected that the interpreter does not catch the infinite recursion in this case?
jan.nijtmanswiki2024-02-07 13:06:45

Fixed [here]. Thanks for the thorough analysis, and the patch!

> (b) A separate issue really ....

Yes, a separate issue. I have no idea whether the interpreter should/could catch this.

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

Attachments: