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


##########
poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java:
##########
@@ -406,17 +407,76 @@ private boolean removeRelation(String partId, boolean 
removeUnusedParts) {
         if (removeUnusedParts) {
             /* if last relation to target part was removed, delete according 
target part */
             if (part.getRelationCounter() == 0) {
-                try {
-                    part.onDocumentRemove();
-                } catch (IOException e) {
-                    throw new POIXMLException(e);
-                }
-                
getPackagePart().getPackage().removePart(part.getPackagePart());
+                part.removeFromPackage();
             }
         }
         return true;
     }
 
+    /**
+     * Remove this part from the package, together with the parts it relates to
+     * which are not used by any other part
+     */
+    private void removeFromPackage() {
+        try {
+            onDocumentRemove();
+        } catch (IOException e) {
+            throw new POIXMLException(e);
+        }
+        for (RelationPart rp : getRelationParts()) {
+            POIXMLDocumentPart child = rp.getDocumentPart();
+            removeRelation(rp.getRelationship().getId(), false);
+            // the relation counter alone is not enough, as a package part can 
be
+            // represented by more than one document part, e.g. a picture on 
two sheets
+            if (child.getRelationCounter() == 0 && 
!child.isRelationshipTarget()) {
+                child.removeFromPackage();

Review Comment:
   This can leave `XSSFWorkbook.pictures` holding a picture whose part has been 
deleted. `XSSFWorkbook.addPicture` creates the image with `noRelation=true` and 
adds it to `pictures`, so the workbook has no relationship to the image. The 
image's only tracked relation is from the drawing, through the separate 
`XSSFPictureData` that `XSSFDrawing.addPictureReference` creates. When the 
sheet is removed, the count drops to 0, `isRelationshipTarget()` returns false, 
and the part is removed. The second half of 
`testRemoveSheetRemovesDrawingParts` checks exactly this for `own`.
   
   But `wb.getAllPictures()` still returns that picture afterwards, so its 
index is stale. This sequence:
   
   ```java
   int idx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
   wb.getSheet("Template").createDrawingPatriarch().createPicture(anchor, idx);
   wb.removeSheetAt(wb.getSheetIndex("Template"));
   wb.getSheet("Other").createDrawingPatriarch().createPicture(anchor, idx);
   ```
   
   now adds a relationship to a part that is no longer in the package, so the 
written file should be missing the image. On trunk it works, because the image 
part was never removed.
   
   Two ways to fix it:
   - after removal, drop entries from the workbook's `pictures` list whose part 
is no longer in the package (indexes shift, but they then match what a reload 
would show)
   - treat pictures held in that list as still in use, so they aren't removed
   
   A test that reuses the index after `removeSheetAt` would cover it.



-- 
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