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

Limit V3 compound file stream length to 2 GB #274

Merged
merged 1 commit into from
Dec 5, 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
1 change: 1 addition & 0 deletions OpenMcdf.Tests/OpenMcdf.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="MSTest" />
</ItemGroup>
Expand Down
23 changes: 22 additions & 1 deletion OpenMcdf.Tests/RootStorageTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace OpenMcdf.Tests;
using Microsoft.IO;

namespace OpenMcdf.Tests;

[TestClass]
public sealed class RootStorageTests
Expand Down Expand Up @@ -231,4 +233,23 @@ public void DeleteTrimsBaseStream(bool consolidate)

Assert.IsTrue(originalLength > newLength);
}

[TestMethod]
[DoNotParallelize] // High memory usage
public void V3ThrowsIOExceptionAt2GB()
{
const long MaxStreamLength = 2L * 1024 * 1024 * 1024;

RecyclableMemoryStreamManager manager = new();
using RecyclableMemoryStream baseStream = new(manager);
baseStream.Capacity64 = MaxStreamLength;

using var rootStorage = RootStorage.Create(baseStream, Version.V3);
using CfbStream stream = rootStorage.CreateStream("Test");
byte[] buffer = TestData.CreateByteArray(1024 * 1024);
while (baseStream.Length + buffer.Length <= MaxStreamLength)
stream.Write(buffer, 0, buffer.Length);

Assert.ThrowsException<IOException>(() => stream.Write(buffer, 0, buffer.Length));
}
}
5 changes: 5 additions & 0 deletions OpenMcdf/RootContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ enum IOContextFlags
/// </summary>
internal sealed class RootContext : ContextBase, IDisposable
{
const long MaximumV3StreamLength = 2147483648;

readonly IOContextFlags contextFlags;
readonly CfbBinaryWriter? writer;
readonly TransactedStream? transactedStream;
Expand Down Expand Up @@ -185,6 +187,9 @@ public void Flush()

public void ExtendStreamLength(long length)
{
if (Version is Version.V3 && length > MaximumV3StreamLength)
throw new IOException("V3 compound files are limited to 2 GB.");

if (Length < length)
Length = length;
}
Expand Down
Loading