#5014 string index permits invalid charIndex

obsolete: 8.6b2
closed-invalid
5
2012-03-30
2012-03-30
No

% 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)

Discussion

  • Donal K. Fellows

    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

     
  • Donal K. Fellows

    • status: open --> closed-invalid
     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks