|
2012-03-30
| ||
| 15:59 | • Closed ticket [3513146fff]: string index permits invalid charIndex plus 7 ot... artifact: b1ffedfb19 user: dkf | |
| 08:54 | • New ticket [3513146fff]. artifact: a24bf49341 user: blacksqr | |
string index permits invalid charIndex
Closed / Invalid3513146fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 15 years ago
Descriptionplain2012-03-30 08:54:19
% set x a a % set val [string index $x end]] a] Note the extra close bracket at the end of the last statement. Instead of throwing an error for imbalanced brackets, the extra bracket is strangely passed through to the result. Tested on Tcl 8.5.8 and 8.6b2 (Ubuntu)
dkfplain2012-03-30 15:59:26
allow_comments - 1
The [string index] runs to the first balanced bracket. The second is just an ordinary character. Tcl concatenates strings by default. Here's the proof: a disassembly of that command:
% tcl::unsupported::disassemble script {set val [string index $x end]]}
ByteCode 0x0x4dd10, refCt 1, epoch 3, interp 0x0x31c10 (epoch 3)
Source "set val [string index $x end]]"
Cmds 2, src 30, inst 23, litObjs 4, aux 0, stkDepth 3, code/src 0.00
Commands 2:
1: pc 0-21, src 0-29 2: pc 2-16, src 9-27
Command 1: "set val [string index $x end]]"
(0) push1 0 # "val"
Command 2: "string index $x end"
(2) startCommand +15 1 # next cmd at pc 17
(11) push1 1 # "x"
(13) loadScalarStk
(14) push1 2 # "end"
(16) strindex
(17) push1 3 # "]"
(19) concat1 2
(21) storeScalarStk
(22) done
Log in (anonymous is fine) to add a comment.