diff --git a/src/java/org/apache/poi/hssf/record/RowRecord.java b/src/java/org/apache/poi/hssf/record/RowRecord.java index 6fd67c4fa..486a57e2e 100644 --- a/src/java/org/apache/poi/hssf/record/RowRecord.java +++ b/src/java/org/apache/poi/hssf/record/RowRecord.java @@ -35,6 +35,12 @@ public class RowRecord implements Comparable { public final static short sid = 0x208; + + /** The maximum row number that excel can handle (zero bazed) ie 65536 rows is + * max number of rows. + */ + public final static int MAX_ROW_NUMBER = 65535; + //private short field_1_row_number; private int field_1_row_number; private short field_2_first_col; diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java index c84df9032..f8a33427f 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java @@ -84,7 +84,6 @@ public class HSSFRow //protected HSSFRow(Workbook book, Sheet sheet, short rowNum) protected HSSFRow(Workbook book, Sheet sheet, int rowNum) { - this.rowNum = rowNum; cells = new HashMap(10); // new ArrayList(INITIAL_CAPACITY); this.book = book; this.sheet = sheet; @@ -94,7 +93,6 @@ public class HSSFRow row.setLastCol((short) -1); row.setFirstCol((short) -1); - // row.setRowNumber(rowNum); setRowNum(rowNum); } @@ -110,17 +108,12 @@ public class HSSFRow protected HSSFRow(Workbook book, Sheet sheet, RowRecord record) { - //this.rowNum = rowNum; cells = new HashMap(); // ArrayList(INITIAL_CAPACITY); this.book = book; this.sheet = sheet; row = record; - // row.setHeight(record.getHeight()); - // row.setRowNumber(rowNum); setRowNum(record.getRowNumber()); - -// addColumns(book, sheet, record); } /** @@ -206,11 +199,14 @@ public class HSSFRow /** * set the row number of this row. * @param rowNum the row number (0-based) + * @throws IndexOutOfBoundsException if the row number is not within the range 0-65535. */ //public void setRowNum(short rowNum) public void setRowNum(int rowNum) { + if ((rowNum < 0) || (rowNum > RowRecord.MAX_ROW_NUMBER)) + throw new IndexOutOfBoundsException("Row number must be between 0 and "+RowRecord.MAX_ROW_NUMBER+", was <"+rowNum+">"); this.rowNum = rowNum; if (row != null) { diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index c381e1f1a..8c3eb00d5 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -242,10 +242,12 @@ public class HSSFSheet int rownum = lastrow - 1; HSSFRow r = getRow(rownum); - while (r == null && rownum >= 0) + while (r == null && rownum > 0) { r = getRow(--rownum); } + if (r == null) + return -1; return rownum; } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java index 8258b8b86..c82956db6 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java @@ -99,4 +99,32 @@ public class TestHSSFRow } + + public void testRowBounds() + throws Exception + { + HSSFWorkbook workbook = new HSSFWorkbook(); + HSSFSheet sheet = workbook.createSheet(); + //Test low row bound + HSSFRow row = sheet.createRow( (short) 0); + //Test low row bound exception + boolean caughtException = false; + try { + row = sheet.createRow(-1); + } catch (IndexOutOfBoundsException ex) { + caughtException = true; + } + assertTrue(caughtException); + //Test high row bound + row = sheet.createRow(65535); + //Test high row bound exception + caughtException = false; + try { + row = sheet.createRow(65536); + } catch (IndexOutOfBoundsException ex) { + caughtException = true; + } + assertTrue(caughtException); + } + }