Skip to content

🐛 fix(validate): raise MemoryError when schema allocation fails - #2177

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:sec-fix/n53-xsd-oom
Nov 15, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:sec-fix/n53-xsd-oom

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Fixes #2171.

Running out of memory while compiling or validating an XML Schema or RELAX NG schema could surface as a ValueError about the schema or about a validation limit. The message named the wrong cause, so callers that handle a MemoryError never saw the shortage. 🧠 Both entry points now raise MemoryError whenever an allocation fails.

The schema arena records a lost allocation in one flag. Compilation and validation now check that flag before they report any error or limit, because an error or limit reached after a lost allocation is a symptom of that loss. The choice limit and the pattern budget keep their own ValueErrors and no longer set the out-of-memory flag, so the two causes stay separate.

Behavior changes

  • An allocation failure during XMLSchema(...), RelaxNG(...), or their validate() calls raises MemoryError, where it could raise ValueError before.
  • The choice limit and the pattern budget still raise ValueError when they are reached without an allocation failure.

A schema that runs out of memory while it compiles can report the
wrong error. Every arena allocation failure leaves a partial build
behind, so a name that could not be resolved produced "does not
resolve" or "root element is not an xs:schema", and callers got a
ValueError instead of MemoryError. Validation checked the choice limit
and the pattern budget before the arena flag, so a lost allocation
after either limit tripped was reported as an ambiguous content model
or an exhausted budget.

The arena flag now decides at both entry points, before any error the
partial build raised. The choice limit no longer sets the arena flag,
so a limit trip and an allocation failure stay distinct. The
allocation-failure sweep now expects MemoryError on every path it
reaches.
@gaborbernat gaborbernat added the bug Something isn't working label Nov 15, 2026
@codspeed

codspeed Bot commented Nov 15, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 582 untouched benchmarks
⏩ 89 skipped benchmarks1


Comparing gaborbernat:sec-fix/n53-xsd-oom (a8548b7) with main (aceeaf9)

Open in CodSpeed

Footnotes

  1. 89 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@gaborbernat
gaborbernat merged commit 54b4b9b into tox-dev:main Nov 15, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XSD and RELAX NG raise ValueError instead of MemoryError

1 participant