Article View: comp.lang.tcl
Article #23Re: Experiment: inline bytecode for [lrange] with variable indices (written by Claude)
From: et99
Date: Thu, 08 Oct 2026 18:35
Date: Thu, 08 Oct 2026 18:35
189 lines
8303 bytes
8303 bytes
On 10/8/2026 7:07 AM, Rich wrote:
> et99 <et99@rocketship1.me> wrote:
>> One interesting find along the way: when a command's compile routine
>> hits an error (for example [format %d notanumber] or [string is bogus
>> x]), the compiler doesn't raise a script-level error. It bakes a
>> "syntax" bytecode into the code, which I found in about 8 places in
>> the source. The error only appears at run time, and only if that
>> code is reached, so a mistake can sit dormant in production code for
>> years.
>
> That is likely to match Tcl interpreted script behavior. An 'if'
> clause (say for checking for an incorrect data in a var) that is not
> exercised by an explicit test can also hold a syntax error (the
> interpreter will only care that the 'if' body has matching curly braces
> at parse time) that can lay dormant until one day, years in the future,
> the incorrect data being tested for finally makes the 'if' test true,
> then the body is executed, and the syntax error can manifest itself.
Yes, quite true. The trick here was to use the fact that the compiler already found an error, but simply stashed that info away. Using getbytecode (better than disassemble, since the later truncates some text) one can find the syntax error bytecode. It has a specific signature, two pushes and then the syntax bytecode.
In order to load all the packages, I needed to use a subprocess to first try to do the package require, since many would fail, and a few would crash or hang tcl when trying, maybe on loading a dll. If they loaded, then the main process would use them.
The ones that are t1-t7 were my test cases based on finding them in the tcl core code via a grep search.
if { $known } {
proc t1 {} {format %d nontanumber; set foo bar; format %x foo}
proc t2 {} { throw {"} "some message" }
proc t3 {} { subst {[format}} ;# "
proc t4 {} { string is bogus "somevalue" }
proc t5 {} { tcl::unsupported::assemble {push 1
push 2} }
proc t6 {} { tcl::unsupported::assemble {bogusOpcode} }
# t7: trips TclCompileScript's own "too many nested compilations (infinite
# loop?)" guard (iPtr->numLevels / 5 > iPtr->maxNestingDepth / 4). This is a
# compile-time recursion-depth check against runaway nested compilation of
# things like deeply nested "if" bodies -- 2000 levels comfortably clears
# the default threshold (~1250 with the default recursionlimit of 1000)
# with margin to spare on builds with a different default.
proc make_nested_if_body {n} {
set open [string repeat "if {1} {" $n]
set close [string repeat "}" $n]
return "${open}set x 1${close}"
}
interp recursionlimit {} 50
proc t7 {} [make_nested_if_body 500]
}
Here's the output. There's one that appears to have been the result of copy/paste of some code into several places. In the case of format, it only does this if everything is literal text, i.e. not a variable.
Discovered 917 package(s); 804 loaded into this interpreter, 113 failed.
=== Packages that failed to load ===
<snip>
Found 22991 procs - scanning now please wait
=== Procs that errored during getbytecode ===
:::: ":::" isn't a procedure
=== Procs with a bytecode-level (dormant) error opcode ===
<These were from my test cases>:
::t1 (2 occurrence(s))
at offset 10:
message : expected integer but got "nontanumber"
errorcode: TCL VALUE NUMBER
-code : 1
at offset 41:
message : expected integer but got "foo"
errorcode: TCL VALUE NUMBER
-code : 1
::t2 (1 occurrence(s))
at offset 16:
message : unmatched open quote in list
errorcode: TCL VALUE LIST QUOTE
-code : 1
::t3 (1 occurrence(s))
at offset 15:
message : missing close-bracket
errorcode: NONE
-code : 1
::t4 (1 occurrence(s))
at offset 10:
message : bad class "bogus": must be alnum, alpha, ascii, control, boolean, dict, digit, double, entier, false, graph, integer, li... (truncated, 197 chars total)
errorcode: TCL LOOKUP INDEX class bogus
-code : 1
::t5 (1 occurrence(s))
at offset 10:
message : stack is unbalanced on exit from the code (depth=2)
errorcode: TCL ASSEM BADSTACK
-code : 1
::t6 (1 occurrence(s))
at offset 10:
message : bad instruction "bogusOpcode": must be push, add, append, appendArray, appendArrayStk, appendStk, arrayExistsImm, arrayE... (truncated, 1661 chars total)
errorcode: TCL LOOKUP INDEX instruction bogusOpcode
-code : 1
::t7 (1 occurrence(s))
at offset 10:
message : too many nested compilations (infinite loop?)
errorcode: TCL LIMIT STACK
-code : 1
<These are from the packages>:
::asn::asnGetT61String (1 occurrence(s))
at offset 444:
message : not enough arguments for all format specifiers
errorcode: TCL FORMAT FIELDVARMISMATCH
-code : 1
::controlwidget::verticalAxis::Snit_methodcomputeLogHeight (1 occurrence(s))
at offset 279:
message : unbalanced open paren
in expression "...tom) - $options(-ytop)"
errorcode: TCL PARSE EXPR UNBALANCED
-code : 1
::math::geometry::areaParallellogram (1 occurrence(s))
at offset 88:
message : unbalanced open paren
in expression "...($x2 * $y1 - $x1 * $y2"
errorcode: TCL PARSE EXPR UNBALANCED
-code : 1
::critcl::MakeScalarLimited (1 occurrence(s))
at offset 2155:
message : missing operator at _@_
in expression "$minop_@_$maxop eq ">=<=")"
errorcode: TCL PARSE EXPR MISSING
-code : 1
::diagram::core::geo::areaParallellogram (1 occurrence(s))
at offset 88:
message : unbalanced open paren
in expression "...($x2 * $y1 - $x1 * $y2"
errorcode: TCL PARSE EXPR UNBALANCED
-code : 1
::diagram::basic::geo::areaParallellogram (1 occurrence(s))
at offset 88:
message : unbalanced open paren
in expression "...($x2 * $y1 - $x1 * $y2"
errorcode: TCL PARSE EXPR UNBALANCED
-code : 1
::diagram::element::geo::areaParallellogram (1 occurrence(s))
at offset 88:
message : unbalanced open paren
in expression "...($x2 * $y1 - $x1 * $y2"
errorcode: TCL PARSE EXPR UNBALANCED
-code : 1
::diagram::point::geo::areaParallellogram (1 occurrence(s))
at offset 88:
message : unbalanced open paren
in expression "...($x2 * $y1 - $x1 * $y2"
errorcode: TCL PARSE EXPR UNBALANCED
-code : 1
Done
-et
Technical Details
Message-ID:
<11a8nq5$3tnm$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11a46mp$2f14b$1@dont-email.me> <1791401131-1108@newsgrouper.org> <11a77uh$3gpfh$1@dont-email.me> <11a882k$3sq6e$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: et99 <et99@rocketship1.me> Newsgroups: comp.lang.tcl Subject: Re: Experiment: inline bytecode for [lrange] with variable indices (written by Claude) Date: Thu, 8 Oct 2026 11:35:47 -0700 Organization: A noiseless patient Spider Lines: 189 Message-ID: <11a8nq5$3tnm$1@dont-email.me> References: <11a46mp$2f14b$1@dont-email.me> <1791401131-1108@newsgrouper.org> <11a77uh$3gpfh$1@dont-email.me> <11a882k$3sq6e$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 08 Oct 2026 18:35:50 +0000 (UTC) Injection-Info: dont-email.me; logging-data="128758"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18jB3KqJIeztC3PGdAORPzo"; posting-host="f99c1eab37cc5ad5c75580fd173b29c9" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:27frXaGcod0vCkMcTzzCYqw3aMI= sha256:I3OFt6uygmTf0TjV2dBUeeUyyDL22W19vfMAv0ZqZZU= sha1:LnnmIgw/DUKiuEW/EBwekw7efuE= sha256:NWXZ1l8fKMe6hhna099jIQ1wftaziwVTFJqmwrDlApI= Content-Language: en-US In-Reply-To: <11a882k$3sq6e$1@dont-email.me>