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

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


Apr 7, 2009 at 10:30 AM // reply »
11,247 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,247 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,247 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,247 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
May 24, 2013 at 5:39 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam Oops! My mistake! I hadn't gotten that far in my testing - I'm still baby stepping my way through the process. ... read »
May 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools