Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with opening corrupted Open XML documents #1681

Closed
BenjaminLopVic opened this issue Feb 28, 2024 · 6 comments · Fixed by #1829
Closed

Bug with opening corrupted Open XML documents #1681

BenjaminLopVic opened this issue Feb 28, 2024 · 6 comments · Fixed by #1829
Assignees

Comments

@BenjaminLopVic
Copy link

Describe the bug
Opening a corrupted document lock the file.

To Reproduce
Steps to reproduce the behavior:

  1. Simulate a corrupted file (simply create a txt file with content in it and change the extension to .docx)
  2. Try opening the file with WordProcessingDocument.Open(...)

Observed behavior
The method WordProcessingDocument.Open(...) throws an exception, stating that the file is corrupted but keeps the file locked to. This can be verified using tools like "File Locksmith."

Expected behavior
Continue throwing the exception but release the file before.

Desktop

  • OS: Windows 11
  • .NET Target: .NET Framework 4.8
  • DocumentFormat.OpenXml Version: 3.0.1

Thanks.

@BenjaminLopVic BenjaminLopVic changed the title Bug with corrupted Open XML documents Bug with opening corrupted Open XML documents Feb 28, 2024
@Numpsy
Copy link
Contributor

Numpsy commented Mar 15, 2024

I'm seeing this as well.
Opening the file myself and passing the stream into WordprocessingDocument.Open instead of the path might be a work around.

@twsouthwick
Copy link
Member

I believe this has been fixed in 3.0.2. Please reopen if you're still seeing this

@Numpsy
Copy link
Contributor

Numpsy commented Jun 17, 2024

If I do something like this

static void Main(string[] args)
{
    try
    {
        using var doc = WordprocessingDocument.Open(@"S:\test\WIDE.doc", false);
    }
    catch
    {
        using var file = File.Open(@"S:\test\WIDE.doc", FileMode.Open, FileAccess.Read);
    }
}

On .NET 8.0 then the call to Open throws System.IO.FileFormatException with the message "File contains corrupted data." and then the call to File.Open fails because the file is still locked - doesn't seem right?

Debugging it, it looks like the call to Package.Open at

_package = Package.Open(_stream, mode.Value, access.Value);
throws, and then that exception escapes into the caller without disposing the FileStream created on the supplied path.

(I don't have an option to reopen the issue myself)

@BenjaminLopVic
Copy link
Author

Hello,

I am still experiencing this issue on version 3.0.2 and I am unable to reopen the issue.

Here is a sample code:

using DocumentFormat.OpenXml.Packaging;

Console.WriteLine("Hello, World!");
WordprocessingDocument wDoc = null;
try
{
    wDoc = WordprocessingDocument.Open(@"...\corruptedFile.docx", true);
}
catch (Exception ex)
{
    Console.WriteLine(ex);
}
finally
{
    wDoc?.Dispose();
}
Console.ReadKey();

With this code, the file remains locked by the console until it is shut down.

Thanks.

@twsouthwick twsouthwick reopened this Jun 18, 2024
@twsouthwick twsouthwick self-assigned this Jun 18, 2024
@twsouthwick
Copy link
Member

thanks for the response - I'll take a look

@Numpsy
Copy link
Contributor

Numpsy commented Nov 22, 2024

I gave the 3.1.2-ci0019 CI build a test, and the error I was seeing has been fixed by that :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants