naruto-lgtm opened a new pull request, #1543:
URL: https://github.com/apache/poi/pull/1543

   Repro: open WithThreeCharts.xlsx, call `removeSheetAt(1)` and write it: 
`xl/charts/chart1.xml` and `chart2.xml` are still in the file, with the plotted 
values in their `numCache`. Doing the same with `removeSlide(0)` on 
pie-chart.pptx keeps `ppt/embeddings/Microsoft_Excel_Worksheet1.xlsx`, the full 
workbook behind the chart.
   Cause: `POIXMLDocumentPart.removeRelation(part, true)` removes a part once 
nothing relates to it, but leaves that part's own relations, so whatever only 
it used stays in the package and `write()` saves it (CWE-212).
   Fix: when a part is removed this way, release its relations too and remove 
each child that no relationship in the package targets any more, recursively.
   
   Across test-data this affects 29 workbooks for `removeSheetAt` (a drawing's 
charts, chart style/colour parts and images) and 35 decks for `removeSlide`. 
#1524 noted that `XMLSlideShow.removeSlide` already drops a slide's notes, 
charts and pictures; it does, but not the charts' embedded workbooks, SmartArt 
data, embedded OLE objects, comments, audio or video.
   
   The package check matters because one package part can be represented by 
several document parts. `XSSFDrawing.createPicture` creates a new 
`XSSFPictureData` per placement, so a logo placed on two sheets has two 
document parts with a count of one each, and trusting the counter alone deletes 
the image the remaining sheet still shows (the test covers this). Relationships 
POI doesn't track as relations, such as internal hyperlinks, also keep a part 
alive. `removeSlide` now drops the presentation's relation to the slide after 
its per-type handling, so that loop still sees the slide's relations and keeps 
`_charts`/`_pictures` in step.
   
   Removing each sheet or slide of every xlsx/pptx in test-data (1,259 
removals) no longer leaves charts, images, embeddings or diagram parts behind, 
and creates no relationship to a missing part. 51187.pptx keeps its slide as on 
trunk, because the slide has a relationship to itself. The cost is one scan of 
the package relationships per child part that would be removed, and only on 
removal: about 160 ms to remove a sheet with 100 pictures at the default 
1000-entry limit, about 2 s with 10,000 pictures. If you'd rather avoid that, 
an index of incoming relationships built once per removal would make it linear.
   
   Not covered: a removed sheet's strings stay in sharedStrings.xml. The table 
is append-only, and compacting it means renumbering cells in every sheet and in 
SXSSF's streamed rows, so that seemed better as its own change.
   
   `TestXSSFWorkbook.testRemoveSheetRemovesDrawingParts` and 
`TestXSLFSlideShow.testRemoveSlideThatHasChartAndSmartArt` fail before the 
change. `:poi-ooxml` (`check`, JDK 17) and `:poi-integration` pass locally 
apart from three auto-size width tests (`TestSXSSFSheetAutoSizeColumn`, 
`stackoverflow23114397` in `TestXSSFBugs`/`TestSXSSFBugs`) that fail the same 
way on trunk here.
   


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