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 »
68 Comments

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


Apr 7, 2009 at 10:30 AM // reply »
11,314 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 »
28 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 »
11,314 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 »
11,314 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 »
34 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 »
11,314 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
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 19, 2013 at 11:33 AM
Filter vs. ngHide With ngRepeat In AngularJS
In your assessment, is it correct to say that given a list of say 500 items its more performant to use the `ngHide` method over the `filter` method? ... read »
Jun 19, 2013 at 10:18 AM
ColdFusion Path Usage And Manipulation Overview
Anyone happen to know if the file created by getTempFile will be automatically removed at any point? Nothing mentioned in the docs, and restarting CF doesn't remove them, so it seems it needs manu ... read »
Jun 19, 2013 at 9:41 AM
Working With Inherited Collections In AngularJS
I actually just ran into this same situation with a demo I was putting together. Your implementation of multi-lvl $scope's > Mine :) ... read »
Jun 19, 2013 at 8:17 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Prateek, to match a word or text you should use .toContain('word') that's a jasmine reference. website is : http://pivotal.github.io/jasmine/ ... read »
Jun 19, 2013 at 8:10 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Hi Guys, Actually i am doing e2e test of angular js of my project but i am not getting one thing that is how to press enter key through the test when my form is filled as i am not using a button but ... read »
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
Jun 18, 2013 at 3:39 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
Hi Ben, THANKS! While not bleeding edge, it is new to me & I like learning new things every day! ... read »
Jun 18, 2013 at 12:30 PM
Disabling Auto-Correct And Auto-Capitalize Features On iPhone Inputs
Also spellcheck="false" should be mentioned as part of html5 specs ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools