This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new dc09d97c6f PAPBinTable.rebuild: keep a PAPX's properties at an
internal paragraph mark (#1509)
dc09d97c6f is described below
commit dc09d97c6fb4d2a5dae75468ef169e7a4ab42a2b
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Nov 2 14:00:56 2026 +0100
PAPBinTable.rebuild: keep a PAPX's properties at an internal paragraph mark
(#1509)
* PAPBinTable.rebuild: keep a PAPX's properties at an internal paragraph
mark (#1499)
rebuild splits paragraphs at every CR, cell mark and page break. When
such a mark fell inside a PAPX interval rather than at its end, no PAPX
was collected for the paragraph and it got empty default properties, so
e.g. text in a table cell before an internal CR came out as a non-table
paragraph. Per [MS-DOC] 2.4.2 the interval's properties cover all of its
text, so those paragraphs now take the properties of the PAPX(s)
covering the mark, minus sprmPFTtp, sprmPFInnerTableCell and
sprmPFInnerTtp: the cell and row end marks belong only to the
interval's last paragraph, and copying them would split cells and rows
early. Paragraphs that no PAPX covers still get defaults.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* Expect IllegalArgumentException when extracting
POIHWPFFuzzer-5195207308541952.doc
The fuzzed file has a PAPX with sprmPFInTable whose interval spans
hundreds of paragraph marks and runs from the main text into the header
subdocument. With internal paragraph marks now keeping the PAPX's
properties, a table crosses the subdocument boundary and Range.getTable
rejects the first header paragraph.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---------
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../org/apache/poi/hwpf/model/PAPBinTable.java | 114 ++++++++++++++++++---
.../org/apache/poi/hwpf/model/TestPAPBinTable.java | 72 +++++++++++++
test-data/poi-integration-exceptions.csv | 1 +
3 files changed, 171 insertions(+), 16 deletions(-)
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/PAPBinTable.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/PAPBinTable.java
index 225fc97e32..65892e8979 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/PAPBinTable.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/PAPBinTable.java
@@ -213,10 +213,27 @@ public class PAPBinTable
if ( papxs.isEmpty() )
{
- LOG.atWarn().log("Paragraph [{}; {}) has no PAPX. Creating new
one.", box(startInclusive),box(endExclusive));
- // create it manually
- PAPX papx = new PAPX( startInclusive, endExclusive,
- new SprmBuffer( 2 ) );
+ // No PAPX ends at this paragraph mark, so it is inside a PAPX
interval. Per
+ // [MS-DOC] 2.4.2 the properties of that interval cover all of
its text.
+ List<PAPX> covering = getCoveringPapxs( oldPapxSortedByEndPos,
+ lastPapxIndex, charIndex );
+ SprmBuffer sprmBuffer = null;
+ if ( !covering.isEmpty() )
+ {
+ covering.sort( papxFileOrderComparator );
+ sprmBuffer = mergeSprms( covering );
+ }
+ if ( sprmBuffer == null )
+ {
+ LOG.atWarn().log("Paragraph [{}; {}) has no PAPX. Creating
new one.", box(startInclusive),box(endExclusive));
+ sprmBuffer = new SprmBuffer( 2 );
+ }
+ else
+ {
+ // the cell and row end marks belong to the interval's
last paragraph only
+ sprmBuffer = withoutTableEndMarks( sprmBuffer );
+ }
+ PAPX papx = new PAPX( startInclusive, endExclusive, sprmBuffer
);
newPapxs.add( papx );
lastParStart = endExclusive;
@@ -239,18 +256,7 @@ public class PAPBinTable
// restore file order of PAPX
papxs.sort(papxFileOrderComparator);
- SprmBuffer sprmBuffer = null;
- for ( PAPX papx : papxs )
- {
- if ( papx.getGrpprl() == null || papx.getGrpprl().length <= 2 )
- continue;
-
- if ( sprmBuffer == null ) {
- sprmBuffer = papx.getSprmBuf().copy();
- } else {
- sprmBuffer.append( papx.getGrpprl(), 2 );
- }
- }
+ SprmBuffer sprmBuffer = mergeSprms( papxs );
PAPX newPapx = new PAPX( startInclusive, endExclusive, sprmBuffer
);
newPapxs.add( newPapx );
@@ -263,6 +269,82 @@ public class PAPBinTable
LOG.atDebug().log("PAPX rebuilded from document text in {} ms ({}
elements)", box(currentTimeMillis() - start),box(paragraphs.size()));
}
+ /**
+ * @return the PAPXs whose interval contains {@code charIndex}, starting
the search at
+ * {@code fromIndex} in {@code papxsSortedByEnd}
+ */
+ private static List<PAPX> getCoveringPapxs( List<PAPX> papxsSortedByEnd,
+ int fromIndex, int charIndex )
+ {
+ List<PAPX> covering = new ArrayList<>();
+ for ( int papxIndex = fromIndex; papxIndex < papxsSortedByEnd.size();
papxIndex++ )
+ {
+ PAPX papx = papxsSortedByEnd.get( papxIndex );
+ if ( papx.getStart() > charIndex )
+ {
+ // a later PAPX could only cover charIndex by overlapping this
one, which
+ // doesn't happen for the PAPXs of one FKP; stop to keep the
search short
+ break;
+ }
+ if ( papx.getEnd() > charIndex )
+ {
+ covering.add( papx );
+ }
+ }
+ return covering;
+ }
+
+ /**
+ * @return the SPRMs of {@code papxs} concatenated in list order, or
{@code null} if none
+ * of them has any
+ */
+ private static SprmBuffer mergeSprms( List<PAPX> papxs )
+ {
+ SprmBuffer sprmBuffer = null;
+ for ( PAPX papx : papxs )
+ {
+ if ( papx.getGrpprl() == null || papx.getGrpprl().length <= 2 )
+ continue;
+
+ if ( sprmBuffer == null ) {
+ sprmBuffer = papx.getSprmBuf().copy();
+ } else {
+ sprmBuffer.append( papx.getGrpprl(), 2 );
+ }
+ }
+ return sprmBuffer;
+ }
+
+ /**
+ * @return a copy of {@code sprmBuffer} without the SPRMs that mark a
paragraph as the
+ * end of a table cell or row
+ */
+ private static SprmBuffer withoutTableEndMarks( SprmBuffer sprmBuffer )
+ {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ // keep the istd
+ bos.write( sprmBuffer.toByteArray(), 0, 2 );
+ for ( SprmIterator iterator = sprmBuffer.iterator();
iterator.hasNext(); )
+ {
+ SprmOperation sprm = iterator.next();
+ if ( sprm.getType() == SprmOperation.TYPE_PAP )
+ {
+ switch ( sprm.getOperation() )
+ {
+ case 0x17: // sprmPFTtp
+ case 0x4b: // sprmPFInnerTableCell
+ case 0x4c: // sprmPFInnerTtp
+ continue;
+ default:
+ break;
+ }
+ }
+ byte[] bytes = sprm.toByteArray();
+ bos.write( bytes, 0, bytes.length );
+ }
+ return new SprmBuffer( bos.toByteArray(), 2 );
+ }
+
public void insert(int listIndex, int cpStart, SprmBuffer buf)
{
diff --git
a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/model/TestPAPBinTable.java
b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/model/TestPAPBinTable.java
index 6588162260..487e845e2f 100644
---
a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/model/TestPAPBinTable.java
+++
b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/model/TestPAPBinTable.java
@@ -19,7 +19,9 @@ package org.apache.poi.hwpf.model;
import static org.apache.poi.hwpf.HWPFTestDataSamples.openSampleFile;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -29,6 +31,9 @@ import org.apache.poi.hwpf.HWPFDocFixture;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToTextConverter;
import org.apache.poi.hwpf.model.io.HWPFFileSystem;
+import org.apache.poi.hwpf.sprm.ParagraphSprmUncompressor;
+import org.apache.poi.hwpf.sprm.SprmBuffer;
+import org.apache.poi.hwpf.usermodel.ParagraphProperties;
import org.junit.jupiter.api.Test;
public final class TestPAPBinTable {
@@ -84,4 +89,71 @@ public final class TestPAPBinTable {
_hWPFDocFixture.tearDown();
}
+
+ @Test
+ void testRebuildParagraphMarkInsideNestedCellPapx() {
+ // one PAPX over "A\r\r" marking the end of a nested table cell
(GitHub #1499)
+ SprmBuffer sprms = new SprmBuffer(2);
+ sprms.addSprm((short) 0x2416, (byte) 1); // sprmPFInTable
+ sprms.addSprm((short) 0x6649, 2); // sprmPItap
+ sprms.addSprm((short) 0x244B, (byte) 1); // sprmPFInnerTableCell
+ List<PAPX> paragraphs = rebuild("A\r\r", new PAPX(0, 3, sprms));
+
+ assertEquals(2, paragraphs.size());
+ ParagraphProperties first = getProperties(paragraphs.get(0), 0, 2);
+ assertTrue(first.getFInTable(), "paragraph before the internal CR
stays in the table");
+ assertEquals(2, first.getItap());
+ assertFalse(first.getFInnerTableCell(), "only the interval's last
paragraph ends the cell");
+
+ ParagraphProperties last = getProperties(paragraphs.get(1), 2, 3);
+ assertTrue(last.getFInTable());
+ assertEquals(2, last.getItap());
+ assertTrue(last.getFInnerTableCell());
+ }
+
+ @Test
+ void testRebuildParagraphMarkInsideRowEndPapx() {
+ SprmBuffer sprms = new SprmBuffer(2);
+ sprms.addSprm((short) 0x2416, (byte) 1); // sprmPFInTable
+ sprms.addSprm((short) 0x2417, (byte) 1); // sprmPFTtp
+ sprms.addSprm((short) 0x2403, (byte) 1); // sprmPJc
+ List<PAPX> paragraphs = rebuild("A\r\u0007", new PAPX(0, 3, sprms));
+
+ assertEquals(2, paragraphs.size());
+ ParagraphProperties first = getProperties(paragraphs.get(0), 0, 2);
+ assertTrue(first.getFInTable());
+ assertEquals(1, first.getJc());
+ assertFalse(first.getFTtp(), "only the interval's last paragraph ends
the row");
+
+ ParagraphProperties last = getProperties(paragraphs.get(1), 2, 3);
+ assertTrue(last.getFTtp());
+ assertEquals(1, last.getJc());
+ }
+
+ @Test
+ void testRebuildParagraphWithoutPapx() {
+ SprmBuffer sprms = new SprmBuffer(2);
+ sprms.addSprm((short) 0x2403, (byte) 1); // sprmPJc
+ List<PAPX> paragraphs = rebuild("A\rB\r", new PAPX(2, 4, sprms));
+
+ assertEquals(2, paragraphs.size());
+ assertEquals(0, getProperties(paragraphs.get(0), 0, 2).getJc(), "no
PAPX covers the first paragraph");
+ assertEquals(1, getProperties(paragraphs.get(1), 2, 4).getJc());
+ }
+
+ private static List<PAPX> rebuild(String text, PAPX papx) {
+ StringBuilder docText = new StringBuilder(text);
+ PAPBinTable table = new PAPBinTable();
+ table.getParagraphs().add(papx);
+ ComplexFileTable complexFileTable = new ComplexFileTable();
+ complexFileTable.getTextPieceTable().add(new
SinglentonTextPiece(docText));
+ table.rebuild(docText, complexFileTable);
+ return table.getParagraphs();
+ }
+
+ private static ParagraphProperties getProperties(PAPX papx, int start, int
end) {
+ assertEquals(start, papx.getStart());
+ assertEquals(end, papx.getEnd());
+ return ParagraphSprmUncompressor.uncompressPAP(new
ParagraphProperties(), papx.getGrpprl(), 2);
+ }
}
diff --git a/test-data/poi-integration-exceptions.csv
b/test-data/poi-integration-exceptions.csv
index f6d2ec5076..12ffa1d3a8 100644
--- a/test-data/poi-integration-exceptions.csv
+++ b/test-data/poi-integration-exceptions.csv
@@ -345,6 +345,7 @@
diagram/clusterfuzz-testcase-minimized-POIVisioFuzzer-5842659694215168.vsdx,hand
document/clusterfuzz-testcase-minimized-POIHWPFFuzzer-5195207308541952.doc,additional,HPSF,,java.lang.ArithmeticException,integer
overflow,
document/clusterfuzz-testcase-minimized-POIHWPFFuzzer-5195207308541952.doc,handle,HWPF,,java.lang.IndexOutOfBoundsException,Block
111 not found,
document/clusterfuzz-testcase-minimized-POIHWPFFuzzer-5195207308541952.doc,handle,HPSF,,org.opentest4j.AssertionFailedError,expected:
<true> but was: <false>,
+document/clusterfuzz-testcase-minimized-POIHWPFFuzzer-5195207308541952.doc,extract,"HWPF,HPSF",,java.lang.IllegalArgumentException,This
paragraph is not the first one in the table,
slideshow/clusterfuzz-testcase-minimized-POIXSLFFuzzer-5611274456596480.pptx,"handle,extract","XSLF,OPC",,java.io.IOException,has
more data than its declared size,
diagram/clusterfuzz-testcase-minimized-POIHDGFFuzzer-6478389109981184.vsd,extract,"HDGF,HPSF",,java.lang.IllegalArgumentException,"Encountered
too deep nesting, cannot process stream",
diagram/clusterfuzz-testcase-minimized-POIHDGFFuzzer-6478389109981184.vsd,handle,HDGF,,java.lang.IllegalArgumentException,"Encountered
too deep nesting, cannot process stream",
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]