From 7e7bde2595efc8b52531708c03326455a126ce80 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sun, 12 Feb 2012 10:44:17 +0000 Subject: [PATCH] validate row and column indexes in SXSSF git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1243232 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../org/apache/poi/xssf/streaming/SXSSFRow.java | 16 ++++++++++++++++ .../apache/poi/xssf/streaming/SXSSFSheet.java | 7 +++++++ 3 files changed, 24 insertions(+) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index ea93e88316..e4a391cd11 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + validate row number and column index in SXSSF when creating new rows / cells 51498 - fixed evaluation of blank cells in COUNTIF 52576 - support changing external file references in HSSFWorkbook 49896 - support external references in FormulaRenderer diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java index 52735db7fe..341f56b833 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java @@ -20,6 +20,7 @@ package org.apache.poi.xssf.streaming; import java.util.Iterator; import java.util.NoSuchElementException; +import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; @@ -88,6 +89,8 @@ public class SXSSFRow implements Row */ public Cell createCell(int column, int type) { + checkBounds(column); + if(column>=_cells.length) { SXSSFCell[] newCells=new SXSSFCell[Math.max(column+1,_cells.length*2)]; @@ -99,6 +102,19 @@ public class SXSSFRow implements Row return _cells[column]; } + /** + * @throws RuntimeException if the bounds are exceeded. + */ + private static void checkBounds(int cellIndex) { + SpreadsheetVersion v = SpreadsheetVersion.EXCEL2007; + int maxcol = SpreadsheetVersion.EXCEL2007.getLastColumnIndex(); + if (cellIndex < 0 || cellIndex > maxcol) { + throw new IllegalArgumentException("Invalid column index (" + cellIndex + + "). Allowable column range for " + v.name() + " is (0.." + + maxcol + ") or ('A'..'" + v.getLastColumnName() + "')"); + } + } + /** * Remove the Cell from this row. * diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java index c453203ac9..5e2aba7b0a 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java @@ -22,6 +22,7 @@ import java.util.Iterator; import java.util.TreeMap; import java.util.Map; +import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.SheetUtil; @@ -84,6 +85,12 @@ public class SXSSFSheet implements Sheet, Cloneable */ public Row createRow(int rownum) { + int maxrow = SpreadsheetVersion.EXCEL2007.getLastRowIndex(); + if (rownum < 0 || rownum > maxrow) { + throw new IllegalArgumentException("Invalid row number (" + rownum + + ") outside allowable range (0.." + maxrow + ")"); + } + //Make the initial allocation as big as the row above. Row previousRow=rownum>0?getRow(rownum-1):null; int initialAllocationSize=0;