Bug 30008 - ArrayIndexOutOfBounds exception in UnknownRecord.java and Nullpointer Exception in HSSFSheet.java
Summary: ArrayIndexOutOfBounds exception in UnknownRecord.java and Nullpointer Excepti...
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 2.5-FINAL
Hardware: All All
: P3 normal with 10 votes (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2004-07-09 18:33 UTC by christian.schuhegger
Modified: 2005-08-23 16:23 UTC (History)
0 users

just load this up in hssf and it will blow up without the suggested fixes (7.00 KB, application/octet-stream)
2005-08-23 07:54 UTC, jameison martin

Note You need to log in before you can comment on or make changes to this bug.
Description christian.schuhegger 2004-07-09 18:33:47 UTC
in the following patch you can see that i added a check for an array index out
of bounds and a check for a null pointer. 

it is quite possible that the two methods i fixed state in their pre-conditions
that something like that should not happen, but then the bug is somewhere else
because these pre-conditions were violated.

Index: src/java/org/apache/poi/hssf/record/UnknownRecord.java
RCS file:
retrieving revision 1.9
diff -c -r1.9 UnknownRecord.java
*** src/java/org/apache/poi/hssf/record/UnknownRecord.java	9 Apr 2004 13:05:20
-0000	1.9
--- src/java/org/apache/poi/hssf/record/UnknownRecord.java	30 Jun 2004 17:31:44
*** 62,68 ****
          sid     = id;
          thedata = new byte[size];
!         System.arraycopy(data, offset, thedata, 0, size);
--- 62,69 ----
          sid     = id;
          thedata = new byte[size];
!         int len = Math.min(size, data.length - offset);
!         System.arraycopy(data, offset, thedata, 0, len);
Index: src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
RCS file:
retrieving revision 1.27
diff -c -r1.27 HSSFSheet.java
*** src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java	9 Apr 2004 13:05:24
-0000	1.27
--- src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java	30 Jun 2004 17:31:44 -0000
*** 308,313 ****
--- 308,315 ----
      public HSSFRow getRow(int rownum)
+         if(null == this.sheet.getRow(rownum))
+             return null;
          HSSFRow retval = new HSSFRow(book, sheet, this.sheet.getRow(rownum));
          return retval;
Comment 1 jameison martin 2005-04-08 19:50:20 UTC
without the change to UnknowRecord.java (i hit this and basically coded the
same) i cannot use HSSF to read spreadsheets with pivot tables (and probably
pivot charts).  so this is a pretty important change to get back to the mothership.
Comment 2 amit 2005-05-05 01:55:08 UTC
The Excel template when read using HSSF throws the arrayIndexException. Just 
for kicks when I altered the code in UnknownRecord (Line 62):
from System.arraycopy(data, offset, thedata, 0, size); to

try {
            System.arraycopy(data, offset, thedata, 0, size);
        } catch (Exception e) {
            // Hack to clear the bug
            thedata = new byte[data.length - offset];
            System.arraycopy(data, offset, thedata, 0, data.length - offset);

things went ahead fine. But I guess this may not be the correct solution even 
though it worked for me. I can provide a test template if it is of any help 
Comment 3 Jason Height 2005-08-23 04:55:43 UTC
I believe that this is an invalid fix. If someone has an excel file that
generates this exception please REOPEN this bug and attach the file. Sorry cant
do much with this limited information.

Comment 4 jameison martin 2005-08-23 07:54:03 UTC
Created attachment 16158 [details]
just load this up in hssf and it will blow up without the suggested fixes
Comment 5 jameison martin 2005-08-23 07:55:42 UTC
try reading in the attached .xls. unless something has changed it will blow up
as described without the fixes described.

is that still too 'limited' or do you need a sample program that will invoke it
as well?
Comment 6 Jason Height 2005-08-23 08:00:48 UTC
Using HEAD and the latest CVS snapshot the following works for me:

      HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(new File(
      wb.write(new FileOutputStream(new File("c:/TestPivot_out.xls")));

I can even open and use the out file in excel!!

Jameison can you please confirm that the above would have previously failed.

Comment 7 Jason Height 2005-08-24 00:23:24 UTC
I believe that this has been fixed in HEAD. Based on the earlier comments it
would explode on read, now it will read and write the workbook without problems.