This is an automated email from the ASF dual-hosted git repository.

centic9 pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git

commit bf0025c02bee146850784c012aeb810d0fc02fe5
Author: Dominik Stadler <[email protected]>
AuthorDate: Sun Nov 1 11:19:09 2026 +0100

    Prevent StackOverflowException via too deep nesting of formulas
    
    Should fix https://issues.oss-fuzz.com/issues/499126302
---
 .../poi/ss/tests/formula/TestFormulaParser.java    | 25 ++++++++++++++++++++++
 .../org/apache/poi/ss/formula/FormulaParser.java   | 14 ++++++++----
 .../poi/ss/formula/OperandClassTransformer.java    | 20 +++++++++++------
 .../test/java/org/apache/poi/POIDataSamples.java   |  5 +++++
 ...-minimized-FormulaParserFuzzer-5191407997091840 |  1 +
 5 files changed, 54 insertions(+), 11 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 cf92ed72ec..6e9b177810 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
@@ -19,11 +19,18 @@
 package org.apache.poi.ss.tests.formula;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.poi.POIDataSamples;
 import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.formula.FormulaParseException;
@@ -226,4 +233,22 @@ class TestFormulaParser {
         assertThrows(FormulaParseException.class,
                 () -> FormulaParser.parse(formula, evalWorkbook, 
FormulaType.SHARED, 0));
     }
+
+    @Test
+    void testNestingIsLimited() throws IOException {
+        File file = POIDataSamples.getIntegrationInstance()
+                
.getFile("clusterfuzz-testcase-minimized-FormulaParserFuzzer-5191407997091840");
+
+        String formula = FileUtils.readFileToString(file, 
StandardCharsets.UTF_8);
+        assertNotNull(formula);
+        assertFalse(formula.isEmpty());
+
+        HSSFWorkbook workbook = new HSSFWorkbook();
+        HSSFEvaluationWorkbook evalWorkbook = 
HSSFEvaluationWorkbook.create(workbook);
+
+        // prevention of StackOverflowException should kick in and report a 
more meaningful exception
+        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 cb9a89b5ce..bea1d52d87 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
@@ -294,7 +294,7 @@ public final class FormulaParser {
         if (++_nestingDepth > MAX_NESTING_DEPTH) {
             throw new FormulaParseException("Formula has more than " + 
MAX_NESTING_DEPTH
                     + " levels of nested parentheses, functions or unary 
signs. "
-                    + "You can increase this value via 
FormulaParser.setMaxNestingDepth()");
+                    + "You can increase the limit via 
FormulaParser.setMaxNestingDepth()");
         }
     }
 
@@ -395,7 +395,7 @@ public final class FormulaParser {
 
     private static ParseNode augmentWithMemPtg(ParseNode root) {
         Ptg memPtg;
-        if (needsMemFunc(root)) {
+        if (needsMemFunc(root, 0)) {
             memPtg = new MemFuncPtg(root.getEncodedSize());
         } else {
             memPtg = new MemAreaPtg(root.getEncodedSize());
@@ -408,7 +408,13 @@ public final class FormulaParser {
      *  a tMemFunc token is used"
      *
      */
-    private static boolean needsMemFunc(ParseNode root) {
+    private static boolean needsMemFunc(ParseNode root, int nesting) {
+        if (nesting > FormulaParser.getMaxNestingDepth()) {
+            throw new FormulaParseException("Formula has more than " + 
FormulaParser.getMaxNestingDepth()
+                    + " levels of nested parentheses, functions or unary 
signs. "
+                    + "You can increase the limit via 
FormulaParser.setMaxNestingDepth()");
+        }
+
         Ptg token = root.getToken();
         if (token instanceof AbstractFunctionPtg) {
             return true;
@@ -423,7 +429,7 @@ public final class FormulaParser {
         if (token instanceof OperationPtg || token instanceof ParenthesisPtg) {
             // expect RangePtg, but perhaps also UnionPtg, IntersectionPtg etc
             for(ParseNode child : root.getChildren()) {
-                if (needsMemFunc(child)) {
+                if (needsMemFunc(child, nesting + 1)) {
                     return true;
                 }
             }
diff --git 
a/poi/src/main/java/org/apache/poi/ss/formula/OperandClassTransformer.java 
b/poi/src/main/java/org/apache/poi/ss/formula/OperandClassTransformer.java
index 6d9e9fa843..262f123ee2 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/OperandClassTransformer.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/OperandClassTransformer.java
@@ -73,7 +73,7 @@ final class OperandClassTransformer {
             default -> throw new IllegalStateException("Incomplete code - 
formula type ("
                     + _formulaType + ") not supported yet");
         };
-        transformNode(rootNode, rootNodeOperandClass, false);
+        transformNode(rootNode, rootNodeOperandClass, false, 0);
     }
 
     /**
@@ -82,7 +82,13 @@ final class OperandClassTransformer {
      * the function return value).
      */
     private void transformNode(ParseNode node, byte desiredOperandClass,
-            boolean callerForceArrayFlag) {
+            boolean callerForceArrayFlag, int nesting) {
+        if (nesting > FormulaParser.getMaxNestingDepth()) {
+            throw new FormulaParseException("Formula has more than " + 
FormulaParser.getMaxNestingDepth()
+                    + " levels of nested parentheses, functions or unary 
signs. "
+                    + "You can increase the limit via 
FormulaParser.setMaxNestingDepth()");
+        }
+
         Ptg token = node.getToken();
         ParseNode[] children = node.getChildren();
         boolean isSimpleValueFunc = isSimpleValueFunction(token);
@@ -99,7 +105,7 @@ final class OperandClassTransformer {
                     // caller's class, e.g. FREQUENCY stays 'A' in 
COUNT(ABS(FREQUENCY(...))).
                     childOperandClass = Ptg.CLASS_VALUE;
                 }
-                transformNode(child, childOperandClass, localForceArray);
+                transformNode(child, childOperandClass, localForceArray, 
nesting + 1);
             }
             setSimpleValueFuncClass((AbstractFunctionPtg) token, 
desiredOperandClass, callerForceArrayFlag);
             return;
@@ -125,12 +131,12 @@ final class OperandClassTransformer {
             // be converted to 'V' type.
             byte localDesiredOperandClass = desiredOperandClass == 
Ptg.CLASS_REF ? Ptg.CLASS_VALUE : desiredOperandClass;
             for (ParseNode child : children) {
-                transformNode(child, localDesiredOperandClass, 
callerForceArrayFlag);
+                transformNode(child, localDesiredOperandClass, 
callerForceArrayFlag, nesting + 1);
             }
             return;
         }
         if (token instanceof AbstractFunctionPtg afp) {
-            transformFunctionNode(afp, children, desiredOperandClass, 
callerForceArrayFlag);
+            transformFunctionNode(afp, children, desiredOperandClass, 
callerForceArrayFlag, nesting + 1);
             return;
         }
         if (children.length > 0) {
@@ -191,7 +197,7 @@ final class OperandClassTransformer {
     }
 
     private void transformFunctionNode(AbstractFunctionPtg afp, ParseNode[] 
children,
-            byte desiredOperandClass, boolean callerForceArrayFlag) {
+            byte desiredOperandClass, boolean callerForceArrayFlag, int 
nesting) {
 
         boolean localForceArrayFlag;
         byte defaultReturnOperandClass = afp.getDefaultOperandClass();
@@ -271,7 +277,7 @@ final class OperandClassTransformer {
         for (int i = 0; i < children.length; i++) {
             ParseNode child = children[i];
             byte paramOperandClass = afp.getParameterClass(i);
-            transformNode(child, paramOperandClass, localForceArrayFlag);
+            transformNode(child, paramOperandClass, localForceArrayFlag, 
nesting + 1);
         }
     }
 
diff --git a/poi/src/test/java/org/apache/poi/POIDataSamples.java 
b/poi/src/test/java/org/apache/poi/POIDataSamples.java
index 6106e449f4..d52378a5d3 100644
--- a/poi/src/test/java/org/apache/poi/POIDataSamples.java
+++ b/poi/src/test/java/org/apache/poi/POIDataSamples.java
@@ -125,6 +125,11 @@ public final class POIDataSamples {
         return _instXmlDSign;
     }
 
+    public static POIDataSamples getIntegrationInstance() {
+        if(_instXmlDSign == null) _instXmlDSign = new 
POIDataSamples("integration");
+        return _instXmlDSign;
+    }
+
     /**
      * Opens a sample file from the test data directory
      *
diff --git 
a/test-data/integration/clusterfuzz-testcase-minimized-FormulaParserFuzzer-5191407997091840
 
b/test-data/integration/clusterfuzz-testcase-minimized-FormulaParserFuzzer-5191407997091840
new file mode 100644
index 0000000000..346dd6e878
--- /dev/null
+++ 
b/test-data/integration/clusterfuzz-testcase-minimized-FormulaParserFuzzer-5191407997091840
@@ -0,0 +1 @@
+4-6..4-56646..29966,666,6.2995,-6666,724, 
--6..996,-6.6696926,666,6.2995,-6666,724, --6,-66664 
,-2-7,6629966,666,6.2996,-66664 
,-2-7,66,--62996,-9966,666,6.2996,-6.6696926,666,6.2995,-6666,724, --6,-66664 
,-2-7,6629966,666,6.2996,-66664 
,-2-7,66,--62996,-9966,666,6.2996,-6.6696926,666,6.2995,-6666,724, --6,-66664 
,-2-7,6629966,666,6.2996,-66664 
,-2-7,66,--62996,-9966,666,6.2996,-6.6696926,666,6.2995,-6666,724, --6,-66664 
,-2-7,6629966,666,6.2996,-66664 ,-2-7,66,--62996,-9966,666,6.2996,- [...]
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to