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 72047248ad86c5edebdfb1ccca2ea44283101873 Author: Dominik Stadler <[email protected]> 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 a7c84ebf3d..84f73ea785 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 @@ -454,6 +454,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(); @@ -549,6 +553,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 c48b24efed..e1ebd0ca3c 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 @@ -1129,7 +1129,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", @@ -1137,9 +1137,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))); @@ -1147,7 +1147,7 @@ class TestCellFormat { assertNotNull(CellFormatPart.getColor("color" + (i + 1))); } } - + @Test void verifyNamedColors() { assertEquals(new Color(0x000000), CellFormatPart.getColor("Black")); @@ -1159,7 +1159,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")); @@ -1219,7 +1219,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 @@ -1230,22 +1230,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); @@ -1319,11 +1319,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: [email protected] For additional commands, e-mail: [email protected]
