Skip to content

Commit

Permalink
Expose code point limit in yml_decode
Browse files Browse the repository at this point in the history
- Fix error in core when exceeding the default code point limit.
- Expose `yml_decode` code point limit through a runtime setting.
  • Loading branch information
Pieter12345 committed Aug 9, 2023
1 parent 0cbc9e0 commit c5a1823
Showing 1 changed file with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.laytonsmith.annotations.api;
import com.laytonsmith.annotations.core;
import com.laytonsmith.annotations.noboilerplate;
import com.laytonsmith.annotations.seealso;
import com.laytonsmith.core.ArgumentValidation;
import com.laytonsmith.core.MSVersion;
import com.laytonsmith.core.Static;
Expand All @@ -14,9 +15,13 @@
import com.laytonsmith.core.constructs.Construct;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.environments.Environment;
import com.laytonsmith.core.environments.GlobalEnv;
import com.laytonsmith.core.exceptions.CRE.CRECastException;
import com.laytonsmith.core.exceptions.CRE.CREFormatException;
import com.laytonsmith.core.exceptions.CRE.CREPluginInternalException;
import com.laytonsmith.core.exceptions.CRE.CRERangeException;
import com.laytonsmith.core.exceptions.CRE.CREThrowable;
import com.laytonsmith.core.functions.Meta.set_runtime_setting;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
import com.laytonsmith.core.exceptions.MarshalException;
Expand All @@ -31,7 +36,9 @@
import javax.xml.xpath.XPathExpressionException;
import org.xml.sax.SAXException;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.error.YAMLException;
import org.yaml.snakeyaml.parser.ParserException;
import org.yaml.snakeyaml.scanner.ScannerException;

Expand Down Expand Up @@ -222,11 +229,12 @@ public MSVersion since() {
}

@api
@seealso({set_runtime_setting.class})
public static class yml_decode extends AbstractFunction {

@Override
public Class<? extends CREThrowable>[] thrown() {
return new Class[]{CREFormatException.class};
return new Class[]{CREFormatException.class, CREPluginInternalException.class};
}

@Override
Expand All @@ -240,15 +248,27 @@ public Boolean runAsync() {
}

@Override
public Mixed exec(Target t, Environment environment, Mixed... args) throws ConfigRuntimeException {
public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntimeException {
String data = args[0].val();
Yaml yaml = new Yaml();
LoaderOptions options = new LoaderOptions();
Mixed codePointLimit = env.getEnv(GlobalEnv.class)
.GetRuntimeSettingOrCNull("function.yml_decode.code_point_limit", CNull.NULL);
if(!CNull.NULL.equals(codePointLimit)) {
try {
options.setCodePointLimit(ArgumentValidation.getInt32(codePointLimit, t));
} catch (CRERangeException | CRECastException e) {
// Ignore invalid value.
}
}
Yaml yaml = new Yaml(options);
Object ret = null;
Exception cause = null;
try {
ret = yaml.load(data);
} catch (ScannerException | ParserException ex) {
cause = ex;
} catch (YAMLException ex) {
throw new CREPluginInternalException(ex.getMessage(), t);
}
if(!(ret instanceof Map) && !(ret instanceof Collection)) {
throw new CREFormatException("Improperly formatted YML"
Expand All @@ -271,7 +291,10 @@ public Integer[] numArgs() {
public String docs() {
return "array {string} Takes a YML encoded string, and returns an associative array,"
+ " depending on the contents of the YML string. If the YML string is improperly formatted,"
+ " a FormatException is thrown.";
+ " a FormatException is thrown."
+ " If an exception occurs in the YML parsing library, a PluginInternalException is thrown."
+ " By default YML strings up to 3 * 1024 * 1024 (3 MB) code points can be parsed. This limit can"
+ " be exceeded through the 'function.yml_decode.code_point_limit' runtime setting.";
}

@Override
Expand Down

0 comments on commit c5a1823

Please sign in to comment.