tracker issue : CF-4203297

select a category, or use search below
(searches all categories and all time range)

Inconsistant variable access in nested closures

| View in Tracker

Status/Resolution/Reason: To Fix/Fixed/BugVerified

Reporter/Name(from Bugbase): David Valeo / ()

Created: 08/27/2018

Components: Functions

Versions: 2016,11.0,2018

Failure Type: Data Corruption

Found In Build/Fixed In Build: tested and failing on CF11, CF2016 and CF2018 / 2018,0,0,312276

Priority/Frequency: Normal / All users will encounter

Locale/System: ALL / Windows 10 64 bit

Vote Count: 1

Problem Description: When accessing variables in nested closures coldfusion reads from and writes to said variables in an inconsistent manner. 

Steps to Reproduce:
component { 
      function test( event, rc, prc ){
            var testvar = 'a';
            var dostuff = function(){
                  testvar = 'b';
                  var domorestuff = function(){
                        testvar = 'c';
                        writedump( testvar ); // b? so when setting the variable we lost our reference to the outer testvar variable but somehow we still have access to the old one for reading, 
                        writedump( variables.testvar ); // if the above is true and CF lost its scope for setting then it created a new testvar variable in the variables scope and it should be set to 'c' which it is.
                        testvar &= 'c'; //which one is going to be updated here?
                        //this should result in one of 2 outcomes
                        //1. mysteryscope.testvar = 'b' and variables.testvar = 'cc'
                        //2. mysteryscope.testvar = 'bc' and variables.testvar = 'c'
                        writedump( testvar ); // still b, so variables.testvar must be 'cc' right?
                        writedump( variables.testvar ); // nope.. we get a mixed result, it read mysteryscope.testvar and appended 'c' but set variables.testvar. 

Actual Result: It seems like CF is using a different order when traversing scopes for reading and writing variables in nested closures. As a result a new variable is created in the calling components variables scope (which also seems a unexpected) but is not accessed properly when reading the variable back out.

Expected Result: CF should traverse scopes in the same order when getting and setting variables in nested closures.

Any Workarounds: Unknown



Relates to which I raised 2 years ago. Please fix this unexpected behaviour.
Comment by John W.
29806 | October 19, 2018 03:51:18 PM GMT
In above, unscoped testvar='c' (and testvar &= 'c') writes to variables scope (bad). In below, unscoped testvar='c' writes to local scope (bad). <cfscript> function f() { var testvar='c' local.delete('c') local.clear() testvar='c' writeDump(local.testvar)//Actual result: c; Expected result: coldfusion.runtime.UndefinedElementException } f() </cfscript>
Comment by Aaron N.
29810 | October 20, 2018 04:59:32 AM GMT
And Adobe also recommends scoping all variables for performance reasons ( But that recommendation is broken, b/c some variables cannot be scoped, until CF-4203280 is fixed. Thanks!, -Aaron
Comment by Aaron N.
29811 | October 20, 2018 05:14:15 AM GMT