Move regex error to node (#45813)
This commit is contained in:
parent
33d4801213
commit
a1b88ca009
|
@ -817,11 +817,6 @@ public final class Walker extends PainlessParserBaseVisitor<ANode> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ANode visitRegex(RegexContext ctx) {
|
public ANode visitRegex(RegexContext ctx) {
|
||||||
if (false == settings.areRegexesEnabled()) {
|
|
||||||
throw location(ctx).createError(new IllegalStateException("Regexes are disabled. Set [script.painless.regex.enabled] to [true] "
|
|
||||||
+ "in elasticsearch.yaml to allow them. Be careful though, regexes break out of Painless's protection against deep "
|
|
||||||
+ "recursion and long loops."));
|
|
||||||
}
|
|
||||||
String text = ctx.REGEX().getText();
|
String text = ctx.REGEX().getText();
|
||||||
int lastSlash = text.lastIndexOf('/');
|
int lastSlash = text.lastIndexOf('/');
|
||||||
String pattern = text.substring(1, lastSlash);
|
String pattern = text.substring(1, lastSlash);
|
||||||
|
|
|
@ -40,6 +40,8 @@ public final class ERegex extends AExpression {
|
||||||
private final int flags;
|
private final int flags;
|
||||||
private Constant constant;
|
private Constant constant;
|
||||||
|
|
||||||
|
private CompilerSettings settings;
|
||||||
|
|
||||||
public ERegex(Location location, String pattern, String flagsString) {
|
public ERegex(Location location, String pattern, String flagsString) {
|
||||||
super(location);
|
super(location);
|
||||||
|
|
||||||
|
@ -56,7 +58,7 @@ public final class ERegex extends AExpression {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void storeSettings(CompilerSettings settings) {
|
void storeSettings(CompilerSettings settings) {
|
||||||
// do nothing
|
this.settings = settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -66,6 +68,12 @@ public final class ERegex extends AExpression {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void analyze(Locals locals) {
|
void analyze(Locals locals) {
|
||||||
|
if (false == settings.areRegexesEnabled()) {
|
||||||
|
throw createError(new IllegalStateException("Regexes are disabled. Set [script.painless.regex.enabled] to [true] "
|
||||||
|
+ "in elasticsearch.yaml to allow them. Be careful though, regexes break out of Painless's protection against deep "
|
||||||
|
+ "recursion and long loops."));
|
||||||
|
}
|
||||||
|
|
||||||
if (!read) {
|
if (!read) {
|
||||||
throw createError(new IllegalArgumentException("Regex constant may only be read [" + pattern + "]."));
|
throw createError(new IllegalArgumentException("Regex constant may only be read [" + pattern + "]."));
|
||||||
}
|
}
|
||||||
|
|
|
@ -262,7 +262,7 @@ public class WhenThingsGoWrongTests extends ScriptTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testRegexDisabledByDefault() {
|
public void testRegexDisabledByDefault() {
|
||||||
IllegalStateException e = expectThrows(IllegalStateException.class, () -> exec("return 'foo' ==~ /foo/"));
|
IllegalStateException e = expectScriptThrows(IllegalStateException.class, () -> exec("return 'foo' ==~ /foo/"));
|
||||||
assertEquals("Regexes are disabled. Set [script.painless.regex.enabled] to [true] in elasticsearch.yaml to allow them. "
|
assertEquals("Regexes are disabled. Set [script.painless.regex.enabled] to [true] in elasticsearch.yaml to allow them. "
|
||||||
+ "Be careful though, regexes break out of Painless's protection against deep recursion and long loops.", e.getMessage());
|
+ "Be careful though, regexes break out of Painless's protection against deep recursion and long loops.", e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue