Skip to content
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
4 changes: 2 additions & 2 deletions src/DwgIO/DwgDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public byte[] ReadSection(string name)
?? throw new DwgException($"Section '{name}' not found. Available: {string.Join(", ", container.SectionMap.Sections.Select(s => s.Name))}");
try
{
return container.ReadSection(_fileBytes, entry, _verifyChecksums);
return container.ReadSection(_fileBytes, entry, _verifyChecksums, _diagnostics.Add);
}
catch (Exception ex) when (ex is not DwgException && IsParseFailure(ex))
{
Expand Down Expand Up @@ -269,7 +269,7 @@ private List<DwgEntity> ReadOptionalEntities()
}

private byte[] ReadSectionCore(SectionEntry entry) =>
_container!.ReadSection(_fileBytes, entry, _verifyChecksums);
_container!.ReadSection(_fileBytes, entry, _verifyChecksums, _diagnostics.Add);

private static DwgSectionInfo ToSectionInfo(SectionEntry entry) => new()
{
Expand Down
25 changes: 23 additions & 2 deletions src/DwgIO/Internal/Container/R2004Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ namespace DwgIO.Internal.Container;
/// </summary>
internal sealed class R2004Container
{
/// <summary>
/// Some writers declare a section a few bytes larger than its pages can hold.
/// Accept that much overshoot, zero-fill the uncovered tail and report it, while
/// still rejecting a declared size far beyond the page budget before allocating.
/// </summary>
private const ulong MaxSizeOvershoot = 0x100;

private long _decompressedBytes;

/// <summary>Ceiling on section buffers allocated plus page bytes produced by all section reads of one document.</summary>
Expand Down Expand Up @@ -48,9 +55,15 @@ public static R2004Container Parse(byte[] fileBytes)
/// Assembles one named section's decompressed bytes from its pages.
/// Pages starting past the section end are zero pages and skipped;
/// gaps between page start offsets are filled with zeroes.
/// A declared size up to <see cref="MaxSizeOvershoot"/> bytes beyond what the pages
/// can hold is zero-filled and reported through <paramref name="reportDiagnostic"/>.
/// When <paramref name="verifyChecksums"/> is set, a page checksum mismatch throws.
/// </summary>
public byte[] ReadSection(byte[] fileBytes, SectionEntry entry, bool verifyChecksums = false)
public byte[] ReadSection(
byte[] fileBytes,
SectionEntry entry,
bool verifyChecksums = false,
Action<string>? reportDiagnostic = null)
{
if (entry.Encryption == 1)
{
Expand All @@ -64,13 +77,21 @@ public byte[] ReadSection(byte[] fileBytes, SectionEntry entry, bool verifyCheck
};

// The size comes from the file: check it before allocating the result.
if (entry.Size > (ulong)Array.MaxLength || entry.Size > (ulong)entry.PageCount * (ulong)pageLimits.MaxOutputBytes)
ulong pageBudget = (ulong)entry.PageCount * (ulong)pageLimits.MaxOutputBytes;
if (entry.Size > (ulong)Array.MaxLength || entry.Size > pageBudget + MaxSizeOvershoot)
{
throw new DwgException(
$"Section '{entry.Name}' declares {entry.Size} bytes, more than {entry.PageCount} pages of up to {pageLimits.MaxOutputBytes} bytes " +
"or a single buffer can hold.");
}

if (entry.Size > pageBudget)
{
reportDiagnostic?.Invoke(
$"{entry.Name}: declared size {entry.Size} exceeds the {pageBudget} bytes covered by {entry.PageCount} pages " +
$"by {entry.Size - pageBudget}; the uncovered tail is zero-filled.");
}

int totalSize = (int)entry.Size;
Charge(totalSize); // the result buffer counts against the document budget before it exists
var result = new byte[totalSize];
Expand Down
19 changes: 17 additions & 2 deletions tests/DwgIO.Tests/InputHardeningTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,25 @@ public void ReadSection_ValidPage_Assembles()
}

[Fact]
public void ReadSection_SizeBeyondPageCapacity_Throws()
public void ReadSection_SmallSizeOvershoot_ZeroFillsTailAndReports()
{
var (file, pages) = BuildFile(Page(HelloLz77));
var entry = Section(size: 0x7401, maxPageSize: 0x7400, (1, 0));
var entry = Section(size: 15, maxPageSize: 12, (1, 0));
var diagnostics = new List<string>();

var bytes = Container(pages, entry).ReadSection(file, entry, reportDiagnostic: diagnostics.Add);

Assert.Equal(15, bytes.Length);
Assert.Equal("Hello World!", Encoding.ASCII.GetString(bytes, 0, 12));
Assert.All(bytes[12..], b => Assert.Equal(0, b));
Assert.Contains("zero-filled", Assert.Single(diagnostics), StringComparison.Ordinal);
}

[Fact]
public void ReadSection_SizeFarBeyondPageCapacity_Throws()
{
var (file, pages) = BuildFile(Page(HelloLz77));
var entry = Section(size: 0x7400 + 0x101, maxPageSize: 0x7400, (1, 0));

Assert.Throws<DwgException>(() => Container(pages, entry).ReadSection(file, entry));
}
Expand Down
Loading