Bug 41203 - System.IO.Packaging crashes when deleting files, and corrupts .rels also
Summary: System.IO.Packaging crashes when deleting files, and corrupts .rels also
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 4.4.0 (C7)
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2016-05-20 08:49 UTC by James Laird-Wah
Modified: 2016-05-25 15:09 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Patch to check a part's .Package is non-null before calling GetStream (1.03 KB, patch)
2016-05-20 08:49 UTC, James Laird-Wah
Details
Patch: properly truncate .rels when rewriting (697 bytes, patch)
2016-05-20 08:49 UTC, James Laird-Wah
Details
Test case: check that rel deletion doesn't corrupt .rels (1.75 KB, patch)
2016-05-25 13:21 UTC, James Laird-Wah
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description James Laird-Wah 2016-05-20 08:49:26 UTC
Created attachment 16045 [details]
Patch to check a part's .Package is non-null before calling GetStream

I'm using Microsoft's Open XML SDK to mangle Office documents. I have a codebase that works well on Windows, but crashes on Mono. I've tracked down a couple of issues, and patches are attached.

Specifically, the WordprocessingDocument.ChangeDocumentType invariably fails with a packaging error, with an inner NullReferenceException from System.IO.Packaging. I've tracked the cause down to an interaction between ZipPackage.FlushCore() and Package. From Package.cs, in Package.DeletePart:

                                // FIXME: MS.NET doesn't remove the relationship part
                                // Instead it throws an exception if you try to use it
                                if (PartExists (part.RelationshipsPartUri))
                                        GetPart (part.RelationshipsPartUri).Package = null;

Then when FlushCore() tries to write all the parts out, GetStream() attempts to dereference Package and bombs. A patch is attached.

After fixing that one, I found it was writing files that the Open XML SDK wouldn't accept as input. It turned out that _rels/.rels was being damaged when parts were deleted: if the new .rels file was shorter than the old, it wouldn't be truncated, and junk would remain after the end of the relationship XML. A second patch is attached for this one.

Many thanks for the hard work on System.IO.Packaging - it's a complicated beast and it's very, very useful!
Comment 1 James Laird-Wah 2016-05-20 08:49:55 UTC
Created attachment 16046 [details]
Patch: properly truncate .rels when rewriting
Comment 2 Marek Safar 2016-05-25 09:09:01 UTC
I've applied the first patch but the second one looks suspicious. Do you think you could write a test for it?
Comment 3 James Laird-Wah 2016-05-25 13:21:36 UTC
Created attachment 16096 [details]
Test case: check that rel deletion doesn't corrupt .rels

Here's a test case for the .rel truncation corruption.
Comment 4 Marek Safar 2016-05-25 15:09:41 UTC
Patches applied. Thanks!