Major Flaw in CF9 - May Break Code! and he is right it will break code.

"> ColdFusion and another Major flaw with local scope

ColdFusion and another Major flaw with local scope

I was reading a blog today on another major flaw in ColdFusion 9, and I thought I would investigate this one a little closer as it didn't seem to make sense.

Sami Hoda posted Major Flaw in CF9 - May Break Code! and he is right it will break code.

So how did this bug never get found.

Anyway lets have a closer look at the problem. The problem is actually in the component which is this.

<cfcomponent>
   <cffunction access="public" name="doSomething" returntype="any" output="false" >
      <cfset var local = getProperties() />
      <cfset local.strVersion = SERVER.ColdFusion.ProductVersion />
      <cfset local.strLevel = SERVER.ColdFusion.ProductLevel />
      <cfreturn local >
   </cffunction>
   <cffunction name="getProperties" returntype="struct" access="private" output="false">
      <cfset var local = structNew() />
      <cfset local.firstName = "John" />
      <cfset local.lastName = "Doe" />
      <cfreturn local >
   </cffunction>
</cfcomponent>

So lets have a closer look at the very first line.

<cfset var local = getProperties() />

This looks like code that we all would write, and although I personally use a property on the scope myself this is still very legal code.

My first port of call was to run the debugger over that line of code, and discovered that if you write the code as it is, the method getProperties() is never called.

That's right, it is never called.

So no big deal I thought I would refactor the code to look like this.

<cfset var local = ''/>
      <cfset local = getProperties() />

And ran the line debugger over it again, this time the method was called and the return struct was indeed returned and assigned correctly, or so we think.

But the rest of the code, makes it break even more. Lets explain that some more.

When the method getProperties is return, it seems to return the original as a private scope. So when the next 2 sets are called in the original above code, it is defined in a separate scope again.

So that the last return only returns what has been set in that function.

This is extremely weird behaviour.



  • Justin Carter's Gravatar It's not really a bug, it's a known backwards compatibility issue for a feature that Adobe knowingly introduced and which the comunity wanted.

    It's simply because "local" is now a scope inside a CFC function and you can't assign a struct to a scope or call StructNew() on a scope, just like in previous versions of CF. See Sami's blog for the workarounds.
    # Posted By Justin Carter | 10/31/09 2:13 AM
  • Andrew Scott's Gravatar So you are saying that

    <cfset var local = {username='andrews', password='password'} />

    is being set to the local scope?

    Do you want to think about that one, and if what you say is correct why does this work in ColdFusion 9?
    # Posted By Andrew Scott | 10/31/09 6:37 AM
  • Sami Hoda's Gravatar Thanks for spreading the word.
    # Posted By Sami Hoda | 11/11/09 5:19 PM
  • Andrew Scott's Gravatar No problem, I see that it is ear marked for being fixed in the next update.
    # Posted By Andrew Scott | 11/12/09 4:56 AM