Created attachment 26030 [details] patch Hi, 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, Sjoerd
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
Created attachment 26033 [details] WordDoc for Junit test
Created attachment 26034 [details] WordDoc for Junit Test 2
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); assertEquals("",hs.getHeaderText()); } public void testHeader1() { HeaderStories hs = new HeaderStories(headerSubStringErr1); assertEquals("",hs.getHeaderText()); }
Thanks for the patch and test files. Fix, along with a unit test committed in r998146.