spring-projects / spring-projects/spring-ai

Stack overflow exception in trimAdjacentBlankLines() method due to inefficient regex in ExtractedTextFormatter

Open Beginner friendly
#2,247 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

Bug description
When using TikaDocumentReader, I often encountered the following error:

java
Caused by: java.lang.StackOverflowError: null  
    at java.base/java.util.regex.Pattern$Caret.match(Pattern.java:3896)  
    at java.base/java.util.regex.Pattern$Curly.match1(Pattern.java:4597)  
    at java.base/java.util.regex.Pattern$Curly.match(Pattern.java:4546)  
    at java.base/java.util.regex.Pattern$Dollar.match(Pattern.java:3996)  
    at java.base/java.util.regex.Pattern$Caret.match(Pattern.java:3906)  
    at java.base/java.util.regex.Pattern$GroupHead.match(Pattern.java:4969).  

After debugging, I found that the issue lies in the non-optimized regular expression from the trimAdjacentBlankLines() method in the ExtractedTextFormatter class. In my case, the problem occasionally occurred even with a files with small number of empty lines (~150) with default VM stack settings -Xmx8192m.

Steps to reproduce
For testing the occurrence of this error, I created an XLSX file with a large number of empty rows. I am attaching it to this issue.
Stack_overflow_exception_test.xlsx

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in ExtractedTextFormatter at trimAdjacentBlankLines() and inspect the regular expression used there. Run the attached Stack_overflow_exception_test.xlsx through TikaDocumentReader to reproduce the failure, then verify that formatting completes without StackOverflowError while adjacent blank lines are still trimmed.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend, performance
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.