This is an automated email from the ASF dual-hosted git repository. centic9 pushed a commit to branch npes_fuzzing_and_test_improvements in repository https://gitbox.apache.org/repos/asf/poi.git
commit d80b7ef268a8f8f733aa4cb9421f9164c342f05b Author: Dominik Stadler <[email protected]> AuthorDate: Sun Oct 25 19:53:16 2026 +0100 Avoid stackoverflow on formula with many nested names and ranges Fixes https://issues.oss-fuzz.com/issues/491506839 --- .../org/apache/poi/ss/tests/formula/TestFormulaParser.java | 12 ++++++++++++ .../main/java/org/apache/poi/ss/formula/FormulaParser.java | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/formula/TestFormulaParser.java b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/formula/TestFormulaParser.java index d39cc3866c..cf92ed72ec 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/formula/TestFormulaParser.java +++ b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/formula/TestFormulaParser.java @@ -214,4 +214,16 @@ class TestFormulaParser { assertThrows(FormulaParseException.class, () -> checkFormula("r:[]!d:F��")); } + + @Test + void testStackOverflow() { + // this triggers nested method calls and thus needs to be included + // in the existing nested-handling in FormulaParser + String formula = "_a:a".repeat(4000); + + HSSFWorkbook workbook = new HSSFWorkbook(); + HSSFEvaluationWorkbook evalWorkbook = HSSFEvaluationWorkbook.create(workbook); + assertThrows(FormulaParseException.class, + () -> FormulaParser.parse(formula, evalWorkbook, FormulaType.SHARED, 0)); + } } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java b/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java index 8ade019bba..cb9a89b5ce 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java @@ -293,7 +293,8 @@ public final class FormulaParser { private void enterNesting() { if (++_nestingDepth > MAX_NESTING_DEPTH) { throw new FormulaParseException("Formula has more than " + MAX_NESTING_DEPTH - + " levels of nested parentheses, functions or unary signs"); + + " levels of nested parentheses, functions or unary signs. " + + "You can increase this value via FormulaParser.setMaxNestingDepth()"); } } @@ -1288,7 +1289,16 @@ public final class FormulaParser { */ private SheetIdentifier parseSheetRange(String bookname, NameIdentifier sheet1Name, boolean isSndPartOfQuotedRange) { nextChar(); - SheetIdentifier sheet2 = parseSheetName(isSndPartOfQuotedRange); + + // need to keep an eye on nesting to avoid stackoverflow + SheetIdentifier sheet2; + enterNesting(); + try { + sheet2 = parseSheetName(isSndPartOfQuotedRange); + } finally { + _nestingDepth--; + } + if (sheet2 != null) { return new SheetRangeIdentifier(bookname, sheet1Name, sheet2.getSheetIdentifier()); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
