Skip to content
Tcl Source Code View Ticket
Login

View Ticket

TEBC mishandled in [string replace] optimization ?

Closed / Fixed1af8de570511f68630bd54c24579ceef69c63546Created 11 years agoUpdated 11 years ago
Descriptionplain2016-03-19 00:43:51
This:
proc p1 { val } {
    set ret ""
    for { set i 0 } { $i < $val } { incr i } {
        set ret [format "0%s" $ret]
    }
    return $ret
}

proc p2 { val } {
    return [string replace [p1 4] 1 1 $val]
}

puts [ set s [p2 x] ]



Will cause a fault.

I think I've tracked it to an optimization for inlining a [string replace] (if the string going in is same length as going out, optimized routine), and it appears the _effects_ can be eliminated by adjusting [/artifact/c36c352f54aa5c40?ln=5272] from calling NEXT_INST_F() with 1,1,1, versus 1,0,0. I am not versed in the inner workings of the compiler though, so will leave this for experts and watch with interest what discussions follow.
dkfhtml2016-03-20 18:20:05

Reproduced. This will be present in 8.6.5.

dkfhtml2016-03-20 20:41:54

Fixed. Ended up adapting the code into this (the body of the test I'm using) because that avoids procedures and unnecessary puts calls.

    apply {{x y} {
	# Generate an unshared string value
	set val ""
	for { set i 0 } { $i < $x } { incr i } {
	    set val [format "0%s" $val]
	}
	string replace $val[unset val] 1 1 $y
    }} 4 x

dkfhtml2016-03-20 20:43:08

And also applied to trunk.

dkfhtml2016-03-20 20:52:21

The problem was not that the arguments to NEXT_INST_F were wrong (I've no idea why that masked the error without giving wrong answers!) but rather that I was flushing the internal representation wrongly in one case. More strictly, I was applying the "right" flush to the wrong object, and this was a case in the code that wasn't tested; I'd cut-n-paste some code (doing a tricky bit of poking inside the internals of the string internal representation) from one place to just a little bit below without adjusting what variable it was applied to, and the complexity of the code made it too easy for the eye to slide over the problem without immediately seeing it. As part of the fix, I refactored the code so that the tricky bit is in a macro, making it easier for the reader to see that this sort of problem no longer exists.

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