Friday, August 15, 2014

ColdFusion 9.0.1 Bug: Function Executes Twice & Line Preceding Function Call Not Executed!?

This is just insane. I stumbled on a perfect storm of syntax that will lead to the craziest execution error.


First, write a simple function that accepts a struct and displays some text:
public void function SimpleFunction1 (struct incomingStruct) {
 WriteOutput("This is SimpleFunction1.");
}

Then, Call that function using a named, inline short-hand struct for the parameter:
SimpleFunction1(incomingStruct={item1="ok",item2="go"});

So far, this will work fine. However, wrap this function call in a try/catch block and not only will the function execute twice, but any instruction in the line preceding the function call will be ignored.
try {
    abort; //this line will never execute.
    SimpleFunction1(incomingStruct={item1="ok",item2="go"}); //this function will execute 2x
}
catch {
rethrow;
}

As a workaround, you can either remove the name from the argument in the function call, or make the argument a variable instead of an inline struct... I suppose you could also remove the try/catch block, but that's not really a good idea, is it?

Here is a complete example. Numbers One and Two should work fine. Number Three will fail.

try{
 //one
 WriteOutput("

SimpleFunction1 with inline struct

"); WriteOutput("WORKING!"); SimpleFunction1({item1="ok",item2="go"}); //two WriteOutput("

SimpleFunction1 with var substituted for inline struct

"); tempstruct ={item1="ok",item2="go"}; WriteOutput("WORKING!"); SimpleFunction1(incomingStruct=tempstruct); //three WriteOutput("

SimpleFunction1 inline shorthand struct and named parameter

"); WriteOutput("NOT WORKING!"); //this line will never execute SimpleFunction1(incomingStruct={item1="ok",item2="go"});//this function call is executed twice } catch (any e){ rethrow; } public void function SimpleFunction1 (struct incomingStruct) { WriteOutput("This is SimpleFunction1."); }

OUTPUT:
(I have to apologize. The script formatter I use strips out BR tags, so you'll need to add line breaks to the code above to make the output match the screen shot below.)
You may notice I use a "WriteOutput" command to demonstrate the line that fails to execute. You experienced devs out there are probably thinking "use CFFLUSH to clear the output buffer". I tried, but to no effect. Replace that WriteOutput with an Abort; It won't trigger.
Another thought was that perhaps something about the naming convention or the syntax was causing the try/catch to fire somehow. But, if you remove the try/catch, it executes fine.
If you try using a comment as the 'ignored line' it will skip that and ignore nearest preceding command.

8/15/2014 Update: I have reported this one to Adobe  https://bugbase.adobe.com/index.cfm?event=bug&id=3806526

8/15/2014 Update2: It appears to fail on CF 9.0.1, 9.0.2 and 10, but it is fixed and working properly in CF11!