Ability To Leverage ColdFusion Bug Will Mean Errors Later

Posted April 7, 2009 at 9:36 AM by Ben Nadel

Tags: ColdFusion

NOTE: The following code is BAD CODE. This is just an exploration of BAD CODE. DO NOT COPY THIS CODE.

I was just reading over on Jon Hartmann's blog about problems he's been experiencing with ColdFusion 8's new implicit struct / array creation. He's coming to find out that implicit structs do not get block-level processed in ColdFusion 8, but rather, as Barney Boisvert pointed out on my blog, that it gets compiled as a collection of individual commands. To quote Barney's example:

  • <cfset a[arrayLen(a) + 1] = { foo = 2 } />

... gets compiled into to:

  • <cfset a[arrayLen(a) + 1] = structNew() />
  • <cfset a[arrayLen(a) + 1].foo = 2 />

... which is why it causes errors.

As I was reading Jon's post, it got me thinking; the fact that ColdFusion compiles in this way would actually give one the ability to leverage the piece-wise creation of structs. Take a look at this example I put together:

  • <!--- Create configuration settings. --->
  • <cfset objConfig = {
  • Root = GetDirectoryFromPath( GetCurrentTemplatePath() ),
  • Tags = (objConfig.Root & "tags\"),
  • COM = (objConfig.Root & "com\")
  • } />
  •  
  • <!--- Output congifuration object. --->
  • <cfdump
  • var="#objConfig#"
  • label="Configuration Settings"
  • />

As you can see in this demo, I am referring to the target variable, objConfig, within the definition of the very same variable, objConfig. This seems like some recursive nightmare (how something be defined as itself plus some other stuff???), and should in fact throw an error stating that "objConfig" is not defined. But, due to the way that ColdFusion compiles the implicit struct / array creation, the code above actually runs fine and when we CFDump out the config variable, we get:

 
 
 
 
 
 
ColdFusion 8 Implicit Struct Creation Bug Can Be Leveraged. 
 
 
 

The "sloppy" code ran just as someone might "intend" it to (even though it should work).

Aside from this being bad code to begin with, the real problem I see here is that once Adobe fixes implicit struct / array notation (hopefully in new version of ColdFusion), any code that "leverages" this bug will break. I'm not advocating in any way that Adobe even care about this kind of backwards compatibility - this is simply something that popped into my head when reading Jon's blog.




Reader Comments

Apr 7, 2009 at 10:12 AM // reply »
66 Comments

I dub this: the Nadel-Schrödinger Struct Creation Method.


Apr 7, 2009 at 10:30 AM // reply »
10,640 Comments

@Rick,

I had to look up Schrödinger. Are you equating my struct creation to half living half dead cat?? :)


Apr 7, 2009 at 10:31 AM // reply »
25 Comments

That code makes my head hurt.


Apr 7, 2009 at 11:35 AM // reply »
25 Comments

Oh my god. That is perhaps the most horrible CFML ever, despite the fact that it works perfectly on ColdFusion. No offense, Ben, but dear god.

If I ever see code like that in real life, I will beat the author senseless. Even if you live in Australia and it takes $2k and 30 hours to fly down there to do it.

And if Adobe maintains backwards compatibility for this horrible oversight at the expense of making it work like every other language in the world, I'll find as many of them as I can to beat senseless as well. It's horrible.


Apr 7, 2009 at 12:01 PM // reply »
10,640 Comments

@Barney,

Ha ha ha ha, please don't think that I would write something like this :) I only did it for show, I swear!


Apr 7, 2009 at 12:08 PM // reply »
25 Comments

Oh, I know. But people aren't very discriminating when they see stuff on line. The tongue-in-cheek just doesn't come across. Do you remember that URL "shortening" service I made? The first one, I mean, where it was deliberately coded to ensure you only got a shorter URL if your starting URL was like 500 characters long? My faith in people dropped significantly after the number of "sweet new service, I added it to my list" and "can I deploy that on my own site?" emails I got.

I guess my point is "be careful." You know it's silly. I know it's silly. Rick knows it's silly. The half-dead cat knows it's silly. But a lot of people will say "Ben blogged about it, it must be good," with no further thought.


Apr 7, 2009 at 12:49 PM // reply »
2 Comments

Ben,

This is AWESOME! I just used this to help refactor my URL shortening application based on Barney's code! But for some reason it doesn't work on Railo any ideas?

Dan

P.S. Kidding! ;-) Barney lives close enough to me that it wouldn't take him more than a tank of gas to show up at my door to rip this code out of my hands and leave me senseless. Really funny but sad observation Barney.


Apr 7, 2009 at 1:10 PM // reply »
10,640 Comments

@Barney,

I didn't even think of that. Good point. I have put a note at the top of the blog post trying to emphasize that this is bad code!


Apr 7, 2009 at 2:18 PM // reply »
66 Comments

@Ben Wow! That is some clever code! I'll be using this on all my applications. This guy I worked with threatened to defenestrate himself if I used it, but I explained, "Hey, pal: I saw it ONLINE!"

I'll bet @Barney is sick with jealousy right now...

;-)


Apr 7, 2009 at 2:41 PM // reply »
33 Comments

hmmm....

it depends.


Apr 7, 2009 at 3:01 PM // reply »
20 Comments

CF seems to have screwed up the expectations of what should happen with implicit structs. I tried testing what would happen if I insert implicit structs into an array with an incrementor, but that seems to yield some strange results as well. Didn't test with implicit arrays, but I'm assuming the results would be the same.

http://fojasaur.us/weblog/post.cfm/coldfusion-implicit-structs-are-confusing

Seems as though
arr[i++] = i increments i but arr[i++] = {test=i } does not.


Apr 17, 2009 at 1:49 PM // reply »
25 Comments

Wow alan, it's like you're psychic or something. How ever did you know?

Akismet, Ben?


Apr 17, 2009 at 1:53 PM // reply »
10,640 Comments

@Barney,

Ha ha ha ha, wow! Taken care of.



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »