Bug 49936 - [Patch] java.lang.StringIndexOutOfBoundsException in org.apache.poi.hwpf.usermodel.HeaderStories
Summary: [Patch] java.lang.StringIndexOutOfBoundsException in org.apache.poi.hwpf.user...
Alias: None
Product: POI
Classification: Unclassified
Component: HWPF (show other bugs)
Version: 3.7-dev
Hardware: All All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2010-09-15 20:14 UTC by ssmeets
Modified: 2010-09-17 10:14 UTC (History)
0 users

patch (1.62 KB, text/plain)
2010-09-15 20:14 UTC, ssmeets
WordDoc for Junit test (60.00 KB, application/msword)
2010-09-16 13:33 UTC, ssmeets
WordDoc for Junit Test 2 (92.50 KB, application/msword)
2010-09-16 13:34 UTC, ssmeets

Note You need to log in before you can comment on or make changes to this bug.
Description ssmeets 2010-09-15 20:14:21 UTC
Created attachment 26030 [details]


For some reason I have some Word documents that have a header/footer that causes the following StringIndexOutOfBoundsException:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
	at java.lang.String.substring(String.java:1937)
	at org.apache.poi.hwpf.usermodel.HeaderStories.getAt(HeaderStories.java:162)
	at org.apache.poi.hwpf.usermodel.HeaderStories.getFirstHeader(HeaderStories.java:87)
	at org.apache.poi.hwpf.extractor.WordExtractor.getHeaderText(WordExtractor.java:177)
	at com.ravn.test.poi.MSDocTester.parse(MSDocTester.java:28)
	at com.ravn.test.Tester.main(Tester.java:17)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)

After investigation it turns out that prop.getEnd() was smaller then prop.getStart() in one case and in the other case both prop.getStart() and prop.getEnd() were bigger then headerStories.text().length. Therefore I implemented two additional checks in the HeaderStories.java in the getAt method, see attached patch. Could this be added to the next release if approved?

Thanks in advance,
Comment 1 Nick Burch 2010-09-16 12:08:43 UTC
Any chance you could upload a problem word document that currently breaks? This can then be used as the basis of a unit test to be committed with this patch
Comment 2 ssmeets 2010-09-16 13:33:42 UTC
Created attachment 26033 [details]
WordDoc for Junit test
Comment 3 ssmeets 2010-09-16 13:34:53 UTC
Created attachment 26034 [details]
WordDoc for Junit Test 2
Comment 4 ssmeets 2010-09-16 13:35:11 UTC
Attached the two files it was failing on. For some reason I can not get JUnit testing not working, but I belive this should be the test cases:

headerSubStringErr = HWPFTestDataSamples.openSampleFile("Case1.doc");
headerSubStringErr1 = HWPFTestDataSamples.openSampleFile("Case2.doc");

public void testHeader() {
  HeaderStories hs = new HeaderStories(headerSubStringErr);

public void testHeader1() {
  HeaderStories hs = new HeaderStories(headerSubStringErr1);
Comment 5 Nick Burch 2010-09-17 10:14:30 UTC
Thanks for the patch and test files. Fix, along with a unit test committed in r998146.