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

Dispose stream in initializer if loading file fails #1829

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,17 @@ public StreamPackageFeature(Stream stream, PackageOpenMode openMode, bool isOwne
Mode = initialMode == FileMode.Create ? Mode = FileMode.Open : initialMode;
Access = openMode == PackageOpenMode.Read ? FileAccess.Read : FileAccess.ReadWrite;

InitializePackage(initialMode, Access);
try
{
InitializePackage(initialMode, Access);
}
catch when (isOwned)
{
// Ensure that the stream if created is disposed before leaving the constructor so we don't hold onto it
_stream?.Dispose();
throw;
}

_isOwned = isOwned;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using DocumentFormat.OpenXml.Builder;
using System.IO;
using Xunit;

namespace DocumentFormat.OpenXml.Features.Tests;

public class FilePackageFeatureTests
{
[Fact]
public void OpenInvalidFileFailsGracefully()
{
// Arrange
var path = Path.GetTempFileName();
File.WriteAllText(path, "This is not a valid document");

// Act
Assert.Throws<FileFormatException>(() => new FilePackageFeature(path, PackageOpenMode.Read));

// Assert
using var reopened = File.Open(path, FileMode.Open, FileAccess.Read);
Assert.NotNull(reopened);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using DocumentFormat.OpenXml.Builder;
using DocumentFormat.OpenXml.Framework;
using DocumentFormat.OpenXml.Packaging;
using NSubstitute;
using System;
Expand Down Expand Up @@ -399,6 +400,37 @@ public void PartReload()
Assert.Same(relationshipBefore, relationshipAfter);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void OpenInvalidStreamFailsGracefully(bool isOwned)
{
// Arrange
var stream = new DisposingWatcherInvalidStream([1, 2, 3, 4]);

// Act
Assert.Throws<FileFormatException>(() => new StreamPackageFeature(stream, PackageOpenMode.Read, isOwned: isOwned));

// Assert
Assert.Equal(isOwned, stream.IsDisposed);
}

private sealed class DisposingWatcherInvalidStream : DelegatingStream
{
public DisposingWatcherInvalidStream(byte[] bytes)
: base(new MemoryStream(bytes))
{
}

public bool IsDisposed { get; private set; }

protected override void Dispose(bool disposing)
{
IsDisposed = true;
base.Dispose(disposing);
}
}

private static readonly PartInfo Part1 = new(new("/part1", UriKind.Relative), "type1/content");
private static readonly PartInfo Part2 = new(new("/part2", UriKind.Relative), "type2/content");
private static readonly PartInfo PartRels = new(new("/_rels/.rels", UriKind.Relative), "application/vnd.openxmlformats-package.relationships+xml");
Expand Down
Loading