Repository navigation
🐛 fix(validate): compare whole range facet numbers - #2164
Conversation
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.
Merging this PR will improve performance by 6.59%1 warning
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
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.
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
XMLSchemaandRelaxNGcould 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
lengthortotalDigitswritten with leading zeros, is now read whole when the schema compiles.Fixes #1764.