diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/utils/RedactionSearchUtility.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/utils/RedactionSearchUtility.java index c3a5323e..a830b7cd 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/utils/RedactionSearchUtility.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/utils/RedactionSearchUtility.java @@ -11,6 +11,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; 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.entity.TextEntity; import com.iqser.red.service.redaction.v1.server.model.document.textblock.TextBlock; @@ -20,6 +23,9 @@ import lombok.experimental.UtilityClass; @UtilityClass public class RedactionSearchUtility { + private static final Logger log = LoggerFactory.getLogger(RedactionSearchUtility.class); + + /** * 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())); List boundaries = new LinkedList<>(); - while (matcher.find()) { - boundaries.add(new TextRange(matcher.start(group) + textBlock.getTextRange().start(), matcher.end(group) + textBlock.getTextRange().start())); + try { + 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; } @@ -312,11 +322,16 @@ public class RedactionSearchUtility { private static List getTextRangesByPatternWithLineBreaks(TextBlock textBlock, int group, Pattern pattern) { + String searchTextWithLineBreaks = textBlock.searchTextWithLineBreaks(); Matcher matcher = pattern.matcher(searchTextWithLineBreaks); List boundaries = new LinkedList<>(); - while (matcher.find()) { - boundaries.add(new TextRange(matcher.start(group) + textBlock.getTextRange().start(), matcher.end(group) + textBlock.getTextRange().start())); + try { + 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; }