Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#465] Avoid NPE with missing 'r' attribute #514

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class RowSpliterator implements Spliterator<Row> {
private final HashMap<Integer, BaseFormulaCell> sharedFormula = new HashMap<>();
private final HashMap<CellRangeAddress, String> arrayFormula = new HashMap<>();
private int rowCapacity = 16;
private int trackedRowIndex = 0;

public RowSpliterator(ReadableWorkbook workbook, InputStream inputStream) throws XMLStreamException {
this.workbook = workbook;
Expand Down Expand Up @@ -82,7 +83,10 @@ private Row next() throws XMLStreamException {
if (!"row".equals(r.getLocalName())) {
throw new NoSuchElementException();
}
int rowIndex = r.getIntAttribute("r");

int trackedColIndex = 0;
int rowIndex = getRowIndexWithFallback(++trackedRowIndex);

List<Cell> cells = new ArrayList<>(rowCapacity);
int physicalCellCount = 0;

Expand All @@ -91,7 +95,7 @@ private Row next() throws XMLStreamException {
break;
}

Cell cell = parseCell();
Cell cell = parseCell(trackedColIndex++);
CellAddress addr = cell.getAddress();
ensureSize(cells, addr.getColumn() + 1);

Expand All @@ -102,9 +106,20 @@ private Row next() throws XMLStreamException {
return new Row(rowIndex, physicalCellCount, cells);
}

private Cell parseCell() throws XMLStreamException {
String cellRef = r.getAttribute("r");
CellAddress addr = new CellAddress(cellRef);
private int getRowIndexWithFallback(int fallbackRowIndex) {
Integer rowIndexOrNull = r.getIntAttribute("r");
return rowIndexOrNull != null ? rowIndexOrNull : fallbackRowIndex;
}

private CellAddress getCellAddressWithFallback(int trackedColIndex) {
String cellRefOrNull = r.getAttribute("r");
return cellRefOrNull != null ?
new CellAddress(cellRefOrNull) :
new CellAddress(trackedRowIndex, trackedColIndex);
}

private Cell parseCell(int trackedColIndex) throws XMLStreamException {
CellAddress addr = getCellAddressWithFallback(trackedColIndex);
String type = r.getOptionalAttribute("t").orElse("n");
String styleString = r.getAttribute("s");
String formatId = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -155,6 +156,7 @@ private List<RowDates> readUsingFastExcel() throws IOException {
"/xlsx/write.xlsx",
"/xlsx/issue143.xlsx",
"/xlsx/issue161.xlsx",
"/xlsx/issue514.xlsx",
// "/xlsx/xlsx-stream-d-date-cell.xlsx"
})
void testFile(String file) {
Expand Down
Binary file not shown.