]> source.dussan.org Git - poi.git/commitdiff
Bug 66425: Avoid exceptions found via poi-fuzz
authorDominik Stadler <centic@apache.org>
Wed, 17 Apr 2024 18:15:46 +0000 (18:15 +0000)
committerDominik Stadler <centic@apache.org>
Wed, 17 Apr 2024 18:15:46 +0000 (18:15 +0000)
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

poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java
poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java
poi/src/test/java/org/apache/poi/hssf/dev/TestBiffViewer.java
poi/src/test/java/org/apache/poi/hssf/dev/TestRecordLister.java
poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java
test-data/spreadsheet/clusterfuzz-testcase-minimized-POIHSSFFuzzer-6483562584932352.xls [new file with mode: 0644]
test-data/spreadsheet/stress.xls

index fe4aa0a00ab9657a63a9e959a9df169c5b0cd5ea..bc6ddf0c62293cc8d189f13dbb9a59b0b878e53e 100644 (file)
@@ -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));
+        }
+    }
 }
index 35e5ff55d6d64acac5b3092c138df819cfbaba03..f94e6893f7a4ebc1f2d7c6759ac9427620a8ed40 100644 (file)
@@ -922,7 +922,7 @@ public class DataFormatter {
                 sdf.setTimeZone(LocaleUtil.getUserTimeZone());
                 dateFormat = sdf;
             } else {
-                dateFormat = defaultNumFormat;
+                dateFormat = defaultDateformat;
             }
         }
         synchronized (dateFormat) {
index 491d68d1598d4e829debf11e72d66b715aa9ddca..d23f1dbecef5d24f54cfd6e22ba2256b41efc33d 100644 (file)
@@ -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;
     }
index 7f1c1f456163a1a76e9a646b458b23a4b54d59f5..34359e1d2e81db24c7ca0c9886a2296f631ccd52 100644 (file)
@@ -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;
     }
index 0d6c1af5b93f474072cce0ce5c0476d003cecbcc..31cfa031b00568c1a391669388bb12522aad1f77 100644 (file)
@@ -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 (file)
index 0000000..f2ec6f1
Binary files /dev/null and b/test-data/spreadsheet/clusterfuzz-testcase-minimized-POIHSSFFuzzer-6483562584932352.xls differ
index 98ce7f52d696d9a3b2170da68b9f893a409b4c8d..0c9f7271897f76f3fa7dc9d65ec477544d0af359 100644 (file)
Binary files a/test-data/spreadsheet/stress.xls and b/test-data/spreadsheet/stress.xls differ