Tcl Source Code

View Ticket
Login
Ticket UUID: 1899164
Title: Cached bytecode not typed; expr vs. script
Type: Bug Created on: 2008-02-21 21:55:45
Submitter: dgp Assigned to: dgp
Subsystem: 47. Bytecode Compiler Severity:
Priority: 8 Last modified: 2008-03-08 04:08:54
Status: Closed Closed by: dgp
Resolution: Fixed Closed on: 2008-03-07 21:08:54
Version: obsolete: 8.4.18
Description:
The implementation of Tcl_ExprObj()
tries to detect when the Tcl_Obj
passed in is a PRECOMPILED "bytecode"
value, and then validate interps and
forgive any epoch expiration before
executing the bytecode.

This is incorrect, because a PRECOMPILED
value has been compiled as a script to
produce the bytecode, and that bytecode
is not a valid set of instructions to
interpret the same string as an expression.
([eval {0}] and [expr {0}] are different!)

Worse still, the cleanup stages at the
end of Tcl_ExprObj() will free the bytecode,
and since PRECOMPILED values suppress the
original string rep., the original bytecode
will be lost with no way to recover it.

Attached is a patch to make Tcl_ExprObj()
do the only sensible thing -- raise an
error.

No one has noticed this, probably because
it appears difficult and unnatural to
pass a PRECOMPILED value to Tcl_ExprObj(),
possibly even requiring internals intrusion
to accomplish it.

If there's a way to create a test case for
this with reasonable effort in either the
Tcl or the tbcload test suite, I'd appreciate
that contribution.  Otherwise, just review
and commit this fix please.
User Comments:
dgp added on 2008-02-22 04:55:46:

File Added - 267449: precompexpr.patch

dgp added on 2008-02-22 22:11:18:
Logged In: YES 
user_id=80530
Originator: YES


The problem is more widespread
than I thought:

% proc 0+0 {} {return WTF?}
% set script { 0+0 }
 0+0
% time $script
34 microseconds per iteration
% expr $script
WTF?

dgp added on 2008-02-22 22:11:19:

File Deleted - 267449:

dgp added on 2008-02-22 22:16:55:
Logged In: YES 
user_id=80530
Originator: YES

% proc 0+0 {} {return WTF?}
% set script 0+0
0+0
% time $script
40 microseconds per iteration
% expr $script
WTF?
% append script { }
0+0
% expr $script
0
% time $script
5 microseconds per iteration
% expr $script
0
% if 1 $script
0

dgp added on 2008-02-23 00:15:18:
Logged In: YES 
user_id=80530
Originator: YES


I misread the codePtr->refCount
management, so I was wrong about
the danger of losing the precompiled
bytecode.

Because cached bytecode is not
so transient, the dangers of
using the wrong bytecodes do
flow both ways.

dgp added on 2008-02-23 00:36:37:
Logged In: YES 
user_id=80530
Originator: YES


this isn't about tbcload anymore,
so assigning to msofer for assistance
when he returns.

dgp added on 2008-03-05 01:52:13:
Logged In: YES 
user_id=80530
Originator: YES


tests committed

dgp added on 2008-03-08 02:26:48:
Logged In: YES 
user_id=80530
Originator: YES


Fixed on HEAD for 8.5.2.

dgp added on 2008-03-08 04:08:54:
Logged In: YES 
user_id=80530
Originator: YES


and fixed for 8.4.19 too.