Skip to content

Instantly share code, notes, and snippets.

@marcesher
Created January 12, 2012 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcesher/1603030 to your computer and use it in GitHub Desktop.
Save marcesher/1603030 to your computer and use it in GitHub Desktop.
cfc with no var scoping
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++ ){
}
}
}
<cfset obj = new BadObject()>
<cfoutput>
#obj.loop1()#
</cfoutput>
@marcesher
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment