Skip to content

Commit

Permalink
backport apache#12249
Browse files Browse the repository at this point in the history
  • Loading branch information
ferhatsb committed Sep 24, 2024
1 parent f2ed5d9 commit 163b059
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
* This class also provides helpers to explore the different paths of the {@link Automaton}.
*/
public final class GraphTokenStreamFiniteStrings {

/** Maximum level of recursion allowed in recursive operations. */
private static final int MAX_RECURSION_LEVEL = 1000;

private final Map<Integer, BytesRef> idToTerm = new HashMap<>();
private final Map<Integer, Integer> idToInc = new HashMap<>();
private final Automaton det;
Expand Down Expand Up @@ -262,8 +266,15 @@ private int getTermID(int incr, int prevIncr, BytesRef term) {
return id;
}

private static void articulationPointsRecurse(Automaton a, int state, int d, int[] depth, int[] low, int[] parent,
BitSet visited, List<Integer> points) {
private static void articulationPointsRecurse(
Automaton a,
int state,
int d,
int[] depth,
int[] low,
int[] parent,
BitSet visited,
List<Integer> points) {
visited.set(state);
depth[state] = d;
low[state] = d;
Expand All @@ -275,7 +286,12 @@ private static void articulationPointsRecurse(Automaton a, int state, int d, int
a.getNextTransition(t);
if (visited.get(t.dest) == false) {
parent[t.dest] = state;
articulationPointsRecurse(a, t.dest, d + 1, depth, low, parent, visited, points);
if (d < MAX_RECURSION_LEVEL) {
articulationPointsRecurse(a, t.dest, d + 1, depth, low, parent, visited, points);
} else {
throw new IllegalArgumentException(
"Exceeded maximum recursion level during graph analysis");
}
childCount++;
if (low[t.dest] >= depth[state]) {
isArticulation = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.lucene.util.graph;

import java.util.ArrayList;
import java.util.Iterator;

import org.apache.lucene.analysis.CannedTokenStream;
Expand Down Expand Up @@ -596,4 +597,27 @@ public void testMultipleSidePaths() throws Exception {
terms = graph.getTerms("field", 7);
assertArrayEquals(terms, new Term[] {new Term("field", "network")});
}

public void testLongTokenStreamStackOverflowError() throws Exception {

ArrayList<Token> tokens =
new ArrayList<Token>() {
{
add(token("fast", 1, 1));
add(token("wi", 1, 1));
add(token("wifi", 0, 2));
add(token("fi", 1, 1));
}
};

// Add in too many tokens to get a high depth graph
for (int i = 0; i < 1024 + 1; i++) {
tokens.add(token("network", 1, 1));
}

TokenStream ts = new CannedTokenStream(tokens.toArray(new Token[0]));
GraphTokenStreamFiniteStrings graph = new GraphTokenStreamFiniteStrings(ts);

assertThrows(IllegalArgumentException.class, graph::articulationPoints);
}
}
2 changes: 1 addition & 1 deletion lucene/ivy-versions.properties
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ io.prometheus.version = 0.2.0
/javax.activation/activation = 1.1.1
/javax.servlet/javax.servlet-api = 3.1.0
/joda-time/joda-time = 2.2
/junit/junit = 4.12
/junit/junit = 4.13.2

/mecab/mecab-ipadic = 2.7.0-20070801
/mecab/mecab-ko-dic = 2.0.3-20170922
Expand Down
2 changes: 1 addition & 1 deletion lucene/version.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# RELEASE MANAGER must change this file after creating a release and
# enter new base version (format "x.y.z", no prefix/appendix):
version.base=7.7.3
version.base=7.7.3.1

# Other version property defaults, don't change:
version.suffix=SNAPSHOT
Expand Down

0 comments on commit 163b059

Please sign in to comment.