pjfanning commented on code in PR #1570:
URL: https://github.com/apache/poi/pull/1570#discussion_r4648072699


##########
poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java:
##########
@@ -319,11 +320,14 @@ public static List<PackagePart> 
getSheetDrawingParts(PackagePart sheetPart, bool
             throws InvalidFormatException {
         PackageRelationshipCollection drawingsList =
                 getRelationshipsByType(sheetPart, XSSFRelation.DRAWINGS, 
allowStrictOoxmlFiles);
-        int drawingsSize = drawingsList.size();
-        List<PackagePart> drawingParts = new ArrayList<>(drawingsSize);
-        for (int i = 0; i < drawingsSize; i++) {
-            PackageRelationship drawings = drawingsList.getRelationship(i);
+        List<PackagePart> drawingParts = new ArrayList<>();
+        Set<PackagePartName> seen = new HashSet<>();
+        for (PackageRelationship drawings : drawingsList) {

Review Comment:
   Minor: this still resolves a URI and builds a `PackagePartName` for every 
relationship (and `getRelationshipsByType` copies the filtered collection into 
a new TreeMap) before the dedupe kicks in, so a .rels with ~1M duplicate 
drawing relationships still costs that on every `getShapes()` call. A worksheet 
can have at most one `<drawing>`, so resolving the `r:id` from the sheet's 
`<drawing>` element would bound this properly, though that's a bigger change.



##########
poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java:
##########
@@ -319,11 +320,14 @@ public static List<PackagePart> 
getSheetDrawingParts(PackagePart sheetPart, bool
             throws InvalidFormatException {
         PackageRelationshipCollection drawingsList =
                 getRelationshipsByType(sheetPart, XSSFRelation.DRAWINGS, 
allowStrictOoxmlFiles);
-        int drawingsSize = drawingsList.size();
-        List<PackagePart> drawingParts = new ArrayList<>(drawingsSize);
-        for (int i = 0; i < drawingsSize; i++) {
-            PackageRelationship drawings = drawingsList.getRelationship(i);
+        List<PackagePart> drawingParts = new ArrayList<>();
+        Set<PackagePartName> seen = new HashSet<>();
+        for (PackageRelationship drawings : drawingsList) {
             PackagePartName drawingsName = 
PackagingURIHelper.createPartName(drawings.getTargetURI());

Review Comment:
   Not new in this PR, but since this loop is being reworked: 
`createPartName(drawings.getTargetURI())` throws `InvalidFormatException` for a 
drawing relationship with `TargetMode=External` or an absolute target that 
isn't a valid part name. That aborts the whole loop, and `getShapes()` then 
logs 'Failed to load shapes' and returns null, dropping the valid drawings too. 
Missing parts are skipped (TIKA-2134); could external relationships be skipped 
and invalid targets caught per relationship in the same way?



##########
poi-ooxml/src/test/java/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java:
##########
@@ -461,6 +461,28 @@ void testGetSheetDrawingParts() throws Exception {
         }
     }
 
+    @Test
+    void testDrawingTargetedByDuplicateRelationships() throws Exception {
+        try (OPCPackage pkg = 
XSSFTestDataSamples.openSamplePackage("WithTextBox.xlsx")) {

Review Comment:
   `XSSFTestDataSamples.openSamplePackage` wraps an InputStream that is never 
closed, so this leaks a file handle per run. Could the test also cover a 
duplicate-`<sheet>` workbook and an external/invalid drawing target alongside a 
valid one (see the inline comments)?



##########
poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java:
##########
@@ -319,11 +320,14 @@ public static List<PackagePart> 
getSheetDrawingParts(PackagePart sheetPart, bool
             throws InvalidFormatException {
         PackageRelationshipCollection drawingsList =
                 getRelationshipsByType(sheetPart, XSSFRelation.DRAWINGS, 
allowStrictOoxmlFiles);
-        int drawingsSize = drawingsList.size();
-        List<PackagePart> drawingParts = new ArrayList<>(drawingsSize);
-        for (int i = 0; i < drawingsSize; i++) {
-            PackageRelationship drawings = drawingsList.getRelationship(i);
+        List<PackagePart> drawingParts = new ArrayList<>();
+        Set<PackagePartName> seen = new HashSet<>();
+        for (PackageRelationship drawings : drawingsList) {
             PackagePartName drawingsName = 
PackagingURIHelper.createPartName(drawings.getTargetURI());
+            if (!seen.add(drawingsName)) {

Review Comment:
   This dedupes only within one sheet's relationships. The same amplification 
still works via duplicate `<sheet>` entries in workbook.xml sharing one `r:id`: 
`SheetIterator` doesn't dedupe `sheetRefs`, so e.g. 4,000 `<sheet name="sN" 
sheetId="N" r:id="rId1"/>` lines (~25 bytes each) make 
`XSSFEventBasedExcelExtractor.getText()` / Tika walk 4,000 sheets resolving to 
the same part and parse its drawing 4,000 times. Deduping at the 
iterator/extractor level (sheetRefs by target part, or caching parsed drawings 
by `PackagePartName` per reader) would close that too.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to