Bug 54860 - Range.getTable(...) doesn't initialize previous Paragraph for comparison
Summary: Range.getTable(...) doesn't initialize previous Paragraph for comparison
Status: NEW
Alias: None
Product: POI
Classification: Unclassified
Component: HWPF (show other bugs)
Version: 3.8-FINAL
Hardware: PC Windows Vista
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-04-18 08:01 UTC by Manuel Bork
Modified: 2017-11-04 13:00 UTC (History)
0 users



Attachments
Testcase (3.10 KB, text/plain)
2013-04-30 14:32 UTC, Manuel Bork
Details
Example document for the testcase (35.00 KB, application/octet-stream)
2013-04-30 14:32 UTC, Manuel Bork
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Manuel Bork 2013-04-18 08:01:26 UTC
org.apache.poi.hwpf.usermodel.Range.getTable(Paragraph) checks if a given Paragraph is the first in the table by comparing it with the previous paragraph. So the previous paragraph is created in line 926 by calling 

<code>
            Paragraph previous = Paragraph.newParagraph( this,
                    _paragraphs.get( r._parStart - 1 ) );
</code>

Unfortunatly, the end index in the sections list of the Range of the previous Paragraph is not correctly initialized.

So the comparison in line 931 fails, if r._sectionStart is larger than 1 (as previous._sectionEnd is still 0.

<code>
            if ( previous.isInTable() && //
                    previous.getTableLevel() == tableLevel //
                    && previous._sectionEnd >= r._sectionStart )
</code>

Resolution: Initialize the previous Paragraph by calling initAll():

<code>
        if ( r._parStart != 0 )
        {
            Paragraph previous = Paragraph.newParagraph( this,
                    _paragraphs.get( r._parStart - 1 ) );
            previous.initAll(); // initialize sections for proper comparison
            if ( previous.isInTable() && //
                    previous.getTableLevel() == tableLevel //
                    && previous._sectionEnd >= r._sectionStart )
            {
                throw new IllegalArgumentException(
                        "This paragraph is not the first one in the table" );
            }
        }

</code>
Comment 1 Nick Burch 2013-04-18 09:59:40 UTC
Are you able to put together a small unit test that shows off the problem, and shows that your proposed fix solves it?
Comment 2 Manuel Bork 2013-04-30 14:32:22 UTC
Created attachment 30244 [details]
Testcase
Comment 3 Manuel Bork 2013-04-30 14:32:48 UTC
Created attachment 30245 [details]
Example document for the testcase
Comment 4 Manuel Bork 2013-04-30 14:33:26 UTC
Please find here a test case for the bug.
Comment 5 Dominik Stadler 2017-11-04 13:00:17 UTC
I tried to finally apply this, but unfortunately this change breaks some existing tests: TestWordExtractorBugs.testProblemMetadata, I could not see quickly why it happens now and how to resolve this...