From 9453fa908ae9a0c9a602725f4d5e1d6994b2b3f3 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Wed, 17 Apr 2024 18:15:46 +0000 Subject: [PATCH] Bug 66425: Avoid exceptions found via poi-fuzz Use correct default date-format, add some tests Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=66381 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1917070 13f79535-47bb-0310-9956-ffa450edef68 --- .../xssf/usermodel/TestXSSFDataFormat.java | 34 ++++++++++++++++++ .../poi/ss/usermodel/DataFormatter.java | 2 +- .../apache/poi/hssf/dev/TestBiffViewer.java | 1 + .../apache/poi/hssf/dev/TestRecordLister.java | 1 + .../poi/ss/usermodel/TestDataFormatter.java | 34 ++++++++++++++++++ ...nimized-POIHSSFFuzzer-6483562584932352.xls | Bin 0 -> 13134 bytes test-data/spreadsheet/stress.xls | Bin 64512 -> 65024 bytes 7 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 test-data/spreadsheet/clusterfuzz-testcase-minimized-POIHSSFFuzzer-6483562584932352.xls diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java index fe4aa0a00a..bc6ddf0c62 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java @@ -21,6 +21,7 @@ import static org.apache.poi.xssf.XSSFTestDataSamples.openSampleWorkbook; import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; +import java.util.Date; import org.apache.poi.ss.formula.ConditionalFormattingEvaluator; import org.apache.poi.ss.formula.WorkbookEvaluatorProvider; @@ -120,4 +121,37 @@ public final class TestXSSFDataFormat extends BaseTestDataFormat { assertEquals("6.75", formatter.formatCellValue(d1)); } } + + @Test + public void testFormatCellValue() throws IOException { + DataFormatter df = new DataFormatter(); + + assertEquals("", df.formatCellValue(null)); + + try (Workbook wb = new XSSFWorkbook()) { + Cell cell = wb.createSheet("test").createRow(0).createCell(0); + assertEquals("", df.formatCellValue(cell)); + + cell.setCellValue(123); + assertEquals("123", df.formatCellValue(cell)); + + cell.setCellValue(new Date(234092383)); + assertEquals("25571.75107", df.formatCellValue(cell)); + + cell.setCellValue("abcdefgh"); + assertEquals("abcdefgh", df.formatCellValue(cell)); + + cell.setCellValue(true); + assertEquals("TRUE", df.formatCellValue(cell)); + + CellStyle cellStyle = wb.createCellStyle(); + cellStyle.setDataFormat((short)14); + cell.setCellStyle(cellStyle); + cell.setCellValue(new Date(234092383)); + assertEquals("1/3/70", df.formatCellValue(cell)); + + cellStyle.setDataFormat((short)9999); + assertEquals("25571.751069247686", df.formatCellValue(cell)); + } + } } diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java b/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java index 35e5ff55d6..f94e6893f7 100644 --- a/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java @@ -922,7 +922,7 @@ public class DataFormatter { sdf.setTimeZone(LocaleUtil.getUserTimeZone()); dateFormat = sdf; } else { - dateFormat = defaultNumFormat; + dateFormat = defaultDateformat; } } synchronized (dateFormat) { diff --git a/poi/src/test/java/org/apache/poi/hssf/dev/TestBiffViewer.java b/poi/src/test/java/org/apache/poi/hssf/dev/TestBiffViewer.java index 491d68d159..d23f1dbece 100644 --- a/poi/src/test/java/org/apache/poi/hssf/dev/TestBiffViewer.java +++ b/poi/src/test/java/org/apache/poi/hssf/dev/TestBiffViewer.java @@ -46,6 +46,7 @@ class TestBiffViewer extends BaseTestIteratingXLS { excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-5889658057523200.xls", IndexOutOfBoundsException.class); excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-5175219985448960.xls", IndexOutOfBoundsException.class); excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-6137883240824832.xls", IndexOutOfBoundsException.class); + excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-6483562584932352.xls", IndexOutOfBoundsException.class); return excludes; } diff --git a/poi/src/test/java/org/apache/poi/hssf/dev/TestRecordLister.java b/poi/src/test/java/org/apache/poi/hssf/dev/TestRecordLister.java index 7f1c1f4561..34359e1d2e 100644 --- a/poi/src/test/java/org/apache/poi/hssf/dev/TestRecordLister.java +++ b/poi/src/test/java/org/apache/poi/hssf/dev/TestRecordLister.java @@ -49,6 +49,7 @@ class TestRecordLister extends BaseTestIteratingXLS { excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-5889658057523200.xls", IndexOutOfBoundsException.class); excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-5175219985448960.xls", RecordFormatException.class); excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-6137883240824832.xls", RecordFormatException.class); + excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-6483562584932352.xls", RecordFormatException.class); return excludes; } diff --git a/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java b/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java index 0d6c1af5b9..31cfa031b0 100644 --- a/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java +++ b/poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java @@ -35,6 +35,8 @@ import java.util.Locale; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; +import javax.swing.text.DateFormatter; + import org.apache.poi.POITestCase; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.usermodel.HSSFWorkbook; @@ -1162,4 +1164,36 @@ class TestDataFormatter { return true; } + @Test + public void testFormatCellValue() throws IOException { + DataFormatter df = new DataFormatter(); + + assertEquals("", df.formatCellValue(null)); + + try (Workbook wb = new HSSFWorkbook()) { + Cell cell = wb.createSheet("test").createRow(0).createCell(0); + assertEquals("", df.formatCellValue(cell)); + + cell.setCellValue(123); + assertEquals("123", df.formatCellValue(cell)); + + cell.setCellValue(new Date(234092383)); + assertEquals("25571.75107", df.formatCellValue(cell)); + + cell.setCellValue("abcdefgh"); + assertEquals("abcdefgh", df.formatCellValue(cell)); + + cell.setCellValue(true); + assertEquals("TRUE", df.formatCellValue(cell)); + + CellStyle cellStyle = wb.createCellStyle(); + cellStyle.setDataFormat((short)14); + cell.setCellStyle(cellStyle); + cell.setCellValue(new Date(234092383)); + assertEquals("1/3/70", df.formatCellValue(cell)); + + cellStyle.setDataFormat((short)9999); + assertEquals("25571.751069247686", df.formatCellValue(cell)); + } + } } diff --git a/test-data/spreadsheet/clusterfuzz-testcase-minimized-POIHSSFFuzzer-6483562584932352.xls b/test-data/spreadsheet/clusterfuzz-testcase-minimized-POIHSSFFuzzer-6483562584932352.xls new file mode 100644 index 0000000000000000000000000000000000000000..f2ec6f113b0cf25e0fd0ca85fe39dd92cb0c5de2 GIT binary patch literal 13134 zcmeI(4P2Gg9S87pFIT+Y6akHljIL;isH9L-SU_0R*d(Jx4iSY@ArSsdER** z&Uy637d-ZEeB67V>N15X7u9a>r(8Sa4f0(FCU>Qb@|LnO|X@Rm4F zVyMLV5&p@g5rNC|(5Q4#?Xqb0^jjFkwKxJY80#CVAyiC~F~ zB`%Q&k+@XiGKtG2LM0|hTp@9#M3_XlM1;gu5|I)UC9amZMj}e$T8Zl zdxn~=lCd4LPqKvW#h%J&=(xk(Yjn@_mDDr2-Sb~J^Hta1Wj*@x?KBPAx!w5_u=B~- zZ+Y6ez0<~>68@JX(y{H2^ZO$|&fVr-_cP0L#kG%WAG*8mjtKcGx0QYJx5j4E4*bQ@ zELUKHny*YsrDe@rWc{Y5O8e)>H=Cv#jI#bKWo}Rp$o9C&6HaI3J|y!&m_LR(PdREz z$w6hbsN0POc_4Z&GOBFRV#tfig1oiHUF>5TYg9XJR;hId@;Sht_G0 zmJ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