Beware of Implicit Structs Bug in ColdFusion 9,0,1

This has been driving me nuts and I'm not sure how well documented this is, but I've stumbled across a bug in ColdFusion 9,0,1,274733 where implicit structs can end up as 'undefined' and crash your application.

I was working through an example from jQuery in Action, Second Edition (you know, because I was trying to figure something else out) when this happened to me again and I thought I would go ahead and share a simple way for others to test and let me know if it's happening to them.

Create a page called test.cfm and drop this code in it, point your browser to it and you'll probably see an error of 'Variable TERMS is undefined'.


<cfscript>
    if ( not IsDefined('term') ) {
        term = 'unknown';
    };
    term = lcase(term);

    terms = {
        'oil-tanned' = 'A method of leather tanning in which oils or fats are used to cure the leather. Such leather is usually very supple and has a matte or "oily" finish and is not generally polishable.',
        'full-grain' = 'Leather which has not been altered beyond hair removal. Full-grain leather is the most genuine type of leather, as it retains all of the original texture and markings of the original hide.',
        'vibram' = 'A brand of boot and shoe sole created by Vitale Bramani in the 1930s, orginally for climbing boots. They are easily identified by the distinctive yellow Vibram® octagon. The Vibram® brand is recognized worldwide as the leader in high performance soling products for outdoor, dress casual, and service footwear.',
        'goodyear welt' = 'The Goodyear welt is a method of attaching the sole of a shoe to the upper that is hand-stitched and allows multiple sole replacements, extending the life of the footwear.',
        'cambrelle' = ' A non-woven synthetic fabric used primarily as a lining for shoes, boots and slippers.',
        'cordura' = 'A certified fabric from INVISTA used in a wide range of products from luggage and backpacks to boots, to military wear and performance apparel. Cordura® is resistant to abrasions, tears and scuffs.',
        'gore-tex' = 'A water-proof and breathable fabric that offers superior insulating abilities in a light-weight fabric.',
        'stitch-down' = 'A method of boot construction that helps seal the boot against dirt, mud, and water and maximizes flexibility.',
        'unknown' = 'Unknown term.'
    };
</cfscript>
<cfoutput>#terms[term]#</cfoutput>

The funny thing is, if I move the code at the top of the page below the implicit struct, the page loads fine! Here's some code that should work now:


<cfscript>    
    terms = {
        'oil-tanned' = 'A method of leather tanning in which oils or fats are used to cure the leather. Such leather is usually very supple and has a matte or "oily" finish and is not generally polishable.',
        'full-grain' = 'Leather which has not been altered beyond hair removal. Full-grain leather is the most genuine type of leather, as it retains all of the original texture and markings of the original hide.',
        'vibram' = 'A brand of boot and shoe sole created by Vitale Bramani in the 1930s, orginally for climbing boots. They are easily identified by the distinctive yellow Vibram® octagon. The Vibram® brand is recognized worldwide as the leader in high performance soling products for outdoor, dress casual, and service footwear.',
        'goodyear welt' = 'The Goodyear welt is a method of attaching the sole of a shoe to the upper that is hand-stitched and allows multiple sole replacements, extending the life of the footwear.',
        'cambrelle' = ' A non-woven synthetic fabric used primarily as a lining for shoes, boots and slippers.',
        'cordura' = 'A certified fabric from INVISTA used in a wide range of products from luggage and backpacks to boots, to military wear and performance apparel. Cordura® is resistant to abrasions, tears and scuffs.',
        'gore-tex' = 'A water-proof and breathable fabric that offers superior insulating abilities in a light-weight fabric.',
        'stitch-down' = 'A method of boot construction that helps seal the boot against dirt, mud, and water and maximizes flexibility.',
        'unknown' = 'Unknown term.'
    };

    if ( not IsDefined('term') ) {
        term = 'unknown';
    };
    term = lcase(term);
</cfscript>
<cfoutput>#terms[term]#</cfoutput>

If I'm doing something wrong, please don't hesitate to point it out to me. Thanks!

Comments

Apparently, this bug only happens when using CFScript and works fine when using old school tag-based syntax (i.e., <cfif> <cfset>, etc.)
# Posted By Steve Withington | 1/13/11 4:33 PM
Odd, I can reproduce it with this small case:

<cfscript>
if ( not IsDefined('term') ) {
term = 'unknown';
};

terms = {};

</cfscript>
<cfset y = {name="foo"}>
<cfdump var="#variables#">

<cfdump var="#terms#">

# Posted By Raymond Camden | 1/13/11 4:39 PM
So get this: This will throw the bug (well not really since I'm just dumping Variables, but you get the idea):



<cfscript>
if (!IsDefined('term')) {
term = 'unknown';
};

terms = {};

</cfscript>
<cfdump var="#variables#">


but this WILL work right:



<cfscript>
if (!IsDefined('term')) term = 'unknown';

terms = {};

</cfscript>
<cfdump var="#variables#">

This smells like a parser error. Be sure to file a bug report.
# Posted By Raymond Camden | 1/13/11 4:42 PM
I found it. If you remove the ; at the end of the if clause, it should work perfectly. So this is bad:

if(...) { ... };

and this is good:

if(..) {..}
# Posted By Raymond Camden | 1/13/11 4:43 PM
@Ray,

that's bizzare ... i'll have to check, but i'm pretty sure i filed a bug report on this awhile back.

thanks for testing! :)
# Posted By Steve Withington | 1/13/11 5:15 PM
That is weird.

Is there a bug # on the bug tracker we can go vote on?
# Posted By Mark Mandel | 1/13/11 5:27 PM
I agree with Ray - this definitely feels like a parsing issue.
# Posted By Ben Nadel | 1/13/11 5:30 PM
@Mark, (et. al)

i couldn't find my original bug report so i submitted another one. feel free to vote on it: http://cfbugs.adobe.com

bug id: 85822
# Posted By Steve Withington | 1/13/11 8:18 PM
ColdFusion - Version 9,0,0,251028;
Edition - Developer ;
Web-Server - IIS;
Operating System - Windows XP;
Browser - Mozilla, IE

Result - Working fine
# Posted By sanoop | 1/14/11 12:12 PM
@sanoop: You are on CF900, not 901. Thats probably why it worked ok for you.
# Posted By Raymond Camden | 1/14/11 1:48 PM
Do you have a bug report link or bug number for this? I've lost quite a bit of time on this issue (see stackoverflow: http://stackoverflow.com/questions/6948541/odd-col...).

The issue was only on one of our nine servers which are all patched to the same version of ColdFusion.

I'd like to vote up the bug if one exists.

Thanks!

Aaron
# Posted By Aaron Greenlee | 8/11/11 5:54 PM
Just in case anyone else stumbles across this post, I've finally received an email from the Adobe ColdFusion Team which stated they were able to verify the bug.

The message indicated the fix is targeted for ColdFusion 10.0, Beta 1.

Cheers!
# Posted By Steve Withington | 9/26/11 8:56 PM

© 2024, Stephen J. Withington, Jr.  |  Hosted by Hostek.com

Creative Commons License   |   This work is licensed under a Creative Commons Attribution 3.0 Unported License.