Where in the world is the "variables" scope?

Hi, i’ve been a long time CFML developer and I have to admit, i’ve never set a variable as <cfset variables.thing = 1>. I always use <cfset thing = 1> syntax.

Since isDefined() is deemed insecure, so if a variable is not a struct, how do we detect it with structKeyExists() ?

Everywhere I read it says to use the “variables” scope. so structKeyExists(variables, “thing”) should detect it. But it doesn’t.

According to this: Variables Scope CFML Documentation

“The VARIABLES scope is the default scope in ColdFusion. i.e. if a variable is declared in .cfm and .cfc file without explicitly prefixing a scope, or without ‘var’ to a variable inside a function, ColdFusion assigns VARIABLES scope to that variable.”

But then, when testing it out (and also my experience in my own codebase) I get the following situation:

Can someone explain to me where I am going wrong? And how can I use anything other than isDefined() to detect if a variable is defined when scope is not specified?

Perhaps its a setting in Lucee admin i’m not aware of?

Or, is it possible that the docs are wrong? They are most certainly misleading!!!

Strange. I can’t replicate that on my local installations of Lucee 5.4.6.9 and 6.1.0.243 via Tomcat or Commandbox.

<cfscript>
test = 1;
dump( StructKeyExists( variables, "test" ) ); //true
</cfscript>

But on trycf.com I’m getting false. Can’t think of an admin setting that would affect this.

Thanks at least I know i’m not losing my mind here.

I am hoping somebody who is a Lucee Contributer maybe can chime in to bring in some understanding. The Docs are so explicit with how it should work. Its shocking to have it just straight up fail to do what it says it is going to do.

Somebody help!!! I actually have a coding situation that is breaking as a result of this. Its becoming really annoying to have to refactor a bunch of stuff because Lucee is not working as expected with regards to this.

Please could you share details of your stack (i.e. Lucee/Java versions, OS etc - the more detail the better)?

I’ve read once somewhere that Trycf might do some unknown/hidden scoping stuff. I think @AdamCameron wrote something about it in slack somewhere after experimenting, but I can’t say for sure. May be he knows more about it.

You’re right Andreas. Brad offers an explanation here: What scope is this? - #6 by bdw429s

1 Like

@stp1 What is your specific environment?

What result do you get from !isNull(myVar)?

All of the following tests are working for me using Lucee 5.4.5.23 in Linux with Apache 2.4, Java 11, and Tomcat 9:

myVar = 1;
dump(var=StructKeyExists(Variables, "myVar"), label='StructKeyExists(Variables, "myVar")');
dump(var=Variables.keyExists("myVar"), label='Variables.keyExists(myVar)');
dump(var=!isNull(myVar), label='!isNull(myVar)');
dump(var=Variables, label='Variables');

TryCF does indeed seem to be the problem. It produces different results for your code, depending on the CFML engine used:
image

I’m on Lucee 5.3.12 on Windows. So according to the table BK_BK posted, i should not expect it to work in any version of Lucee 5 but I can expect it to work in Lucee 6?

5.3.12 on Windows. I wonder if there is a difference between linux and windows versions with respect to this. According the replies to my post i’m seeing, there is.

Reminder: The issue is not actually about Lucee. It is about TryCF.

No with all due respect, the reason I made this post was because my production and development evnironments running Lucee 5.3.12 on Windows were having this issue. And then I tried on TryCF and I got the same result. So I wrote the post to try and figure out. Is it a setting? Some other environmental factor ?

I need to know what exactly is affecting this.

So, I quickly spun off the Lucee Version lucee-5.3.2.77 and the code below works fine:

<cfscript>
test = 1;
dump([server.lucee.version,StructKeyExists( variables, "test" )] ); //true
</cfscript>

However, if I dump(variables) on that version I get a NULL Pointer Exception with my Java 11.0.23, so there might be some issues regarding the variables scope on 5.3.2.77.

<cfscript>
test = 1;
dump([server.lucee.version,StructKeyExists( variables, "test" )] ); //true
dump(variables);// throws null pointer exception on 5.3.2.77
</cfscript>

But on actual Lucee 6.1.0.243 it works fine:
image

What works on scopes and what not on trycf.com is another story and I’d ignore that and make decisions on my own versions. As already told, trycf does some scoping changes on the code you submit.

If that scoping issue would be in such a way you’ve described, we would have a lots of requests here in the forum. And I don’t know about any config settings dealing with changing that scope.

Also, I’d recommend not using 5.3.2.77 at all anymore because of the known CVEs. I’d focus on moving to the 5.4 LTS version or maybe even 6.x.

2 Likes