Skip to content

Refuse deeply nested equations instead of overflowing the stack (#69) - #70

Merged
simonebortolin merged 4 commits into
mainfrom
claude/happy-sagan-sll9vh
Nov 20, 2026
Merged

simonebortolin merged 4 commits into
mainfrom
claude/happy-sagan-sll9vh

Conversation

@simonebortolin

Copy link
Copy Markdown
Owner

LaTeX, UnicodeMath, MathML or OMML nested a few hundred levels deep overflowed
the stack in the readers (LaTeXReader.Pass2, RewriteTeXOnly, the UnicodeMath
translator) or the writers, which ends the process.

  • The XML of a MathML or OMML source, and every equation read, are refused
    beyond EquationStylesheets.MaxDepth levels (FormatException).
  • The recursive methods of the readers, the writers and the templates the XSLT
    generator compiles call RuntimeHelpers.EnsureSufficientExecutionStack: reading
    turns it into a FormatException (InvalidDataException for MTEF), writing
    throws InsufficientExecutionStackException, as the OMML stylesheet guard now
    does too (it threw ArgumentException).

Co-Authored-By: Claude Opus 5.5 [email protected]
Claude-Session: https://claude.ai/code/session_01SyVioATwYG9TEkfaF3Xtix

LaTeX, UnicodeMath, MathML or OMML nested a few hundred levels deep overflowed
the stack in the readers (LaTeXReader.Pass2, RewriteTeXOnly, the UnicodeMath
translator) or the writers, which ends the process.

- The XML of a MathML or OMML source, and every equation read, are refused
  beyond EquationStylesheets.MaxDepth levels (FormatException).
- The recursive methods of the readers, the writers and the templates the XSLT
  generator compiles call RuntimeHelpers.EnsureSufficientExecutionStack: reading
  turns it into a FormatException (InvalidDataException for MTEF), writing
  throws InsufficientExecutionStackException, as the OMML stylesheet guard now
  does too (it threw ArgumentException).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SyVioATwYG9TEkfaF3Xtix
…scripts (#69)

A LaTeX equation that the grammar refuses (a chain of \sqrt too long for the
stack, for one) said "Not valid UnicodeMath" with the UnicodeMath it was
rewritten to: it now says "Not valid LaTeX" with the LaTeX given. Chains
without braces (x^x^x..., x_x_x..., x\sqrt\sqrt...) are refused with a
FormatException, as nesting is.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SyVioATwYG9TEkfaF3Xtix
The depth check that every equation read now goes through allocated a stack
and an enumerator of each element's children, and Parse(MathML) allocated
15-19% more than before. It now walks the tree by its links, and asks
HasElements before FirstNode, which would make an XText of the text of every
token. The benchmarks against main: the same allocations, or fewer (the OMML
stylesheets use the same check), and times within the noise (geometric mean
-1.0%).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SyVioATwYG9TEkfaF3Xtix
RejectsDeepOmml(m:nary, 250) failed on net481: 250 n-ary operators are
about 502 levels of XML, within MaxDepth, and whether the stylesheet ran out
of the 1 MB stack depended on the frames of the runtime (on .NET Framework it
did not, and the equation was read). The tests that expect a refusal now nest
beyond MaxDepth, or far beyond what any 1 MB stack holds; the equations of
the issue, near the limit, are checked to be read or refused, never to end
the process. The LaTeX nested 20000 deep (over a minute on net481) is 5000.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SyVioATwYG9TEkfaF3Xtix
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Pre-release removed now that this PR is closed.

@simonebortolin
simonebortolin merged commit 84ef645 into main Nov 20, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants