Patch from bug #55650 from Andrej - Avoid AIOOBE if a non-existant Xfs is requested for a styleAvoid AIOOBE if a non-existant Xfs is requested for a style

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1534818 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2013-10-22 22:04:16 +00:00
parent a32a1cf538
commit 5ac455fd72
2 changed files with 154 additions and 136 deletions

View File

@ -20,17 +20,23 @@ package org.apache.poi.xssf.model;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.FontFamily;
import org.apache.poi.ss.usermodel.FontScheme;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
@ -49,8 +55,6 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
/**
@ -302,7 +306,7 @@ public class StylesTable extends POIXMLDocumentPart {
}
public CTXf getCellStyleXfAt(int idx) {
return styleXfs.get(idx);
return idx < styleXfs.size() ? styleXfs.get(idx) : null;
}
public int putCellStyleXf(CTXf cellStyleXf) {
styleXfs.add(cellStyleXf);

View File

@ -836,4 +836,18 @@ public class TestXSSFCellStyle extends TestCase {
assertNull(style.getStyleXf());
}
/**
* Avoid ArrayIndexOutOfBoundsException when getting cell style
* in a workbook that has an empty xf table.
*/
public void testBug55650() {
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("52348.xlsx");
StylesTable st = workbook.getStylesSource();
assertEquals(0, st._getStyleXfsSize());
// no exception at this point
XSSFCellStyle style = workbook.getSheetAt(0).getRow(0).getCell(0).getCellStyle();
assertNull(style.getStyleXf());
}
}