Skip to content

Commit

Permalink
Fix potentially using wrong env to get procs when threaded
Browse files Browse the repository at this point in the history
Keeping a reference to the "currentEnv" in Script is problematic when using threads and sharing the same Script object. In addition to procedure resolution in eval(), this could have potentially affected aliases that have threading and label permission enforcement.
  • Loading branch information
PseudoKnight committed Aug 31, 2023
1 parent 62587c3 commit 9cd1bca
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
25 changes: 7 additions & 18 deletions src/main/java/com/laytonsmith/core/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public class Script {
boolean compilerError = false;
private final long compileTime;
private String label;
private Environment currentEnv;
private Set<Class<? extends Environment.EnvironmentImpl>> envs;
private FileOptions fileOptions;
private SmartComment smartComment;
Expand All @@ -102,14 +101,6 @@ public String toString() {
return b.toString();
}

private Procedure getProc(String name) {
return currentEnv.getEnv(GlobalEnv.class).GetProcs().get(name);
}

public Environment getCurrentEnv() {
return currentEnv;
}

public String getLabel() {
return label;
}
Expand Down Expand Up @@ -215,9 +206,8 @@ public boolean uncompilable() {

public void run(final List<Variable> vars, Environment myEnv, final MethodScriptComplete done) {
//Some things, such as the label are determined at compile time
this.currentEnv = myEnv;
this.currentEnv.getEnv(GlobalEnv.class).SetLabel(this.label);
this.currentEnv.getEnv(GlobalEnv.class).SetAliasComment(this.smartComment);
myEnv.getEnv(GlobalEnv.class).SetLabel(this.label);
myEnv.getEnv(GlobalEnv.class).SetAliasComment(this.smartComment);
MCCommandSender p = myEnv.getEnv(CommandHelperEnvironment.class).GetCommandSender();
if(!hasBeenCompiled || compilerError) {
Target target = Target.UNKNOWN;
Expand All @@ -231,7 +221,7 @@ public void run(final List<Variable> vars, Environment myEnv, final MethodScript
throw ConfigRuntimeException.CreateUncatchableException("Unable to run command, script not yet compiled,"
+ " or a compiler error occurred for that command. To see the compile error, run /reloadaliases", target);
}
enforceLabelPermissions();
enforceLabelPermissions(myEnv);

try {
for(ParseTree rootNode : cright) {
Expand All @@ -249,8 +239,8 @@ public void run(final List<Variable> vars, Environment myEnv, final MethodScript
}
}

currentEnv.getEnv(StaticRuntimeEnv.class).getIncludeCache().executeAutoIncludes(currentEnv, this);
MethodScriptCompiler.execute(rootNode, currentEnv, done, this);
myEnv.getEnv(StaticRuntimeEnv.class).getIncludeCache().executeAutoIncludes(myEnv, this);
MethodScriptCompiler.execute(rootNode, myEnv, done, this);
}
} catch (ConfigRuntimeException ex) {
//We don't know how to handle this really, so let's pass it up the chain.
Expand Down Expand Up @@ -322,7 +312,6 @@ public Mixed eval(ParseTree c, final Environment env) throws CancelCommandExcept
}

final Mixed m = c.getData();
currentEnv = env;
if(m instanceof Construct co) {
if(co.getCType() != ConstructType.FUNCTION) {
if(co.getCType() == ConstructType.VARIABLE) {
Expand Down Expand Up @@ -354,7 +343,7 @@ public Mixed eval(ParseTree c, final Environment env) throws CancelCommandExcept

if(possibleFunction.hasProcedure()) {
//Not really a function, so we can't put it in Function.
Procedure p = getProc(m.val());
Procedure p = globalEnv.GetProcs().get(m.val());
if(p == null) {
throw new CREInvalidProcedureException("Unknown procedure \"" + m.val() + "\"", m.getTarget());
}
Expand Down Expand Up @@ -1028,7 +1017,7 @@ public void checkAmbiguous(List<Script> scripts) throws ConfigCompileException {
}
}

public void enforceLabelPermissions() {
public void enforceLabelPermissions(Environment currentEnv) {
String label = currentEnv.getEnv(GlobalEnv.class).GetLabel();
if(label == null || label.equals(Static.GLOBAL_PERMISSION)) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/laytonsmith/core/functions/Meta.java
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ public Mixed execs(Target t, Environment environment, Script parent, ParseTree..
environment.getEnv(GlobalEnv.class).SetLabel(parent.getLabel());
}
environment.getEnv(CommandHelperEnvironment.class).SetCommandSender(sender);
parent.enforceLabelPermissions();
parent.enforceLabelPermissions(environment);
ParseTree tree = nodes[1 + offset];
parent.eval(tree, environment);
environment.getEnv(CommandHelperEnvironment.class).SetCommandSender(originalSender);
Expand Down

0 comments on commit 9cd1bca

Please sign in to comment.