Title:
Bug 79601:(Watson Migration Closure)Local variables declared within a function do not behave as expected if they are assigned a null value and a variable of the same name exists in a different scope where it has a value
| View in TrackerStatus/Resolution/Reason: Closed/Won't Fix/LowImpact
Reporter/Name(from Bugbase): Ryan Cogswell / Ryan Cogswell (RyanC)
Created: 08/27/2009
Components: Language, Functions
Versions: 9.0
Failure Type: Unspecified
Found In Build/Fixed In Build: 9,0,0,241018 /
Priority/Frequency: Normal / Unknown
Locale/System: English / Win All
Vote Count: 1
Problem:
Local variables declared within a function do not behave as expected if they are assigned a null value and a variable of the same name exists in a different scope where it has a value. I would expect the local variable to always be the one found since the whole purpose of declaring it as a local variable is to limit the scope of the variable being used. The behavior in this scenario of the IsDefined and IsNull methods and of trying to output the value of the variable is not the behavior I would expect or want. This issue also exists in CF 8, though slightly different now in CF 9 with the introduction of the local scope and IsNull method.
Method:
[vmannebo 12/4/2010]
\\blrfs04.macromedia.com\builds\coldfusion\bugs\79601
Couple of issues here:
1) First customers's use of isNUll is wrong. The syntax he is using is isNull("x") instead of isNull(x)
2) scope lookup and behavior is different for isNull, isDefined() and when used in cfoutput
3) When you run the cfm, there are 3 cases.
i) case 1 looks all good. IN
ii) In case 2, isdefined and isNUll search only till local whereas in the cfoutput below, variables scope is also searched for the variable
iii) In case 3, it looks like isdefined is looking in variables scope too where as isNull returns "YES" when used in cfoutput but "NO" when used in the cfif condition
Paste this into a CF page and execute in order to see the behavior.<cffunction name="testNullLocalVarBehavior" output="true"><cfset var map = CreateObject("java", "java.util.HashMap").init()><cfset var myTestVar = map.get("missingkey")>For variables.local.myTestVar:<br>IsDefined returns #IsDefined("variables.local.myTestVar")#<br>IsNull returns #IsNull("variables.local.myTestVar")#<br>Trying to output the value produces<cftry>#variables.local.myTestVar#<cfif IsNull("variables.local.myTestVar")>which seems inconsistent with IsNull returning #IsNull("variables.local.myTestVar")#</cfif><cfcatch>an error.</cfcatch></cftry><br><br>For local.myTestVar:<br>IsDefined returns #IsDefined("local.myTestVar")#<br>IsNull returns #IsNull("local.myTestVar")#<br>Trying to output the value produces<cftry>#local.myTestVar#<cfif IsNull("local.myTestVar")>which seems inconsistent with IsNull returning #IsNull("local.myTestVar")#</cfif><cfcatch>an error.</cfcatch></cftry><br><br>For myTestVar:<br>IsDefined returns #IsDefined("myTestVar")#<br>IsNull returns #IsNull("myTestVar")#<br>Trying to output the value produces<cftry>#myTestVar#<cfif IsNull("myTestVar")>which seems inconsistent with IsNull returning #IsNull("myTestVar")#</cfif><cfcatch>an error.</cfcatch></cftry></cffunction><cfset local.myTestVar = "<br><u>This local.myTestVar should only be reachable when inside testNullLocalVarBehavior by using variables.local.myTestVar</u><br>"><cfset myTestVar = "<br><u>This myTestVar should only be reachable when inside testNullLocalVarBehavior by using variables.myTestVar</u><br>"><cfset testNullLocalVarBehavior()>
Result:
----------------------------- Additional Watson Details -----------------------------
Watson Bug ID: 3039788
External Customer Info:
External Company:
External Customer Name: Ryan Cogswell
External Customer Email: 40DB35B34471F8BF9920157F
External Test Config: 08/27/2009
Attachments:
Comments: