Skip to content

Commit d106e8b

Browse files
authored
Fix: use 'light' match-ing (not depending on scope) (elastic#11656)
back-port of elastic#11653
1 parent f1501b4 commit d106e8b

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

logstash-core/src/main/java/org/logstash/config/ir/compiler/EventCondition.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.Map;
66
import java.util.Objects;
77
import java.util.function.Predicate;
8+
import org.jruby.Ruby;
9+
import org.jruby.RubyRegexp;
810
import org.jruby.RubyString;
911
import org.jruby.runtime.builtin.IRubyObject;
1012
import org.jruby.util.ByteList;
@@ -466,34 +468,40 @@ private static boolean valueIsTruthy(Object object) {
466468
!Boolean.toString(false).equals(other);
467469
}
468470

471+
private static RubyRegexp newRegexp(String pattern) {
472+
final Ruby runtime = RubyUtil.RUBY;
473+
return RubyRegexp.newRegexpFromStr(runtime, runtime.newString(pattern), 0);
474+
}
475+
476+
private static boolean matches(RubyString str, RubyRegexp regexp) {
477+
return regexp.match_p(RubyUtil.RUBY.getCurrentContext(), str).isTrue(); // match? returns true/false
478+
}
479+
469480
private static final class FieldMatches implements EventCondition {
470481

471482
private final FieldReference field;
472483

473-
private final RubyString regex;
484+
private final RubyRegexp regexp;
474485

475-
private FieldMatches(final String field, final String regex) {
486+
private FieldMatches(final String field, final String pattern) {
476487
this.field = FieldReference.from(field);
477-
this.regex = RubyUtil.RUBY.newString(regex);
488+
this.regexp = newRegexp(pattern);
478489
}
479490

480491
@Override
481492
public boolean fulfilled(final JrubyEventExtLibrary.RubyEvent event) {
482-
final Object tomatch = event.getEvent().getUnconvertedField(field);
483-
return tomatch instanceof RubyString &&
484-
!((RubyString) tomatch).match(WorkerLoop.THREAD_CONTEXT.get(), regex).isNil();
493+
final Object toMatch = event.getEvent().getUnconvertedField(field);
494+
return toMatch instanceof RubyString && matches((RubyString) toMatch, regexp);
485495
}
486496
}
487497

488498
private static final class ConstantMatches implements EventCondition {
489499

490500
private final boolean matches;
491501

492-
private ConstantMatches(final Object constant, final String regex) {
502+
private ConstantMatches(final Object constant, final String pattern) {
493503
this.matches = constant instanceof String &&
494-
!(RubyUtil.RUBY.newString((String) constant).match(
495-
WorkerLoop.THREAD_CONTEXT.get(),
496-
RubyUtil.RUBY.newString(regex)).isNil());
504+
matches(RubyUtil.RUBY.newString((String) constant), newRegexp(pattern));
497505
}
498506

499507
@Override

0 commit comments

Comments
 (0)