Check regex for validity before querying
Change-Id: I03bbefcecc2b51f8ca611a9453fd253dd972718e
diff --git a/Changes b/Changes
index fbede7d..dfd1dc6 100644
--- a/Changes
+++ b/Changes
@@ -1,4 +1,4 @@
-0.59.0 2019-11-27
+0.59.0 2019-11-28
- [bugfix] Fix offset retrieval in concurrent searches
(diewald)
- [cleanup] Removed deprecated numberOf() method from index
@@ -9,6 +9,8 @@
& libraries (margaretha)
- [feature] Support for vector representation of terms in KoralQuery
(diewald)
+ - [bugfix] Check regex for validity before querying
+ (diewald)
0.58.7 2019-09-16
- [bugfix] Fix the behaviour of negative operands in virtual
diff --git a/src/main/java/de/ids_mannheim/korap/KrillQuery.java b/src/main/java/de/ids_mannheim/korap/KrillQuery.java
index 791a15b..f6f1c74 100644
--- a/src/main/java/de/ids_mannheim/korap/KrillQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/KrillQuery.java
@@ -1288,8 +1288,17 @@
if (v.matches("^[si]:\\.[\\+\\*]\\??$")) {
return new SpanRepetitionQueryWrapper();
};
+
+ SpanRegexQueryWrapper srqw = qb.re(v, isCaseInsensitive);
+
+ if (srqw.error != null) {
+ throw new QueryException(
+ StatusCodes.INVALID_QUERY,
+ "Invalid regex"
+ );
+ };
- saqw.or(qb.re(v, isCaseInsensitive));
+ saqw.or(srqw);
};
if (match.equals("match:ne")) {
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java
index a3ed45a..9f777c0 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java
@@ -12,7 +12,7 @@
public class SpanRegexQueryWrapper extends SpanQueryWrapper {
private SpanQuery query;
-
+ public String error = null;
public SpanRegexQueryWrapper (String field, String re) {
this(field, re, RegExp.ALL, false);
@@ -38,11 +38,18 @@
};
re = re.toLowerCase();
};
- RegexpQuery requery = new RegexpQuery(new Term(field, re), flags);
- query = new SpanMultiTermQueryWrapper<RegexpQuery>(requery);
+ try {
+ RegexpQuery requery = new RegexpQuery(new Term(field, re), flags);
+ query = new SpanMultiTermQueryWrapper<RegexpQuery>(requery);
+ } catch (Exception e) {
+ this.error = e.getLocalizedMessage();
+ }
};
+ public SpanRegexQueryWrapper (RegexpQuery requery) {
+ query = new SpanMultiTermQueryWrapper<RegexpQuery>(requery);
+ };
public SpanQuery toFragmentQuery () {
return this.query;
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
index b4bcf5c..5c6c565 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
@@ -694,6 +694,22 @@
"focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(tokens:s:der, []{1, 1}, right)}))");
};
+ @Test
+ public void queryJSONregexFail () {
+ // "Leserin.{,3}"
+ String json = getJsonString(getClass()
+ .getResource("/queries/segment/regex-simple.jsonld")
+ .getFile());
+ KrillQuery kq = new KrillQuery("tokens");
+
+ try {
+ String res = kq.fromKoral(json).toQuery().toString();
+ fail("Regex not expected to work");
+ }
+ catch (QueryException e) {
+ };
+ };
+
@Test
public void queryJSONregexVectorRewrite () throws QueryException {
diff --git a/src/test/resources/queries/segment/regex-simple.jsonld b/src/test/resources/queries/segment/regex-simple.jsonld
new file mode 100644
index 0000000..b87131b
--- /dev/null
+++ b/src/test/resources/queries/segment/regex-simple.jsonld
@@ -0,0 +1,13 @@
+{
+ "query":{
+ "@type":"koral:token",
+ "wrap":{
+ "@type":"koral:term",
+ "match":"match:eq",
+ "type":"type:regex",
+ "layer":"orth",
+ "key":"Leserin.{,3}"
+ }
+ },
+ "@context":"http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld"
+}