The Occasional Danger Of ColdFusion's Automatic Data Type Conversion

Posted December 2, 2008 at 10:26 AM

Tags: ColdFusion

ColdFusion does a lot to help you out as a programmer. In that vein, ColdFusion will often convert data types for you as it sees it necessary. Unfortunately, that can come back to bite you if you are not careful. This morning, I spent a solid 45 minutes trying to debug an XML error because it didn't occur to me that I was getting a little extra help. To demonstrate, here is an example of the problem I was running into:

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

  • <!--- Create an XML document. --->
  • <cfxml variable="xmlData">
  •  
  • <data>
  • <node>Ben Nadel - Kinky Solutions</node>
  • </data>
  •  
  • </cfxml>
  •  
  •  
  • <!--- Check equality of DUPLICATE nodes. --->
  • <cfif (xmlData.data.node EQ Duplicate( xmlData.data.node ))>
  •  
  • Equal!
  •  
  • <cfelse>
  •  
  • Not Equal!
  •  
  • </cfif>

As you can see here, I am duplicating a given XML node and then comparing it back to the original. Now, if you program in other languages like Java or Javascript, because an XML node is a complex object, you would assume that this would output "Not Equal!" since we are comparing an object to a clone of itself. However, when I run the above code, I get the following output:

Equal!

What I forgot was that when performing equality tests in ColdFusion, ColdFusion tries to convert the objects in question into simple values and then compare the rendered simple values. So, when comparing two XML nodes, the pseudo code for this is actually:

ToString( XmlNodeA ) EQ ToString( XmlNodeB )

When we keep this in mind and think again about comparing an XML node to the Duplicate() of itself, we realize that of course it will always be equal since the string equivalent of the two nodes will always be the same.

This type of automatic data type conversion can be awesome; but, if you forget that it is happening, it can come right around and bit you in the butt! Now, I am not arguing against this - I love that ColdFusion will do this for us; I am just saying that it is important to keep this behind-the-scenes functionality in mind.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




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

Reader Comments

Dec 2, 2008 at 4:41 PM // reply »
1 Comments

Oh yes, I've had this happen to me too. Here's one I encountered:

I had an application where I wanted to display a price value. If the price value had no cents, but the user typed in an ending ".00", I wanted to remove this, and just display the whole dollar amount (e.g. $25 instead of $25.00). So being the smart programmer, I just looked for a ".00" as the last three characters and dropped them if it existed. I tested it, and if worked just as desired.

The first day in production, someone calls and says they keep entering $6,000 but it shows up on the web as "$6,". What!! I test this myself, and yep, the last three zeros are getting dropped. I scour the code, write a little test, and CF keeps thinking ".00" is equal to "000". Of course any human can see these strings are not equal.

They may look like unequal strings to me, but to CF they're the same. Why? Well, CF looks at the first ".00" and decides that this can be interpreted as a numeric ZERO, so that's how it treats it, as numeric. Then the "000" can be interpreted as a numeric ZERO too, so ZERO equals ZERO and those last three zeros are going to be dropped from the value and "$6," is going to be displayed.

The solution is to use the compare() function, which does a string comparison.


Dec 2, 2008 at 4:46 PM // reply »
6,516 Comments

@Steve,

Yeah, "zero" can be tricky cause it can also convert it into a numeric date/time. When it does things like that, I think you can get odd things like ".00" equaling "0AM".


ike
Dec 3, 2008 at 11:22 PM // reply »
78 Comments

Compare is one way to handle that... I tend to prefer int() if not lsnumberformat() or numberformat(). But for that matter, even rereplace(string,"\.00$","") should work fine.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »