You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2026/10/26 20:09:23 UTC

(poi) branch npes_fuzzing_and_test_improvements created (now d5104f33d2)

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

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


      at d5104f33d2 Fuzzing: Remove double testing of write()

This branch includes the following new commits:

     new 92f1c616ba Disable test which performs the same as TestAllFiles
     new d80b7ef268 Avoid stackoverflow on formula with many nested names and ranges
     new 06902d305f Replace assertions with logs
     new c99a65e1ee Fix issues in CellFormat found by fuzzing
     new f7737f04f8 Add fuzzing for CellFormat and enable dictionaries
     new fa4f27c661 Fuzzing: Make it possible to compute coverage of fuzzing
     new d5104f33d2 Fuzzing: Remove double testing of write()

The 7 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


(poi) 07/07: Fuzzing: Remove double testing of write()

Posted by ce...@apache.org.
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 d5104f33d2a72bc66d51ad4c68ec841fee9393af
Author: Dominik Stadler <ce...@apache.org>
AuthorDate: Mon Oct 26 10:16:42 2026 +0100

    Fuzzing: Remove double testing of write()
    
    Should be fully covered by the separate fuzzers already
---
 poi-fuzz/src/main/java/org/apache/poi/fuzz/POIFuzzer.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/poi-fuzz/src/main/java/org/apache/poi/fuzz/POIFuzzer.java b/poi-fuzz/src/main/java/org/apache/poi/fuzz/POIFuzzer.java
index 3318458eff..52fa443106 100644
--- a/poi-fuzz/src/main/java/org/apache/poi/fuzz/POIFuzzer.java
+++ b/poi-fuzz/src/main/java/org/apache/poi/fuzz/POIFuzzer.java
@@ -107,7 +107,7 @@ public class POIFuzzer {
 				}
 			}
 
-			wb.write(NullOutputStream.INSTANCE);
+			// writing is tested in the separate fuzzers anyway: wb.write(NullOutputStream.INSTANCE);
 		} catch (IOException | POIXMLException | IllegalArgumentException | RecordFormatException |
 				 IndexOutOfBoundsException | HSLFException | RecordInputStream.LeftoverDataException |
 				 IllegalStateException | BufferUnderflowException | OpenXML4JRuntimeException |


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


(poi) 01/07: Disable test which performs the same as TestAllFiles

Posted by ce...@apache.org.
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 92f1c616ba38d3377fd414a090bbf50cedf348ff
Author: Dominik Stadler <ce...@apache.org>
AuthorDate: Sun Oct 25 11:31:02 2026 +0100

    Disable test which performs the same as TestAllFiles
    
    This is used for local testing and should not run
    the same tests as TestAllFiles does anyway
---
 .../src/test/java/org/apache/poi/stress/HSLFFileHandler.java            | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/poi-integration/src/test/java/org/apache/poi/stress/HSLFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/HSLFFileHandler.java
index 7bf228cef2..323f9b860f 100644
--- a/poi-integration/src/test/java/org/apache/poi/stress/HSLFFileHandler.java
+++ b/poi-integration/src/test/java/org/apache/poi/stress/HSLFFileHandler.java
@@ -27,6 +27,7 @@ import org.apache.logging.log4j.Logger;
 import org.apache.poi.logging.PoiLogManager;
 import org.apache.poi.hslf.usermodel.HSLFSlideShow;
 import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
+import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 
 public class HSLFFileHandler extends SlideShowHandler {
@@ -59,6 +60,7 @@ public class HSLFFileHandler extends SlideShowHandler {
     }
 
     // a test-case to test all .ppt files without executing the full TestAllFiles
+    @Disabled("This is run as part of overall poi-integration testing already, we should not always run this but only use it locally")
     @Override
     @Test
     void test() throws Exception {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


(poi) 06/07: Fuzzing: Make it possible to compute coverage of fuzzing

Posted by ce...@apache.org.
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 fa4f27c66158285053853dab1982a961da287bbe
Author: Dominik Stadler <ce...@apache.org>
AuthorDate: Mon Oct 26 09:43:38 2026 +0100

    Fuzzing: Make it possible to compute coverage of fuzzing
    
    The script records coverage of all files currently
    in the corpus-directories.
    
    This allows to check which areas of the code are
    well-covered already and which one could use a
    dedicated fuzz-target.
---
 .gitignore                 |   1 +
 poi-fuzz/build.gradle      |  32 ++++++++++++
 poi-fuzz/coverageReport.sh | 124 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 157 insertions(+)

diff --git a/.gitignore b/.gitignore
index 5c89de9eac..0440e06c47 100644
--- a/.gitignore
+++ b/.gitignore
@@ -37,6 +37,7 @@ TEST-org.apache.poi*.xml
 /*/build/
 dist
 lib/
+/poi-fuzz/jacoco-*.zip
 /poi-fuzz/jazzer
 /poi-fuzz/jazzer-*.tar.gz
 /poi-fuzz/jazzer_standalone.jar
diff --git a/poi-fuzz/build.gradle b/poi-fuzz/build.gradle
index 8815205fd1..fdfb1ff9c8 100644
--- a/poi-fuzz/build.gradle
+++ b/poi-fuzz/build.gradle
@@ -70,7 +70,39 @@ tasks.register('testJar', Jar) {
     from sourceSets.test.output + sourceSets.main.output
 }
 
+
+tasks.register('getFuzzDeps', Copy) {
+    from sourceSets.main.runtimeClasspath
+    into 'build/runtime/'
+}
+
 shadowJar{
     // set name to not include the version in the file-name
     archiveFileName = "poi-fuzz-all.jar"
 }
+
+idea {
+    module {
+        excludeDirs += [
+            file("corpus"),
+            file("corpusCellFormat"),
+            file("corpusEncryptDecrypt"),
+            file("corpusFormulaParser"),
+            file("corpusHDGF"),
+            file("corpusHMEF"),
+            file("corpusHPBF"),
+            file("corpusHPSF"),
+            file("corpusHSLF"),
+            file("corpusHSMF"),
+            file("corpusHSSF"),
+            file("corpusHWPF"),
+            file("corpusOldExcel"),
+            file("corpusRle"),
+            file("corpusVisio"),
+            file("corpusXSLF"),
+            file("corpusXSSF"),
+            file("corpusXSSFB"),
+            file("corpusXWPF"),
+        ]
+    }
+}
diff --git a/poi-fuzz/coverageReport.sh b/poi-fuzz/coverageReport.sh
new file mode 100755
index 0000000000..6597154c87
--- /dev/null
+++ b/poi-fuzz/coverageReport.sh
@@ -0,0 +1,124 @@
+#!/bin/bash
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+# Small helper script to produce a coverage report when executing the fuzz-model
+# against the current corpus.
+#
+
+set -eu
+
+if [ $# -gt 0 ]; then
+  ONE=$1
+  echo
+  echo "Fuzzing only ${ONE}"
+else
+  ONE=
+fi
+
+
+# Build the fuzzer and fetch dependency-jars
+echo
+echo Rebuilding
+../gradlew shadowJar getFuzzDeps
+
+
+# extract jar-files of Apache POI
+mkdir -p build/poifiles
+cd build/poifiles
+
+# then unpack the class-files
+for i in `find ../runtime -name poi-*.jar`; do
+  echo $i
+  unzip -o -q $i
+done
+
+# Remove some packages that we do not want to include in the Report
+rm -r com
+rm -r org/openxmlformats
+rm -r org/etsi
+rm -r org/w3
+rm -r org/apache/poi/examples
+
+cd -
+
+
+
+# Fetch JaCoCo Agent
+test -f jacoco-0.8.15.zip || wget --continue https://repo1.maven.org/maven2/org/jacoco/jacoco/0.8.15/jacoco-0.8.15.zip
+unzip -o jacoco-0.8.15.zip lib/jacocoagent.jar lib/jacococli.jar
+mv lib/jacocoagent.jar lib/jacococli.jar build/
+rmdir lib
+
+mkdir -p build/jacoco
+
+echo
+if [[ "$(uname -s)" == CYGWIN* ]]; then
+  echo "Running under Cygwin"
+  JAZZER_DELIMITER=":"
+  PATH_DELIMITER=";"
+else
+  JAZZER_DELIMITER="\\:"
+  PATH_DELIMITER=":"
+fi
+
+
+for i in `cd src/main/java/org/apache/poi/fuzz/ && ls ${ONE}*Fuzzer.java`; do
+  CLASS=`echo $i | sed -e 's/.java//g'`
+  CORPUS=corpus`echo ${CLASS} | sed -e 's/Fuzzer//g' | sed -e 's/POI//g'`
+
+  # no separate corpus for this one
+  if [ "${CLASS}" == "XLSX2CSVFuzzer" ]; then
+    CORPUS=corpusXSSF
+  elif [ "${CLASS}" == "POIFileHandlerFuzzer" ]; then
+    CORPUS=corpus
+  fi
+
+  echo
+  echo Running jazzer for ${CLASS} and ${CORPUS}
+
+  # Run Jazzer with JaCoCo-Agent to produce coverage information
+  ./jazzer \
+    "--cp=build/poifiles${PATH_DELIMITER}build/libs/poi-fuzz-all.jar" \
+    "--instrumentation_includes=org.apache.poi.**${PATH_DELIMITER}org.apache.xmlbeans.**" \
+    --target_class=org.apache.poi.fuzz.${CLASS} \
+    --nohooks \
+    --jvm_args="-javaagent${JAZZER_DELIMITER}build/jacocoagent.jar=destfile=build/jacoco/${CORPUS}.exec" \
+    -rss_limit_mb=8192 \
+    -runs=0 \
+    ${CORPUS}
+done
+
+echo
+echo Having coverage-files
+ls -al build/jacoco/corpus*.exec
+
+# Finally create the JaCoCo report
+echo
+echo Creating JaCoCo report
+java -jar build/jacococli.jar report build/jacoco/corpus*.exec \
+ --classfiles build/poifiles \
+ --sourcefiles /opt/apache/poi/git-svn/poi/src/main/java \
+ --sourcefiles /opt/apache/poi/git-svn/poi-ooxml/src/main/java \
+ --sourcefiles /opt/apache/poi/git-svn/poi-ooxml-lite/src/main/java \
+ --sourcefiles /opt/apache/poi/git-svn/poi-scratchpad/src/main/java \
+ --sourcefiles /opt/apache/xmlbeans/trunk/src/main/java \
+ --sourcefiles src/main/java \
+ --html build/reports/jacoco
+
+
+echo All Done, report is at build/reports/jacoco/index.html


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


(poi) 05/07: Add fuzzing for CellFormat and enable dictionaries

Posted by ce...@apache.org.
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 f7737f04f80221330dc17464b8f17e8a3588cb55
Author: Dominik Stadler <ce...@apache.org>
AuthorDate: Mon Oct 26 09:07:12 2026 +0100

    Add fuzzing for CellFormat and enable dictionaries
---
 .github/workflows/fuzzing.yml                      |  6 +-
 poi-fuzz/README.md                                 |  7 ++-
 .../java/org/apache/poi/fuzz/CellFormatFuzzer.dict | 31 ++++++++++
 .../java/org/apache/poi/fuzz/CellFormatFuzzer.java | 72 ++++++++++++++++++++++
 4 files changed, 114 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/fuzzing.yml b/.github/workflows/fuzzing.yml
index f3b5f6f100..da7b6bb81e 100644
--- a/.github/workflows/fuzzing.yml
+++ b/.github/workflows/fuzzing.yml
@@ -65,6 +65,7 @@ jobs:
         mkdir -p corpusHMEF && cp -a corpus/hmef/* corpusHMEF/
         mkdir -p corpusHPBF && cp -a corpus/publisher/* corpusHPBF/
         mkdir -p corpusHSMF && cp -a corpus/hsmf/* corpusHSMF/
+        mkdir -p corpusCellFormat
         mkdir -p corpusEncryptDecrypt
         mkdir -p corpusFormulaParser
 
@@ -125,7 +126,10 @@ jobs:
       run: ./jazzer --cp=build/libs/poi-fuzz-all.jar "--instrumentation_includes=org.apache.poi.**:org.apache.xmlbeans.**" --target_class=org.apache.poi.fuzz.EncryptDecryptFuzzer -rss_limit_mb=4096 -timeout=120 -max_total_time=120 corpusEncryptDecrypt
 
     - name: Run Fuzzing - FormulaParserFuzzer
-      run: ./jazzer --cp=build/libs/poi-fuzz-all.jar "--instrumentation_includes=org.apache.poi.**:org.apache.xmlbeans.**" --target_class=org.apache.poi.fuzz.FormulaParserFuzzer -rss_limit_mb=4096 -timeout=120 -max_total_time=120 corpusFormulaParser
+      run: ./jazzer --cp=build/libs/poi-fuzz-all.jar "--instrumentation_includes=org.apache.poi.**:org.apache.xmlbeans.**" --target_class=org.apache.poi.fuzz.FormulaParserFuzzer -rss_limit_mb=4096 -timeout=120 -max_total_time=120 -dict=src/main/java/org/apache/poi/fuzz/FormulaParserFuzzer.dict corpusFormulaParser
+
+    - name: Run Fuzzing - CellFormatFuzzer
+      run: ./jazzer --cp=build/libs/poi-fuzz-all.jar "--instrumentation_includes=org.apache.poi.**:org.apache.xmlbeans.**" --target_class=org.apache.poi.fuzz.CellFormatFuzzer -rss_limit_mb=4096 -timeout=120 -max_total_time=120 -dict=src/main/java/org/apache/poi/fuzz/CellFormatFuzzer.dict corpusCellFormat
 
     - name: Run Fuzzing - POIFuzzer
       run: ./jazzer --cp=build/libs/poi-fuzz-all.jar --instrumentation_includes=org.apache.poi.**:org.apache.xmlbeans.** --target_class=org.apache.poi.fuzz.POIFuzzer -rss_limit_mb=4096 -timeout=600 -max_total_time=600 corpus
diff --git a/poi-fuzz/README.md b/poi-fuzz/README.md
index e9d2cbf0e9..d2ef853c06 100644
--- a/poi-fuzz/README.md
+++ b/poi-fuzz/README.md
@@ -165,7 +165,12 @@ Can re-use XSSF-corpus as the same files are processed here
 ## FormulaParser
 
     mkdir corpusFormulaParser
-    ../gradlew shadowJar && ./jazzer --cp=build/libs/poi-fuzz-all.jar "--instrumentation_includes=org.apache.poi.**:org.apache.xmlbeans.**" --target_class=org.apache.poi.fuzz.FormulaParserFuzzer -rss_limit_mb=2560 corpusFormulaParser
+    ../gradlew shadowJar && ./jazzer --cp=build/libs/poi-fuzz-all.jar "--instrumentation_includes=org.apache.poi.**:org.apache.xmlbeans.**" --target_class=org.apache.poi.fuzz.FormulaParserFuzzer -rss_limit_mb=2560 -dict=src/main/java/org/apache/poi/fuzz/FormulaParserFuzzer.dict corpusFormulaParser
+
+## CellFormatFuzzer
+
+    mkdir corpusCellFormat
+    ../gradlew shadowJar && ./jazzer --cp=build/libs/poi-fuzz-all.jar "--instrumentation_includes=org.apache.poi.**:org.apache.xmlbeans.**" --target_class=org.apache.poi.fuzz.CellFormatFuzzer -rss_limit_mb=2560 -dict=src/main/java/org/apache/poi/fuzz/CellFormatFuzzer.dict corpusCellFormat
 
 ## FileHandler
 
diff --git a/poi-fuzz/src/main/java/org/apache/poi/fuzz/CellFormatFuzzer.dict b/poi-fuzz/src/main/java/org/apache/poi/fuzz/CellFormatFuzzer.dict
new file mode 100644
index 0000000000..331abec325
--- /dev/null
+++ b/poi-fuzz/src/main/java/org/apache/poi/fuzz/CellFormatFuzzer.dict
@@ -0,0 +1,31 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Excel Formula Dictionary for FormulaParserFuzzer
+"$"
+"#"
+"_"
+"("
+")"
+";"
+"["
+"Red"
+"Green"
+"]"
+"\\"
+","
+"."
+"0"
+"-"
diff --git a/poi-fuzz/src/main/java/org/apache/poi/fuzz/CellFormatFuzzer.java b/poi-fuzz/src/main/java/org/apache/poi/fuzz/CellFormatFuzzer.java
new file mode 100644
index 0000000000..a818db30c4
--- /dev/null
+++ b/poi-fuzz/src/main/java/org/apache/poi/fuzz/CellFormatFuzzer.java
@@ -0,0 +1,72 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.fuzz;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.nio.BufferUnderflowException;
+import java.nio.charset.StandardCharsets;
+import java.util.Locale;
+import java.util.NoSuchElementException;
+
+import org.apache.commons.lang3.RandomStringUtils;
+import org.apache.commons.lang3.RandomUtils;
+import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.format.CellFormat;
+import org.apache.poi.ss.formula.FormulaParseException;
+import org.apache.poi.ss.formula.FormulaParser;
+import org.apache.poi.ss.formula.FormulaType;
+import org.apache.poi.util.RecordFormatException;
+
+import com.code_intelligence.jazzer.api.FuzzedDataProvider;
+
+/**
+ * Fuzz target for the Apache POI cell formatter.
+ * Used by Google's OSS-Fuzz for continuous security testing.
+ */
+public class CellFormatFuzzer {
+    public static void fuzzerTestOneInput(FuzzedDataProvider data) {
+        Locale locale = switch (data.consumeInt(0, 6)) {
+            case 0 -> Locale.ROOT;
+            case 1 -> Locale.ENGLISH;
+            case 2 -> Locale.GERMAN;
+            case 3 -> Locale.CANADA;
+            case 4 -> Locale.CHINESE;
+            case 5 -> Locale.JAPAN;
+            case 6 -> Locale.KOREAN;
+            default -> throw new IllegalArgumentException();
+        };
+
+        Object obj = switch (data.consumeInt(0, 2)) {
+            case 0 -> data.consumeDouble();
+            case 1 -> data.consumeLong();
+            case 2 -> data.consumeString(data.consumeInt(0, 100));
+            default -> throw new IllegalArgumentException();
+        };
+
+        try {
+            CellFormat cellFormat = CellFormat.getInstance(locale, data.consumeRemainingAsString());
+
+            cellFormat.apply(obj);
+        } catch (IllegalArgumentException | IndexOutOfBoundsException | IllegalStateException |
+                ArithmeticException e) {
+            // expected
+        }
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


(poi) 04/07: Fix issues in CellFormat found by fuzzing

Posted by ce...@apache.org.
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 c99a65e1ee757557e576d30d3c19d664f4322c8f
Author: Dominik Stadler <ce...@apache.org>
AuthorDate: Mon Oct 26 08:25:58 2026 +0100

    Fix issues in CellFormat found by fuzzing
    
    Avoid NPEs and ClassCastExceptions on
    invalid cell format
    
    Fixes https://issues.oss-fuzz.com/issues/577661384
---
 .../java/org/apache/poi/ss/format/CellFormat.java  | 20 +++++++++++++-
 .../apache/poi/ss/format/CellNumberFormatter.java  |  8 ++++++
 .../apache/poi/ss/format/CellNumberStringMod.java  |  8 ++++++
 .../org/apache/poi/ss/format/TestCellFormat.java   | 32 ++++++++++++++--------
 4 files changed, 56 insertions(+), 12 deletions(-)

diff --git a/poi/src/main/java/org/apache/poi/ss/format/CellFormat.java b/poi/src/main/java/org/apache/poi/ss/format/CellFormat.java
index b78fc86886..93becf26ee 100644
--- a/poi/src/main/java/org/apache/poi/ss/format/CellFormat.java
+++ b/poi/src/main/java/org/apache/poi/ss/format/CellFormat.java
@@ -279,6 +279,15 @@ public class CellFormat {
     public CellFormatResult apply(Object value) {
         if (value instanceof Number num) {
             double val = num.doubleValue();
+
+            // check if one of the conditions below would fail with NPE
+            if (val < 0 && (
+                    (formatPartCount == 2 && posNumFmt == null) ||
+                    (formatPartCount > 1 && negNumFmt == null))) {
+                throw new IllegalStateException("Cannot apply format " + format + " on object " + value + " with empty posNumFmt : "
+                    + posNumFmt + " or negNumFmt " + negNumFmt + ", formatPartCount: " + formatPartCount);
+            }
+
             if (val < 0 &&
                     ((formatPartCount == 2
                             && !posNumFmt.hasCondition() && !negNumFmt.hasCondition())
@@ -289,7 +298,12 @@ public class CellFormat {
                 // the default leading minus sign is not also output
                 return negNumFmt.apply(-val);
             } else {
-                return getApplicableFormatPart(val).apply(val);
+                CellFormatPart applicableFormatPart = getApplicableFormatPart(val);
+                if (applicableFormatPart == null) {
+                    throw new IllegalStateException("Cannot apply format " + format + " on object " + value + ", no applicable format part found ("
+                            + "posNumFmt: " + posNumFmt + ", negNumFmt: " + negNumFmt + ", zeroNumFmt: " + zeroNumFmt);
+                }
+                return applicableFormatPart.apply(val);
             }
         } else if (value instanceof java.util.Date d) {
             // Don't know (and can't get) the workbook date windowing (1900 or 1904)
@@ -301,6 +315,10 @@ public class CellFormat {
                 throw new IllegalArgumentException("value " + numericValue + " of date " + value + " is not a valid Excel date");
             }
         } else {
+            if (textFmt == null) {
+                throw new IllegalStateException("Cannot apply format " + format + " on object " + value + " with empty format");
+            }
+
             return textFmt.apply(value);
         }
     }
diff --git a/poi/src/main/java/org/apache/poi/ss/format/CellNumberFormatter.java b/poi/src/main/java/org/apache/poi/ss/format/CellNumberFormatter.java
index f84da27c5e..7d6a7f4402 100644
--- a/poi/src/main/java/org/apache/poi/ss/format/CellNumberFormatter.java
+++ b/poi/src/main/java/org/apache/poi/ss/format/CellNumberFormatter.java
@@ -452,6 +452,10 @@ public class CellNumberFormatter extends CellFormatter {
 
     @Override
     public void formatValue(StringBuffer toAppendTo, Object valueObject) {
+        if (!(valueObject instanceof Number)) {
+            throw new IllegalArgumentException("Expected object of type number for format " + format + ", but had: " + valueObject);
+        }
+
         BigDecimal bd = BigDecimal.valueOf(((Number) valueObject).doubleValue()).multiply(BigDecimal.valueOf(scale));
         double value = bd.doubleValue();
 
@@ -547,6 +551,10 @@ public class CellNumberFormatter extends CellFormatter {
                         modPos++;
                     }
 
+                    if (nextChange.getEnd() == null) {
+                        throw new IllegalArgumentException("Cannot apply format, end could not determined for format: " + format);
+                    }
+
                     // delete end point in original
                     int delEndPos = nextChange.getEnd().pos;
                     if (nextChange.isEndInclusive()) {
diff --git a/poi/src/main/java/org/apache/poi/ss/format/CellNumberStringMod.java b/poi/src/main/java/org/apache/poi/ss/format/CellNumberStringMod.java
index 66b4bf5f2a..7e6b84d272 100644
--- a/poi/src/main/java/org/apache/poi/ss/format/CellNumberStringMod.java
+++ b/poi/src/main/java/org/apache/poi/ss/format/CellNumberStringMod.java
@@ -65,6 +65,14 @@ public class CellNumberStringMod implements Comparable<CellNumberStringMod> {
 
     @Override
     public int compareTo(CellNumberStringMod that) {
+        if (special == null) {
+            if (that.special == null) {
+                return 0;
+            }
+
+            return 1;
+        }
+
         int diff = special.pos - that.special.pos;
         return (diff != 0) ? diff : (op - that.op);
     }
diff --git a/poi/src/test/java/org/apache/poi/ss/format/TestCellFormat.java b/poi/src/test/java/org/apache/poi/ss/format/TestCellFormat.java
index 06297fff84..34ab561b25 100644
--- a/poi/src/test/java/org/apache/poi/ss/format/TestCellFormat.java
+++ b/poi/src/test/java/org/apache/poi/ss/format/TestCellFormat.java
@@ -1131,7 +1131,7 @@ class TestCellFormat {
 
     @Test
     void testNamedColors() {
-        // Make sure we have all standard named colors defined 
+        // Make sure we have all standard named colors defined
         // and are returned as non-null regardless of case
         Stream.of("black", "white", "red", "green", "blue", "yellow", "magenta", "cyan",
                 "Black", "White", "Red", "Green", "Blue", "Yellow", "Magenta", "Cyan",
@@ -1139,9 +1139,9 @@ class TestCellFormat {
                 .map(CellFormatPart::getColor)
                 .forEach(Assertions::assertNotNull);
     }
-    
+
     @Test
-    void testIndexedColorsExist() {       
+    void testIndexedColorsExist() {
         // Make sure the standard indexed colors are returned correctly and regardless of case
         for (int i = 0; i < 56; ++i) {
             assertNotNull(CellFormatPart.getColor("Color " + (i + 1)));
@@ -1149,7 +1149,7 @@ class TestCellFormat {
             assertNotNull(CellFormatPart.getColor("color" + (i + 1)));
         }
     }
-    
+
     @Test
     void verifyNamedColors() {
         assertEquals(new Color(0x000000), CellFormatPart.getColor("Black"));
@@ -1161,7 +1161,7 @@ class TestCellFormat {
         assertEquals(new Color(0xFF00FF), CellFormatPart.getColor("Magenta"));
         assertEquals(new Color(0x00FFFF), CellFormatPart.getColor("cyan"));
     }
-    
+
     @Test
     void verifyIndexedColors() {
         assertEquals(CellFormatPart.getColor("Color1"), CellFormatPart.getColor("black"));
@@ -1221,7 +1221,7 @@ class TestCellFormat {
         assertEquals(new Color(0x333399), CellFormatPart.getColor("Color55"));
         assertEquals(new Color(0x333333), CellFormatPart.getColor("Color56"));
     }
-    
+
     @Test
     void testColorsInWorkbook() throws IOException {
         // Create a workbook, row and cell to test with
@@ -1232,22 +1232,22 @@ class TestCellFormat {
             CellFormatResult result;
             CellFormat cf = CellFormat.getInstance(
                     "[GREEN]#,##0.0;[RED]\\(#,##0.0\\);[COLOR22]\"===\";[COLOR 8]\\\"@\\\"");
-            
+
             cell.setCellValue(100.0);
             result = cf.apply(cell);
             assertEquals("100.0", result.text);
             assertEquals(result.textColor, CellFormatPart.getColor("color 4"));
-            
+
             cell.setCellValue(-50.0);
             result = cf.apply(cell);
             assertEquals("(50.0)", result.text);
             assertEquals(result.textColor, CellFormatPart.getColor("red"));
-    
+
             cell.setCellValue("foo");
             result = cf.apply(cell);
             assertEquals("\"foo\"", result.text);
             assertEquals(result.textColor, CellFormatPart.getColor("cyan"));
-            
+
             cell.setCellValue(0.0);
             result = cf.apply(cell);
             assertEquals("===", result.text);
@@ -1321,11 +1321,21 @@ class TestCellFormat {
 
     @Test
     void testApplyNPE() throws ParseException {
-    CellFormat cf1 = CellFormat.getInstance("broken format balsk\"§$%Q\"§$)U");
+        CellFormat cf1 = CellFormat.getInstance("broken format balsk\"§$%Q\"§$)U");
         SimpleDateFormat sdf1 = new SimpleDateFormat("M/d/y", Locale.ROOT);
         sdf1.setTimeZone(TimeZone.getTimeZone("CET"));
         Date date1 = sdf1.parse("01/11/2012");
         assertThrows(IllegalStateException.class,
                 () -> cf1.apply(date1));
     }
+
+    @Test
+    void testApplyNPE2() {
+        String format = "1sm/#]ê?ÿþ/&TD[Á";
+
+        CellFormat cellFormat = CellFormat.getInstance(Locale.ROOT, format);
+
+        assertThrows(IllegalArgumentException.class,
+                () -> cellFormat.apply(Double.parseDouble("-2.0579980172405396E-35")));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


(poi) 02/07: Avoid stackoverflow on formula with many nested names and ranges

Posted by ce...@apache.org.
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 <ce...@apache.org>
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: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


(poi) 03/07: Replace assertions with logs

Posted by ce...@apache.org.
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 06902d305f782cbcad34ae69c21dc8d06eea7ce6
Author: Dominik Stadler <ce...@apache.org>
AuthorDate: Sun Oct 25 21:17:56 2026 +0100

    Replace assertions with logs
    
    Fixes https://issues.oss-fuzz.com/issues/577667825
---
 .../org/apache/poi/hemf/record/emf/HemfFill.java     | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfFill.java b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfFill.java
index 36571c9fbf..7835f3abb1 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfFill.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfFill.java
@@ -810,14 +810,20 @@ public final class HemfFill {
         final int bufferSize = Math.min(dibSize, HemfPicture.getMaxRecordLength());
         final UnsynchronizedByteArrayOutputStream bos = UnsynchronizedByteArrayOutputStream.builder().setBufferSize(bufferSize).get();
         final long cbBmiSrcAct = IOUtils.copy(leis, bos, cbBmi);
-        assert (cbBmiSrcAct == cbBmi);
+        if (cbBmiSrcAct != cbBmi) {
+            LOG.atWarn().log("cbBmiSrcAct != cbBmi: " + cbBmiSrcAct + " and " + cbBmi);
+        }
         leis.skipFully(undefinedSpace2);
         final long cbBitsSrcAct = IOUtils.copy(leis, bos, cbBits);
-        assert (cbBitsSrcAct == cbBits);
+        if (cbBitsSrcAct != cbBits) {
+            LOG.atWarn().log("cbBitsSrcAct != cbBits: " + cbBitsSrcAct + " and " + cbBits);
+        }
 
         final LittleEndianInputStream leisDib = new LittleEndianInputStream(bos.toInputStream());
         final int dibSizeAct = bitmap.init(leisDib, dibSize);
-        assert (dibSizeAct <= dibSize);
+        if (dibSizeAct > dibSize) {
+            LOG.atWarn().log("dibSizeAct > dibSize: " + dibSizeAct + " and " + dibSize);
+        }
         return (long)undefinedSpace1 + cbBmi + undefinedSpace2 + cbBits;
     }
 
@@ -827,10 +833,14 @@ public final class HemfFill {
         // *** RegionDataHeader ***
         // A 32-bit unsigned integer that specifies the size of this object in bytes. This MUST be 0x00000020.
         long rgnHdrSize = leis.readUInt();
-        assert(rgnHdrSize == 0x20);
+        if (rgnHdrSize != 0x20) {
+            LOG.atWarn().log("rgnHdrSize != 0x20: " + rgnHdrSize);
+        }
         // A 32-bit unsigned integer that specifies the region type. This SHOULD be RDH_RECTANGLES (0x00000001)
         long rgnHdrType = leis.readUInt();
-        assert(rgnHdrType == 1);
+        if (rgnHdrType != 1) {
+            LOG.atWarn().log("rgnHdrType != 1: " + rgnHdrType);
+        }
         // A 32-bit unsigned integer that specifies the number of rectangles in this region.
         long rgnCntRect = leis.readUInt();
         // A 32-bit unsigned integer that specifies the size of the buffer of rectangles in bytes.


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org