diff --git a/src/DwgIO/DwgDocument.cs b/src/DwgIO/DwgDocument.cs index 3c3c448..885f22a 100644 --- a/src/DwgIO/DwgDocument.cs +++ b/src/DwgIO/DwgDocument.cs @@ -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)) { @@ -269,7 +269,7 @@ private List 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() { diff --git a/src/DwgIO/Internal/Container/R2004Container.cs b/src/DwgIO/Internal/Container/R2004Container.cs index aa63553..fc1e519 100644 --- a/src/DwgIO/Internal/Container/R2004Container.cs +++ b/src/DwgIO/Internal/Container/R2004Container.cs @@ -8,6 +8,13 @@ namespace DwgIO.Internal.Container; /// internal sealed class R2004Container { + /// + /// 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. + /// + private const ulong MaxSizeOvershoot = 0x100; + private long _decompressedBytes; /// Ceiling on section buffers allocated plus page bytes produced by all section reads of one document. @@ -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 bytes beyond what the pages + /// can hold is zero-filled and reported through . /// When is set, a page checksum mismatch throws. /// - public byte[] ReadSection(byte[] fileBytes, SectionEntry entry, bool verifyChecksums = false) + public byte[] ReadSection( + byte[] fileBytes, + SectionEntry entry, + bool verifyChecksums = false, + Action? reportDiagnostic = null) { if (entry.Encryption == 1) { @@ -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]; diff --git a/tests/DwgIO.Tests/InputHardeningTests.cs b/tests/DwgIO.Tests/InputHardeningTests.cs index e4089a0..5f070c1 100644 --- a/tests/DwgIO.Tests/InputHardeningTests.cs +++ b/tests/DwgIO.Tests/InputHardeningTests.cs @@ -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(); + + 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(() => Container(pages, entry).ReadSection(file, entry)); }