Skip to content

🐛 fix(validate): compare whole range facet numbers - #2164

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

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:sec-fix/sgju6vrd

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Range facets on numeric types compared only the first 63 characters of a value and of its bound. 🔢 A long value could hide the digits or exponent that decide the comparison, so XMLSchema and RelaxNG could pass an out-of-range value or reject an in-range one. After this change the comparison uses the whole value, and the verdict matches the number the document states.

The schema datatypes allow leading zeros in an integer and an exponent after the digits of a double. The old 63-character window dropped both in long values, so a value of leading zeros compared as zero. Values of 64 characters or more are copied into an allocation of their own length. RELAX NG converts the same text once for every range-bounded alternative, so a long value reuses one buffer for the whole validation instead of holding one copy per alternative. Shorter values allocate nothing.

Behavior changes

  • A numeric value is compared by its whole value. A value that passed a bound only because digits or an exponent were dropped is now rejected, and a long in-range value that was rejected is now accepted.
  • Bounds are still converted to doubles, so two values that differ beyond double precision compare the same way against a bound.
  • A facet value of 64 characters or more, such as a length or totalDigits written with leading zeros, is now read whole when the schema compiles.

Fixes #1764.

Range facets read only the first 63 characters of a numeric value. Leading
zeros or a long fraction could push the digits or the exponent that decide
the comparison past that window, so XMLSchema and RelaxNG judged the value
as a different number and could accept an out-of-range value or reject an
in-range one.

Convert the whole value. A facet bound is copied into the schema's arena,
which holds it once. An instance value under validation is copied into one
buffer per validate() call, grown as longer values arrive, because RelaxNG
converts a text once for each range-bounded alternative it meets and a
copy per alternative would grow with the number of alternatives.
@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 improve performance by 6.59%

1 warning

⚠️ 2 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 2 improved benchmarks
✅ 580 untouched benchmarks
⏩ 89 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_feature[query-closest] 274.7 µs 255.1 µs +7.69%
⚡ test_feature[select-nth] 462.3 µs 438.2 µs +5.5%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:sec-fix/sgju6vrd (69f011f) with main (ba180cf)

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 7a2a98c into tox-dev:main Nov 15, 2026
31 checks passed
gaborbernat added a commit that referenced this pull request Nov 15, 2026
Fixes #2171.

When an allocation failed during `XMLSchema(...)`, `RelaxNG(...)` or
their `validate()` calls, the call could raise a `ValueError` about the
schema or a validation limit instead of `MemoryError`. Compilation
checked the arena's out-of-memory flag only when no exception was set
([schema.c
L1591-L1596](https://github.com/tox-dev/turbohtml/blob/aceeaf94e3e019902748f8682c8e56be7ab3f686/src/turbohtml/_c/validate/schema.c#L1591-L1596)),
so a partial build that had already raised kept its error. Validation
checked the choice limit and the pattern budget before the flag
([schema.c
L1676-L1692](https://github.com/tox-dev/turbohtml/blob/aceeaf94e3e019902748f8682c8e56be7ab3f686/src/turbohtml/_c/validate/schema.c#L1676-L1692)),
so an allocation lost after a limit tripped was reported as that limit.
Both entry points now raise `MemoryError` for an allocation failure,
whatever error the partial build raised.

### A name lost to an allocation failure looked undeclared

`xmlns_scoped` returns `NULL` when the arena cannot grow ([schema.c
L484-L486](https://github.com/tox-dev/turbohtml/blob/aceeaf94e3e019902748f8682c8e56be7ab3f686/src/turbohtml/_c/validate/schema.c#L484-L486)),
and `resolve_ns` then treats the prefix as undeclared
([L528-L535](https://github.com/tox-dev/turbohtml/blob/aceeaf94e3e019902748f8682c8e56be7ab3f686/src/turbohtml/_c/validate/schema.c#L528-L535)).
The name gets no namespace, so compilation fails with "root element is
not an xs:schema" ([xsd.h
L2296-L2298](https://github.com/tox-dev/turbohtml/blob/aceeaf94e3e019902748f8682c8e56be7ab3f686/src/turbohtml/_c/validate/xsd.h#L2296-L2298))
or with a "does not resolve" or structure-namespace error from another
lookup. The arena flag is now checked before the compile result is
returned ([schema.c
L1591-L1595](https://github.com/tox-dev/turbohtml/blob/a8548b7a5e0ffd37576cd88617c785174016aeb0/src/turbohtml/_c/validate/schema.c#L1591-L1595)).

### The choice limit set the out-of-memory flag

`rng_event_over` and `pat_choice_merge` set `mem.no_memory` next to
`choice_limit = -1` ([relaxng.h
L258-L266](https://github.com/tox-dev/turbohtml/blob/aceeaf94e3e019902748f8682c8e56be7ab3f686/src/turbohtml/_c/validate/relaxng.h#L258-L266),
[L609-L611](https://github.com/tox-dev/turbohtml/blob/aceeaf94e3e019902748f8682c8e56be7ab3f686/src/turbohtml/_c/validate/relaxng.h#L609-L611)).
The validate entry point could not tell a limit from a lost allocation.
The limits now set only `choice_limit` ([relaxng.h
L262-L264](https://github.com/tox-dev/turbohtml/blob/a8548b7a5e0ffd37576cd88617c785174016aeb0/src/turbohtml/_c/validate/relaxng.h#L258-L264),
[L607-L608](https://github.com/tox-dev/turbohtml/blob/a8548b7a5e0ffd37576cd88617c785174016aeb0/src/turbohtml/_c/validate/relaxng.h#L605-L608)),
and the entry point tests the flag first ([schema.c
L1679-L1690](https://github.com/tox-dev/turbohtml/blob/a8548b7a5e0ffd37576cd88617c785174016aeb0/src/turbohtml/_c/validate/schema.c#L1679-L1690)).

### Behavior changes

- An allocation failure during compile or validate now raises
`MemoryError` where it could raise `ValueError` before. Calls where no
allocation fails behave as before.
- The choice limit and the pattern budget still raise `ValueError` when
reached without an allocation failure.
- The pattern-budget check is ordered after the allocation check too,
but no sweep covers it, because one run of that budget takes about 100
million NFA steps.

### Tests

The new cases are allocation-failure sweeps in
`tests/_html/fuzz/test_self_test.py`, which run on a `-Dfuzzing=true`
build like the existing sweeps. Each of them fails on upstream/main on
that build. The `xsd-compile` case removed from the #2164 test is
restored there.

### Performance

Callgrind on release LTO builds (`-Dbuildtype=release -Db_lto=true`, no
PGO), at 20 and 220 repetitions. Each count is per call: (220-repetition
count minus 20-repetition count) / 200. main was built clean twice in
the same worktree path for the noise row, and head was built clean in
that path too.

| Row | main | main (second build) | head | head vs main (second build)
|
| ------------------ | ---------: | ------------------: | ---------: |
--------------------------: |
| compile-facets | 75,170 | 75,170 | 75,170 | 0 (0.00%) |
| compile-rng | 69,308 | 69,308 | 69,308 | 0 (0.00%) |
| validate | 13,316,746 | 13,316,746 | 13,321,147 | +4,401 (+0.03%) |
| validate-rng-reuse | 468,400 | 467,562 | 467,693 | +131 (+0.03%) |
| fragment (control) | 302,452 | 302,452 | 302,452 | 0 (0.00%) |

The noise row is exact for every row except validate-rng-reuse, where
the two main builds differ by 838 instructions per call (0.18%). Head
sits within that spread. The validate row is +0.03%, under the +0.05%
noise band, so no optimization round was run. The change adds one
comparison to the validate entry, and the compile path does the same
work when no allocation fails. The control row is `fragment`, because
`tokenize` needs a submodule this worktree does not have.
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 range facets read only the first 63 characters of a number

1 participant