Created
January 12, 2012 20:51
-
-
Save marcesher/1603030 to your computer and use it in GitHub Desktop.
cfc with no var scoping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component output="false"{ | |
function loop1(){ | |
x = 1; | |
for( x = 1; x LTE 100; x++ ){ | |
writeLog("x is #x#"); | |
loop2(); | |
} | |
return x; | |
} | |
function loop2 (){ | |
x = 1; | |
for( x = 1; x LTE 5; x++ ){ | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfset obj = new BadObject()> | |
<cfoutput> | |
#obj.loop1()# | |
</cfoutput> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why does this kill your server even though it's run in a single thread, even when you do this in a non-shared (i.e. application/server) scope?
loop1 sets X = 1. Because it's not var scoped, this creates "x" in the variables scope.
It starts the loop, then runs the loop2() function.
Loop 2 sets X = 1... again, since it's not var scoped it overwrites X in the variables scope.
It then loops 5 times. When the method exits, variables.X will == 6 (5++).
After loop2() exits the first time, loop1() will finish its first loop, incrementing X at the end. Thus, variables.X will now be 7.
The second iteration of Loop1() will start, with variables.x == 7 because loop2() overwrote X.
This entire cycle will continue, with X never exceeding 7. Since it never exceeds 7, the loop1() loop's termination condition is never met.