Guard ParentTree allocation against pathological MCID - #94
Merged
Conversation
A self-review of #93 (v1.5.5) found a regression: the per-page ParentTree array is sized by (max MCID + 1), and PdfStructElem.Mcid is a public setter, so a hand-built structure tree with a huge MCID (or int.MaxValue) would attempt a multi-gigabyte allocation or overflow the length to a negative value — crashing mid-Save instead of failing cleanly. Cap the per-page MCID at 1,000,000 (far above any real page's marked-content count, which is bounded by content-stream size) and throw a clear InvalidOperationException above it, before the array is sized. Documents tagged through the canvas are unaffected: their per-page MCIDs are dense and sequential. Bump to 1.5.6.
This was referenced Jun 17, 2026
This was referenced Jul 1, 2026
This was referenced Jul 15, 2026
This was referenced Aug 19, 2026
This was referenced Sep 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to a self-review of #93 (v1.5.5).
Problem
The v1.5.5 MCID ParentTree change sizes the per-page array by
max(MCID) + 1. SincePdfStructElem.Mcidis a public setter, a hand-built structure tree with a very large MCID (orint.MaxValue) would attempt a multi-gigabyte allocation, or overflowmax(MCID)+1to a negative length — crashing mid-Saverather than failing cleanly. Documents tagged through the canvas are safe (their per-page MCIDs are dense and sequential); this only affected hand-built trees.Fix
Cap the per-page MCID at
1_000_000(per-page marked content is bounded by content-stream size, so this is ~100× beyond any real page) and throw a clearInvalidOperationExceptionabove it, before the array is sized — so neither the overflow nor the large allocation is reachable. It is a robustness guard, not a conformance limit.Tests
StructureTree_pathologicalMcid_throwsInsteadOfOom(assertsint.MaxValuethrowsInvalidOperationException, not OOM/overflow).dotnet format --verify-no-changesclean; AOT smoke passes.Bumps version to 1.5.6.