Ability To Leverage ColdFusion Bug Will Mean Errors Later

Posted April 7, 2009 at 9:36 AM

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:

 Launch code in new window » Download code as text file »

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

... gets compiled into to:

 Launch code in new window » Download code as text file »

  • <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:

 Launch code in new window » Download code as text file »

  • <!--- 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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

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

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


Apr 7, 2009 at 10:30 AM // reply »
7,212 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 »
18 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 »
7,212 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 »
7,212 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 »
56 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 »
32 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 »
7,212 Comments

@Barney,

Ha ha ha ha, wow! Taken care of.


Post Comment  |  Ask Ben

Recent Blog Comments
Feb 9, 2010 at 8:09 AM
Creating A "Remember Me" Login System In ColdFusion
@Nikos, Heck yeah! Glad you got things working smoothly. ... read »
Feb 9, 2010 at 8:05 AM
Creating A "Remember Me" Login System In ColdFusion
No probs :) anyway , Im good now :) ... read »
Feb 9, 2010 at 8:02 AM
Creating A "Remember Me" Login System In ColdFusion
@Nikos, I've seen people use the J2EE sessions, but I have not used them myself... yet. ... read »
Feb 9, 2010 at 7:57 AM
Ask Ben: Converting a Query to an Array
@Stju, Did you actually test this? I ask because there is a fatal flaw in it - you are using the same Row struct for every row. Since Structs are passed by reference, every subsequent update you ma ... read »
Feb 9, 2010 at 7:50 AM
Using jQuery's SlideUp() and SlideDown() Methods With Bottom-Positioned Elements
@Thomas, Not bad. I suppose you could do the same with Top as well as margin. ... read »
Feb 9, 2010 at 7:47 AM
Ask Ben: Creating A PDF And Attaching It To An Email Using ColdFusion
@Johan, I don't think I have one off hand. Basically, you'd just want to use the File attribute of CFDocument to save the PDF to disk. Then, you'd want to use the File attribute of CFMailParam to a ... read »
Feb 9, 2010 at 5:34 AM
Creating A "Remember Me" Login System In ColdFusion
Any change you could show how to take advantage how the J2ee session stuff in your code? http://kb2.adobe.com/cps/182/tn_18232.html ... read »
Feb 9, 2010 at 5:32 AM
Creating A "Remember Me" Login System In ColdFusion
This may help: http://bugs.farcrycms.org/browse/FC-79 Its the session variables' option being selected in the CF Admin ... read »