]> source.dussan.org Git - poi.git/commitdiff
bug 60343, return null if index is out of bounds in StylesTable's getStyleAt(idx)
authorTim Allison <tallison@apache.org>
Tue, 22 Nov 2016 14:47:45 +0000 (14:47 +0000)
committerTim Allison <tallison@apache.org>
Tue, 22 Nov 2016 14:47:45 +0000 (14:47 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1770848 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java

index 18b88203f4e17e670d0f5e88545c89417f4d89f3..bc85de49d6fa6b32cbe5cf0f6e54f5ec40f88f39 100644 (file)
@@ -403,9 +403,18 @@ public class StylesTable extends POIXMLDocumentPart {
         return putFont(font, false);
     }
 
+    /**
+     *
+     * @param idx style index
+     * @return XSSFCellStyle or null if idx is out of bounds for xfs array
+     */
     public XSSFCellStyle getStyleAt(int idx) {
         int styleXfId = 0;
 
+        if (idx < 0 || idx >= xfs.size()) {
+            //BUG-60343
+            return null;
+        }
         // 0 is the empty default
         if(xfs.get(idx).getXfId() > 0) {
             styleXfId = (int) xfs.get(idx).getXfId();
index 7f832c4d9cb77964d69a1631681ca77be32795a5..b6360a6bb1fab5660e9489b7856e90881fff2cc5 100644 (file)
@@ -463,10 +463,8 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
         // Should have one style
         assertEquals(1, wb1.getNumCellStyles());
         wb1.getCellStyleAt((short)0);
-        try {
-            wb1.getCellStyleAt((short)1);
-            fail("Shouldn't be able to get style at 1 that doesn't exist");
-        } catch(IndexOutOfBoundsException e) {}
+        assertNull("Shouldn't be able to get style at 0 that doesn't exist",
+                wb1.getCellStyleAt((short)1));
 
         // Add another one
         CellStyle cs = wb1.createCellStyle();
@@ -476,20 +474,16 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
         assertEquals(2, wb1.getNumCellStyles());
         wb1.getCellStyleAt((short)0);
         wb1.getCellStyleAt((short)1);
-        try {
-            wb1.getCellStyleAt((short)2);
-            fail("Shouldn't be able to get style at 2 that doesn't exist");
-        } catch(IndexOutOfBoundsException e) {}
+        assertNull("Shouldn't be able to get style at 2 that doesn't exist",
+                wb1.getCellStyleAt((short)2));
 
         // Save and reload
         XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb1);
         assertEquals(2, nwb.getNumCellStyles());
         nwb.getCellStyleAt((short)0);
         nwb.getCellStyleAt((short)1);
-        try {
-            nwb.getCellStyleAt((short)2);
-            fail("Shouldn't be able to get style at 2 that doesn't exist");
-        } catch(IndexOutOfBoundsException e) {}
+        assertNull("Shouldn't be able to get style at 2 that doesn't exist",
+                nwb.getCellStyleAt((short)2));
 
         // Now with an existing file
         XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
@@ -497,10 +491,8 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
         wb2.getCellStyleAt((short)0);
         wb2.getCellStyleAt((short)1);
         wb2.getCellStyleAt((short)2);
-        try {
-            wb2.getCellStyleAt((short)3);
-            fail("Shouldn't be able to get style at 3 that doesn't exist");
-        } catch(IndexOutOfBoundsException e) {}
+        assertNull("Shouldn't be able to get style at 3 that doesn't exist",
+                wb2.getCellStyleAt((short)3));
 
         wb2.close();
         wb1.close();