Bug 53404 - IllegalArgumentException: calculated end index is out of allowable range when using HSSFWorkbook#write()
Summary: IllegalArgumentException: calculated end index is out of allowable range when...
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.8-FINAL
Hardware: All All
: P2 critical (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-06-12 20:56 UTC by Edward Q. Bridges
Modified: 2012-10-25 15:32 UTC (History)
0 users



Attachments
example test program (2.80 KB, application/x-gzip)
2012-06-12 20:56 UTC, Edward Q. Bridges
Details
sample source xls file. (16.00 KB, application/octet-stream)
2012-06-13 16:17 UTC, Edward Q. Bridges
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Edward Q. Bridges 2012-06-12 20:56:45 UTC
Created attachment 28922 [details]
example test program

When writing to a file using HSSFWorkbook.write() the write fails with the below stack trace, and wipes out any data existing in the file.

This functionality was introduced in version 3.7b1, and has been confirmed to exist in the most recent production version as well (v3.8).

I've attached a test program that reliably reproduces this.  To use it unzip the attachment and review the README file.

-----------------------

Exception in thread "main" java.lang.IllegalArgumentException: calculated end index (29228) is out of allowable range (29224..29226)
	at org.apache.poi.util.LittleEndianByteArrayOutputStream.<init>(LittleEndianByteArrayOutputStream.java:41)
	at org.apache.poi.hssf.record.StandardRecord.serialize(StandardRecord.java:38)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook$SheetRecordCollector.serialize(HSSFWorkbook.java:1215)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.getBytes(HSSFWorkbook.java:1261)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1160)
	at poifseditor.PoifsEditor.addSomeRows(PoifsEditor.java:45)
	at poifseditor.PoifsEditor.main(PoifsEditor.java:62)
Comment 1 Nick Burch 2012-06-13 15:43:01 UTC
Can you narrow down which record is triggering the problem? Does the file pass the Microsoft Binary File Format Validator before being loaded in POI?
Comment 2 Edward Q. Bridges 2012-06-13 16:16:12 UTC
I don't have access to a Windows machine currently to run the Microsoft Binary File Format Validator.  I've attached a copy of the excel file that is generated, so perhaps someone who is capable of running the validator can do so.

I did not try to narrow down to which record is causing it because all of the records are identical -- they are all inserted by the "poifs-writer" module in the attached test harness.
Comment 3 Edward Q. Bridges 2012-06-13 16:17:08 UTC
Created attachment 28933 [details]
sample source xls file.

this sample file was generated by the poifs-writer module of the attached test program.
Comment 4 Yegor Kozlov 2012-06-13 18:03:03 UTC
Just to make it clear:

 POI-3.6 (write) + POI-3.6 (edit) is success.

 POI-3.6 (write) + POI-3.7-beta1 (edit) fails. 

what is the result of using POI-3.8 (write) + POI-3.8 (edit) ?  

POI-3.6 (write) + POI-3.8 (edit) ?

Yegor
Comment 5 Edward Q. Bridges 2012-06-13 18:19:20 UTC
POI-3.6 (write) + POI-3.6 (edit) is success.

--> confirmed success

POI-3.6 (write) + POI-3.7-beta1 (edit) fails. 

--> confirmed fails

POI-3.8 (write) + POI-3.8 (edit)

--> confirmed successful

POI-3.6 (write) + POI-3.8 (edit)

--> confirmed fails
Comment 6 Edward Q. Bridges 2012-06-13 18:20:55 UTC
FYI, using the attached test harness it's easy to swap versions by changing the version numbers in poifs-writer/pom.xml and poifs-editor/pom.xml and then running 'mvn -U clean package'  before running 'runTest.sh'
Comment 7 Yegor Kozlov 2012-06-14 07:01:26 UTC
I don't see a bug here. POI-3.6 is more than 2 years old and many bugs have been fixed since then. 

can you try a simple workaround: if poifs-writer fails with IllegalArgumentException which indicates that the file was created by a old version of POI then write it to a byte array and read back. Something like this:

 wb = new HSSFWorkbook("created by POI-3.6.xls");
 try {
   // do stuff
 } catch (IllegalArgumentException e){
  
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   wb.write(out);
   wb = new HSSFWorkbook(new ByteArrayOutputStream(out.toByteArray()));

   // do stuff
 }

Let us know if it worked for you.

Cheers,
Yegor
Comment 8 Edward Q. Bridges 2012-06-14 17:30:32 UTC
i've been able to address the issue with what you describe (n.b.: with a fix for the typo to use ByteArrayInputStream as an argument to the HSSFWorksheet ctor).

is there a more reliable way to determine the version other than by exception handling?  e.g. is there a metadata property somewhere that would reflect this?

i've toyed with the idea of using getImplementationVersion() (as the test program uses for printing)...

all this said, i'm surprised by the reluctance to try to address this issue.  as it happens we have several thousand legacy spreadsheets in pre-3.6 format.  can you be more specific why it won't be fixed?
Comment 9 Yegor Kozlov 2012-06-15 07:36:44 UTC
(In reply to comment #8)
> i've been able to address the issue with what you describe (n.b.: with a fix
> for the typo to use ByteArrayInputStream as an argument to the HSSFWorksheet
> ctor).
> 
> is there a more reliable way to determine the version other than by
> exception handling?  e.g. is there a metadata property somewhere that would
> reflect this?
> 

No, HSSF does not write version info in the output.

> i've toyed with the idea of using getImplementationVersion() (as the test
> program uses for printing)...
> 
> all this said, i'm surprised by the reluctance to try to address this issue.
> as it happens we have several thousand legacy spreadsheets in pre-3.6
> format.  can you be more specific why it won't be fixed?

It wasn't clear that you have several thousand legacy spreadsheets in pre-3.6 format. I thought tha tall you need is to upgrade both writer and editor parts to the latest and greatest POI-3.8. 

I re-opened the bug. Hope to have it fixed by POI-3.9 (late 2012). 

Yegor
Comment 10 Edward Q. Bridges 2012-10-05 14:49:32 UTC
Is this still expected to be released in late 2012?  If so, what's the target milestone for it?  Thanks
Comment 11 Yegor Kozlov 2012-10-08 12:46:01 UTC
This bug is open which means we plan to address it in the future. 

POI is a volunteer project. If this bug is important for you, please do work on it and submit a patch. Otherwise please wait. 

We plan to release POI-3.9 in late 2012, hope this problem will be fixed by then.

Yegor

(In reply to comment #10)
> Is this still expected to be released in late 2012?  If so, what's the
> target milestone for it?  Thanks
Comment 12 Yegor Kozlov 2012-10-25 15:15:07 UTC
Should be fixed in r1402186. Please try with the latest build from trunk.

Yegor
Comment 13 Edward Q. Bridges 2012-10-25 15:28:31 UTC
Thanks much.  Will this be going out with the next release?  Or will it need to be scheduled?
Comment 14 Yegor Kozlov 2012-10-25 15:32:48 UTC
Yes, the fix will be included in POI-3.9. I plan to roll it some time Dec 2012.

(In reply to comment #13)
> Thanks much.  Will this be going out with the next release?  Or will it need
> to be scheduled?