Browse Source

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
pull/624/head
Dominik Stadler 1 month ago
parent
commit
9453fa908a

+ 34
- 0
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java View File

import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;


import java.io.IOException; import java.io.IOException;
import java.util.Date;


import org.apache.poi.ss.formula.ConditionalFormattingEvaluator; import org.apache.poi.ss.formula.ConditionalFormattingEvaluator;
import org.apache.poi.ss.formula.WorkbookEvaluatorProvider; import org.apache.poi.ss.formula.WorkbookEvaluatorProvider;
assertEquals("6.75", formatter.formatCellValue(d1)); 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));
}
}
} }

+ 1
- 1
poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java View File

sdf.setTimeZone(LocaleUtil.getUserTimeZone()); sdf.setTimeZone(LocaleUtil.getUserTimeZone());
dateFormat = sdf; dateFormat = sdf;
} else { } else {
dateFormat = defaultNumFormat;
dateFormat = defaultDateformat;
} }
} }
synchronized (dateFormat) { synchronized (dateFormat) {

+ 1
- 0
poi/src/test/java/org/apache/poi/hssf/dev/TestBiffViewer.java View File

excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-5889658057523200.xls", IndexOutOfBoundsException.class); 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-5175219985448960.xls", IndexOutOfBoundsException.class);
excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-6137883240824832.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; return excludes;
} }

+ 1
- 0
poi/src/test/java/org/apache/poi/hssf/dev/TestRecordLister.java View File

excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-5889658057523200.xls", IndexOutOfBoundsException.class); 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-5175219985448960.xls", RecordFormatException.class);
excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-6137883240824832.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; return excludes;
} }

+ 34
- 0
poi/src/test/java/org/apache/poi/ss/usermodel/TestDataFormatter.java View File

import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;


import javax.swing.text.DateFormatter;

import org.apache.poi.POITestCase; import org.apache.poi.POITestCase;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
return true; 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));
}
}
} }

BIN
test-data/spreadsheet/clusterfuzz-testcase-minimized-POIHSSFFuzzer-6483562584932352.xls View File


BIN
test-data/spreadsheet/stress.xls View File


Loading…
Cancel
Save