Merge branch 'RED-10131-master' into 'master'
RED-10131: Catch stackoverflow error of unoptimized regexes Closes RED-10131 See merge request redactmanager/redaction-service!532
This commit is contained in:
commit
daed4e07ef
@ -11,6 +11,9 @@ import java.util.regex.Matcher;
|
|||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.iqser.red.service.redaction.v1.server.model.document.TextRange;
|
import com.iqser.red.service.redaction.v1.server.model.document.TextRange;
|
||||||
import com.iqser.red.service.redaction.v1.server.model.document.entity.TextEntity;
|
import com.iqser.red.service.redaction.v1.server.model.document.entity.TextEntity;
|
||||||
import com.iqser.red.service.redaction.v1.server.model.document.textblock.TextBlock;
|
import com.iqser.red.service.redaction.v1.server.model.document.textblock.TextBlock;
|
||||||
@ -20,6 +23,9 @@ import lombok.experimental.UtilityClass;
|
|||||||
@UtilityClass
|
@UtilityClass
|
||||||
public class RedactionSearchUtility {
|
public class RedactionSearchUtility {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(RedactionSearchUtility.class);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if any part of a CharSequence matches a given regex pattern.
|
* Checks if any part of a CharSequence matches a given regex pattern.
|
||||||
*
|
*
|
||||||
@ -303,8 +309,12 @@ public class RedactionSearchUtility {
|
|||||||
|
|
||||||
Matcher matcher = pattern.matcher(textBlock.subSequence(textBlock.getTextRange()));
|
Matcher matcher = pattern.matcher(textBlock.subSequence(textBlock.getTextRange()));
|
||||||
List<TextRange> boundaries = new LinkedList<>();
|
List<TextRange> boundaries = new LinkedList<>();
|
||||||
while (matcher.find()) {
|
try {
|
||||||
boundaries.add(new TextRange(matcher.start(group) + textBlock.getTextRange().start(), matcher.end(group) + textBlock.getTextRange().start()));
|
while (matcher.find()) {
|
||||||
|
boundaries.add(new TextRange(matcher.start(group) + textBlock.getTextRange().start(), matcher.end(group) + textBlock.getTextRange().start()));
|
||||||
|
}
|
||||||
|
}catch (StackOverflowError stackOverflowError){
|
||||||
|
log.warn("Stackoverflow error for pattern {} in text: {}", pattern.pattern(), textBlock);
|
||||||
}
|
}
|
||||||
return boundaries;
|
return boundaries;
|
||||||
}
|
}
|
||||||
@ -312,11 +322,16 @@ public class RedactionSearchUtility {
|
|||||||
|
|
||||||
private static List<TextRange> getTextRangesByPatternWithLineBreaks(TextBlock textBlock, int group, Pattern pattern) {
|
private static List<TextRange> getTextRangesByPatternWithLineBreaks(TextBlock textBlock, int group, Pattern pattern) {
|
||||||
|
|
||||||
|
|
||||||
String searchTextWithLineBreaks = textBlock.searchTextWithLineBreaks();
|
String searchTextWithLineBreaks = textBlock.searchTextWithLineBreaks();
|
||||||
Matcher matcher = pattern.matcher(searchTextWithLineBreaks);
|
Matcher matcher = pattern.matcher(searchTextWithLineBreaks);
|
||||||
List<TextRange> boundaries = new LinkedList<>();
|
List<TextRange> boundaries = new LinkedList<>();
|
||||||
while (matcher.find()) {
|
try {
|
||||||
boundaries.add(new TextRange(matcher.start(group) + textBlock.getTextRange().start(), matcher.end(group) + textBlock.getTextRange().start()));
|
while (matcher.find()) {
|
||||||
|
boundaries.add(new TextRange(matcher.start(group) + textBlock.getTextRange().start(), matcher.end(group) + textBlock.getTextRange().start()));
|
||||||
|
}
|
||||||
|
}catch (StackOverflowError stackOverflowError){
|
||||||
|
log.warn("Stackoverflow error for pattern {} in text with linebreaks: {}", pattern.pattern(), searchTextWithLineBreaks);
|
||||||
}
|
}
|
||||||
return boundaries;
|
return boundaries;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user