leptris 1.9.426.1-aarch64-linux → 1.9.428.0-aarch64-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +1 -1
  3. data/lib/leptris/version.rb +1 -1
  4. data/lib/libleptris.so +0 -0
  5. data/vendor-src/README.md +1 -1
  6. data/vendor-src/libleptris/CHANGELOG.md +41 -0
  7. data/vendor-src/libleptris/CMakeLists.txt +1 -1
  8. data/vendor-src/libleptris/scripts/verify_manifest.py +11 -3
  9. data/vendor-src/libleptris/src/leptris/encoding/wrapper.c +13 -3
  10. data/vendor-src/libleptris/src/leptris/leptris_internal.h +19 -0
  11. data/vendor-src/libleptris/src/leptris/rng/parse.c +21 -5
  12. data/vendor-src/libleptris/src/leptris/rng/validate.c +284 -40
  13. data/vendor-src/libleptris/src/leptris/xpath/evaluator.c +59 -19
  14. data/vendor-src/libleptris/src/leptris/xpath/evaluator_operators.c +83 -25
  15. data/vendor-src/libleptris/src/leptris/xpath/parser.c +24 -0
  16. data/vendor-src/libleptris/src/leptris/xpath/vm.c +18 -9
  17. data/vendor-src/libleptris/src/leptris/xpath/xpath_compiled.c +13 -1
  18. data/vendor-src/libleptris/src/leptris/xslt/xslt_exec.c +16 -0
  19. data/vendor-src/libleptris/src/leptris/xslt/xslt_internal.h +6 -0
  20. data/vendor-src/libleptris/src/leptris/xslt/xslt_parse.c +20 -0
  21. data/vendor-src/libleptris/test/differential/README.md +9 -0
  22. data/vendor-src/libleptris/test/differential/package.json +1 -1
  23. data/vendor-src/libleptris/test/differential/rs-runner/Cargo.toml +1 -1
  24. data/vendor-src/libleptris/test/fuzz/corpus/encoding_decl_strstr_hbo.bin +1 -0
  25. data/vendor-src/libleptris/test/fuzz/corpus/xpath_bracket_nesting_stack_overflow.bin +0 -0
  26. data/vendor-src/libleptris/test/fuzz/corpus/xslt_apply_identity_recursion.bin +0 -0
  27. data/vendor-src/libleptris/test/fuzz/corpus/xslt_astcache_uaf_session.bin +0 -0
  28. data/vendor-src/libleptris/test/fuzz/corpus/xslt_carrier_number_uaf.bin +1 -0
  29. data/vendor-src/libleptris/test/fuzz/corpus/xslt_dispatch_union_dup_hbo.bin +0 -0
  30. data/vendor-src/libleptris/test/fuzz/corpus/xslt_mixed_set_dispose_uaf.bin +0 -0
  31. data/vendor-src/libleptris/test/fuzz/corpus/xslt_uaf_predicate_layer.bin +0 -0
  32. data/vendor-src/libleptris/test/fuzz/generators/gen_xml_seeds.py +132 -0
  33. data/vendor-src/libleptris/test/rng/test_rng.cpp +297 -0
  34. data/vendor-src/libleptris/test/xpath/test_xpath.cpp +77 -0
  35. data/vendor-src/libleptris/test/xpath/test_xslt.cpp +80 -0
  36. data/vendor-src/libleptris/vcpkg.json +1 -1
  37. metadata +10 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 10c9a23682562c64e2f4d93a15290f6a810ffd0a4566ae5e72bb5cc4c7729676
4
- data.tar.gz: cc28de773cd7fe6cbb0119865c7a52832284c70df7f60ff209baffd2ddbf7fb1
3
+ metadata.gz: bc7ca7b623a34074c13fda1ecdbc041cf5dc377642a9bd9d0396a0dc5a2a64d0
4
+ data.tar.gz: 9f8d153735d723a8e56f8a1bda9716e14d494edca2368db47d6784cda184450f
5
5
  SHA512:
6
- metadata.gz: cd892eb3f7a26eb722d3769975e4f25e7d1635aa6a9d9e745d17d97ec1410983ae69dc70aa3ffaef22892d6721460b5e02ced8a6b6a9d1339b37333662354649
7
- data.tar.gz: 9d8fd2e215416d131bd2f8cada7af1ba4b6b024ea168edd3400975e65e5e8d4d1d2daeb5d34b65e0fbeb5d2b3c802c83732a512048d2b1a7795e94cc3dcac30c
6
+ metadata.gz: 2529c0abf7a3d133858cc0cb8c736d7cfce16981e831306fabf8c4dc77d15c3a36d1f8bba13ebbc8653eb88763d266965af9466d98a52ed8eddbf66b2a789277
7
+ data.tar.gz: ab8b59839527723c04f068379a88811e614a00e9c2536af954df6b80ea9d1bbf260b13889207323057564ef9aea200f621e59b35f72b78a4dfd5121ac0a3b726
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ RSpec::Core::RakeTask.new(:spec)
8
8
  # Pin for `rake compile` and the platform-gem builds. Keep in lockstep
9
9
  # with .github/workflows/build.yml (which calls `rake compile`) and the
10
10
  # CHANGELOG when libleptris releases.
11
- LIBLEPTRIS_VERSION = "1.9.426"
11
+ LIBLEPTRIS_VERSION = "1.9.428"
12
12
 
13
13
  # THE SSOT for the per-minor native bundle list (ruby#645): one line,
14
14
  # space-separated, consumed by the Rakefile, the per-minor builder's
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Leptris
4
- VERSION = "1.9.426.1"
4
+ VERSION = "1.9.428.0"
5
5
  end
data/lib/libleptris.so CHANGED
Binary file
data/vendor-src/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Sources vendored in this gem (recompile rights; the ruby
2
2
  variant builds them at install via extconf.rb):
3
3
 
4
- - libleptris v1.9.426
4
+ - libleptris v1.9.428
5
5
  - utf8proc v2.11.0
6
6
 
7
7
  Rebuild by hand:
@@ -1,5 +1,46 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.9.428] - 2026-11-18
4
+
5
+ ### Fixed
6
+
7
+ - RELAX NG: a `combine="choice"`/`combine="interleave"` merge wraps each merged body in an implicit group (Jing §4.10) — the branch is the body's whole sibling chain, not its first element. Multi-element bodies under a combined define no longer reject everything after the first element (the openmath3.rng OMBIND shape — OM3 documents rejected OMBVAR/OMA after the head OMS) (leptris#2088, leptris#2089).
8
+
9
+ ### Memory safety (SOTA C1, fuzz-lane findings 8–11)
10
+
11
+ - XPath synthetic nodes (attribute/namespace/text) are shared by value across nodesets, sequences, and variable bindings; no set owns them exclusively. Disposing one no longer frees the struct under its other holders: the node parks (a defined-dead sentinel + thread-local graveyard drained at cleanup) instead (nightly 44085294402).
12
+ - Five binding producers (simple-map, sequence, for loop-var, HOF params, switch cases) flagged result sets eagerly and appended raw document nodes — a predicate drop then disposed a document-owned text node. Ownership flags now assert facts and transfer with owned members (leptris#2083).
13
+ - The encoding-declaration scan is bounded: the buffer is not NUL-terminated on every caller and the unbounded `strstr` read past the end.
14
+ - Two clean-error recursion fences: template application caps at 100 levels (an `apply-templates select="."` identity loop previously died on the OS stack), and the XPath parser rejects bracket nesting past 128 with a syntax error instead of exhausting the C stack (nightly 44278536885).
15
+
16
+ ### Packaging
17
+
18
+ - Every release now carries `build-manifest.json` (per-platform toolchain identities, artifact hashes, and the binding versions riding the release), generated by the release workflow; the byte-identity smoke is green after pinning the compiler's path-derived symbol seed; the differential gate reads its pins from the release manifest (leptris#2085, leptris#2086).
19
+
20
+
21
+
22
+ ## [1.9.427] - 2026-11-18
23
+
24
+ ### Fixed
25
+
26
+ The fuzz-lane's memory-safety wave plus the OpenMath2 RELAX NG
27
+ conformance wave — seven fixes in two days, every one from a
28
+ standalone-reproducible input.
29
+
30
+ - **xpath: the bytecode compiled from the wrong cache graph** (SOTA C1 finding 6, the last edge). Same expression text can hold different parses in different cache records — a compiled handle may carry the graveyard's graph (its insert pinned a pre-eviction parse) while the live slot holds a fresher one. The eval path compiled bytecode from the handle's graph but stored it under the expression key, so the slot's later holders ran bytecode whose constants pointed into a graph only the old handle kept alive, and the handle's teardown freed it under them. The bytecode now compiles from **the slot's own graph**; the crash regime went from 90-seconds-to-UAF to 2.2M clean runs.
31
+ - **xslt: the dispatch key lists dedupe their templates** (SOTA C1 finding 7). `match="…|t|…|t"` names the same leaf twice; every alternative appended the same template to the same dispatch key, running `k->n` past the allocation (heap-buffer-overflow at stylesheet parse). Both append sites dedupe per key — which also stops a template dispatching twice per node.
32
+ - **xpath: the union transfers ownership per side** (SOTA C1 finding 8). The union concatenated one leg when the other operand was empty but gated the #514 ownership transfer on both legs — a carrier nodeset unioned with an empty operand left the result sharing an owned node the owning operand freed at cleanup. The transfer is per side.
33
+ - **rng: wrapper bodies match as implicit groups** (#2078). `optional`/`zeroOrMore`/`oneOrMore` matched only their first child; the XML form flattens multi-child wrapper bodies as siblings (Jing §4.10) — openmath2.rng's OMATP `(a,b)+` rejected `<a/><b/>` while claiming to expect `b`. The three wrappers walk the whole child chain; `oneOrMore` repeats it whole.
34
+ - **rng: a choice of attributes selects exactly one branch as a set** (#2079). Per-attribute existence over the union of branches admitted `dec` AND `hex` together on OMF while the required pass demanded both. The attribute walkers carry a selection map (one branch per attr-choice, refs expanded, bounded search); a branch walks as a single node — the choice's children are siblings, and the old list walk crossed branches.
35
+ - **rng: `<data>` content lexes through the XSD builtin library** (#2080). `data_lexical_ok` handled only `integer`; base64Binary validated `not-base64!!!`. Known XSD types route through the engine's builtin lexers — one shared lexical space with the XSD validator (base64 padding/remainder rules, hex even-length; unknown types stay permissive per the RNG fallback contract).
36
+
37
+ ### Tooling
38
+
39
+ - The C1 lane's corpus now carries every pinned input from all eight findings (four UAF/SEGV classes, one overflow, two RNG conformance shapes); the nightly's grammar-aware generator regenerates 200 deep structures per run over them.
40
+
41
+
42
+
43
+
3
44
  ## [1.9.426] - 2026-11-17
4
45
 
5
46
  ### Fixed
@@ -3,7 +3,7 @@
3
3
  cmake_minimum_required(VERSION 3.20)
4
4
 
5
5
  project(leptris
6
- VERSION 1.9.426
6
+ VERSION 1.9.428
7
7
  DESCRIPTION "Fast XML parser and XPath evaluator in pure C"
8
8
  LANGUAGES C CXX
9
9
  )
@@ -25,13 +25,21 @@ matched a local file byte-for-byte; 1 = at least one artifact is missing
25
25
  or mismatched. A local file with no manifest entry is a warning, not a
26
26
  failure (extra build outputs are allowed).
27
27
 
28
- Manifest schema ("leptris.build-manifest/1", as produced by
28
+ Manifest schema ("leptris.build-manifest/2", as produced by
29
29
  .github/workflows/build-manifests.yml):
30
30
 
31
31
  {
32
- "schema": "leptris.build-manifest/1",
32
+ "schema": "leptris.build-manifest/2",
33
33
  "tag": "v1.9.367", // release tag built
34
34
  "generated_at_utc": "2026-10-21T00:00:00Z",
35
+ "bindings": { // schema/2 (A6/C3): the
36
+ "rubygems": "1.9.427.0", // published binding
37
+ "pypi": "1.9.427.0", // versions riding this
38
+ "npm": "0.1.62", // engine release, live
39
+ "crates_io": "1.9.0", // from the registries;
40
+ // differential.yml reads
41
+ // these as its pins
42
+ },
35
43
  "platforms": {
36
44
  "<platform>": { // key: matrix leg name, e.g. linux-amd64
37
45
  "leg": "native", // native | amalgamated
@@ -63,7 +71,7 @@ ARTIFACT_RE = re.compile(
63
71
  r"^(libleptris\.(a|dll|lib)|libleptris\.so(\.[0-9.]+)?"
64
72
  r"|libleptris\.[0-9.]+\.dylib|leptris\.(lib|dll))$")
65
73
 
66
- SCHEMA = "leptris.build-manifest/1"
74
+ SCHEMA = "leptris.build-manifest/2"
67
75
 
68
76
 
69
77
  def sha256_of(path):
@@ -36,9 +36,19 @@ char* leptris_encoding_parse_declaration(const char* xml, size_t len) {
36
36
  return NULL;
37
37
  }
38
38
 
39
- /* Find encoding=" */
40
- const char* encoding_start = strstr(xml, "encoding");
41
- if (!encoding_start || encoding_start - xml > (long)len) {
39
+ /* Find encoding= — BOUNDED: the buffer is NOT NUL-terminated on
40
+ * every caller (the fuzz harness passes exact-size buffers; the
41
+ * unbounded strstr read past the end — SOTA C1 finding 9,
42
+ * fx-art-crash-653f55991b38). */
43
+ const char* limit = xml + len;
44
+ const char* encoding_start = NULL;
45
+ for (const char* p = xml; p + 8 <= limit; p++) {
46
+ if (memcmp(p, "encoding", 8) == 0) {
47
+ encoding_start = p;
48
+ break;
49
+ }
50
+ }
51
+ if (!encoding_start) {
42
52
  return NULL;
43
53
  }
44
54
 
@@ -551,6 +551,20 @@ typedef struct xpath_text_node {
551
551
  struct leptris_element* owner; /* Parent element */
552
552
  } XPathTextNode;
553
553
 
554
+ /* Disposed-synthetic parking (SOTA C1, nightly 44085294402): the
555
+ * XPath-side heap nodes (LeptrisAttributeNode / LeptrisNamespaceNode
556
+ * / XPathTextNode) are SHARED by value across nodesets, sequences and
557
+ * variable bindings — no single set owns them exclusively, so a
558
+ * dispose by one holder must not free the struct (or its payload)
559
+ * out from under the others. xpath_nodeset_dispose_node therefore
560
+ * PARKS: the type byte moves to the sentinel below and the struct
561
+ * joins a thread-local graveyard (drained by
562
+ * leptris_xpath_drain_thread_caches at thread exit). Late readers see
563
+ * a defined dead node and defined-dead content instead of freed
564
+ * memory — the same contract as the XPATH_RESULT_CACHED result
565
+ * parking. */
566
+ #define LEPTRIS_NODE_XPATH_PARKED ((LeptrisNodeType)127)
567
+
554
568
  /* XPath node union - type-safe wrapper for all XPath node types */
555
569
  typedef union xpath_node {
556
570
  LeptrisNodeType type; /* First field for type checking */
@@ -673,6 +687,11 @@ typedef struct xpath_parser {
673
687
  size_t token_count;
674
688
  size_t token_pos;
675
689
  char error_msg[256];
690
+ int depth; /* Expression nesting fence — the
691
+ * parse_* chain recurses per '(' '['
692
+ * '!' and dies on the C stack past
693
+ * the fence (SOTA C1, nightly
694
+ * 44278536885 family). */
676
695
  } XPathParser;
677
696
 
678
697
  /* XPath nodeset - Holds typed node pointers (elements or attributes).
@@ -363,10 +363,20 @@ static int add_define(RngGrammar* g, char* name, char* combine,
363
363
  RngPattern* wrap =
364
364
  pat_new(strcmp(d->combine, "interleave") == 0 ? RNG_INTERLEAVE
365
365
  : RNG_CHOICE);
366
- pat_append(wrap, d->body);
367
- pat_append(wrap, body->first_child);
366
+ /* Each merged body is an IMPLICIT GROUP (Jing §4.10): the branch
367
+ * is the body's whole sibling chain, not its first element. A
368
+ * bare chain inside the combinator made the validator treat the
369
+ * body's 2nd..nth elements as further alternatives of the
370
+ * choice — <a/><b/> consumed a as the entire first branch and
371
+ * stranded b (openmath3 OMBIND, #2088). */
372
+ RngPattern* left = pat_new(RNG_GROUP);
373
+ pat_append(left, d->body);
374
+ RngPattern* right = pat_new(RNG_GROUP);
375
+ pat_append(right, body->first_child);
368
376
  body->first_child = NULL;
369
377
  rng_pattern_free(body);
378
+ pat_append(wrap, left);
379
+ pat_append(wrap, right);
370
380
  rng_flatten_combinator(wrap);
371
381
  d->body = wrap;
372
382
  return 1;
@@ -511,15 +521,21 @@ static int handle_include(RngGrammar* g, LeptrisElement inc,
511
521
  /* The WRAPPER stays as the define's body — same shape
512
522
  * add_define produces — the combinator node is what
513
523
  * the validator walks; unwrapping leaves bare
514
- * siblings with no interleave/choice between them. */
524
+ * siblings with no interleave/choice between them.
525
+ * Each branch wraps in GROUP (#2088, Jing §4.10): the
526
+ * branch is the body's whole sibling chain. */
515
527
  RngPattern* wrap =
516
528
  pat_new(strcmp(ncombine, "interleave") == 0
517
529
  ? RNG_INTERLEAVE
518
530
  : RNG_CHOICE);
519
- pat_append(wrap, d->body);
520
- pat_append(wrap, body->first_child);
531
+ RngPattern* left = pat_new(RNG_GROUP);
532
+ pat_append(left, d->body);
533
+ RngPattern* right = pat_new(RNG_GROUP);
534
+ pat_append(right, body->first_child);
521
535
  body->first_child = NULL;
522
536
  rng_pattern_free(body);
537
+ pat_append(wrap, left);
538
+ pat_append(wrap, right);
523
539
  rng_flatten_combinator(wrap);
524
540
  free(d->combine);
525
541
  d->combine = ncombine;
@@ -10,6 +10,9 @@
10
10
  */
11
11
  #include "rng_internal.h"
12
12
  #include "../dom/element.h"
13
+ /* #2080: <data> content lexes through the engine's XSD builtin
14
+ * library — one shared lexical space with the XSD validator. */
15
+ #include "../xsd/xsd_internal.h"
13
16
  #include <stdarg.h>
14
17
  #include <stdlib.h>
15
18
  #include <string.h>
@@ -126,6 +129,16 @@ static int data_lexical_ok(const RngPattern* data, const char* t) {
126
129
  for (; *q; q++)
127
130
  if (!isdigit((unsigned char)*q)) return 0;
128
131
  }
132
+ /* leptris#2080: <data> content lexes through the XSD builtin
133
+ * library — the engine's own lexical space (base64Binary admitted
134
+ * "not-base64!!!" before this; the datatypeLibrary default is the
135
+ * XSD datatypes). Unknown types stay permissive. */
136
+ if (data->datatype && *data->datatype) {
137
+ char qname[64];
138
+ snprintf(qname, sizeof(qname), "xs:%s", data->datatype);
139
+ if (xsd_builtin_known(qname))
140
+ return xsd_builtin_valid(qname, t) == 1;
141
+ }
129
142
  return 1;
130
143
  }
131
144
 
@@ -313,13 +326,134 @@ static int list_consumes_attr(RngVal* v, RngPattern* head,
313
326
  return 0;
314
327
  }
315
328
 
316
- /* Check the attribute patterns against e's actual attributes. */
329
+ /* ---- ruby#2079: whole-branch attribute-choice selection ----------
330
+ * A <choice> of attribute branches must pick EXACTLY ONE branch as
331
+ * a set: per-attribute existence over the union of branches let
332
+ * attributes from different branches combine (openmath2.rng's OMF
333
+ * admitted dec AND hex). The selection map assigns one branch per
334
+ * attr-choice node; the walkers consult it. Unassigned choices keep
335
+ * the union semantics (collection contexts). */
336
+ #define RNG_ATTR_SEL_MAX 16
337
+ #define RNG_ATTR_SEARCH_MAX_DEPTH 12
338
+
339
+ typedef struct {
340
+ RngPattern* choice;
341
+ RngPattern* branch;
342
+ } AttrSelEntry;
343
+ typedef struct {
344
+ AttrSelEntry e[RNG_ATTR_SEL_MAX];
345
+ size_t n;
346
+ } AttrSel;
347
+
348
+ static int check_required_list_sel(RngVal* v, RngPattern* head,
349
+ LeptrisElement e, const AttrSel* sel);
317
350
  static int check_required_list(RngVal* v, RngPattern* head,
318
351
  LeptrisElement e) {
352
+ return check_required_list_sel(v, head, e, NULL);
353
+ }
354
+
355
+ static RngPattern* attr_sel_branch(const AttrSel* sel,
356
+ const RngPattern* choice) {
357
+ if (!sel) return NULL;
358
+ for (size_t i = 0; i < sel->n; i++)
359
+ if (sel->e[i].choice == choice) return sel->e[i].branch;
360
+ return NULL;
361
+ }
362
+
363
+ static int list_consumes_attr_sel(RngVal* v, RngPattern* head,
364
+ LeptrisElement e, const char* name,
365
+ const AttrSel* sel);
366
+ static int pattern_consumes_attr_sel(RngVal* v, RngPattern* p,
367
+ LeptrisElement e, const char* name,
368
+ const AttrSel* sel) {
369
+ if (sel) {
370
+ RngPattern* b = attr_sel_branch(sel, p);
371
+ if (p->kind == RNG_CHOICE && b)
372
+ return list_consumes_attr_sel(v, b, e, name, sel);
373
+ if (p->kind == RNG_CHOICE && !b && p->first_child &&
374
+ p->first_child->kind == RNG_ATTRIBUTE &&
375
+ !p->first_child->next) {
376
+ /* A single-attribute choice with no assignment is
377
+ * decided by the attribute itself (its own union). */
378
+ }
379
+ }
380
+ return list_consumes_attr_sel(v, p->first_child, e, name, sel);
381
+ }
382
+
383
+ static int node_consumes_attr_sel(RngVal* v, RngPattern* c,
384
+ LeptrisElement e, const char* name,
385
+ const AttrSel* sel);
386
+ static int list_consumes_attr_sel(RngVal* v, RngPattern* head,
387
+ LeptrisElement e, const char* name,
388
+ const AttrSel* sel) {
319
389
  for (RngPattern* c = head; c; c = c->next) {
390
+ if (node_consumes_attr_sel(v, c, e, name, sel)) return 1;
391
+ }
392
+ return 0;
393
+ }
394
+
395
+ /* ONE node — a branch selection must not walk the sibling chain
396
+ * (ruby#2079: the choice's children are siblings; a branch walk that
397
+ * follows ->next crosses into the OTHER branches). */
398
+ static int node_consumes_attr_sel(RngVal* v, RngPattern* c,
399
+ LeptrisElement e, const char* name,
400
+ const AttrSel* sel) {
401
+ {
402
+ switch (c->kind) {
403
+ case RNG_ATTRIBUTE:
404
+ if (c->any_name) {
405
+ const char* got = leptris_element_attribute(e, name);
406
+ return attr_content_satisfied(v, c, got ? got : "");
407
+ }
408
+ if (c->name && strcmp(c->name, name) == 0) {
409
+ const char* got = leptris_element_attribute(e, name);
410
+ return attr_content_satisfied(v, c, got ? got : "");
411
+ }
412
+ break;
413
+ case RNG_OPTIONAL: case RNG_ZERO_OR_MORE:
414
+ case RNG_GROUP: case RNG_INTERLEAVE:
415
+ if (pattern_consumes_attr_sel(v, c, e, name, sel)) return 1;
416
+ break;
417
+ case RNG_CHOICE: {
418
+ RngPattern* b = attr_sel_branch(sel, c);
419
+ if (b) {
420
+ if (node_consumes_attr_sel(v, b, e, name, sel))
421
+ return 1;
422
+ break;
423
+ }
424
+ /* Unassigned: union semantics (collection contexts). */
425
+ if (pattern_consumes_attr(v, c, e, name)) return 1;
426
+ break;
427
+ }
428
+ case RNG_REF: {
429
+ RngDefine* d = find_define(v->g, c->name);
430
+ if (d && d->body &&
431
+ list_consumes_attr_sel(v, d->body, e, name, sel))
432
+ return 1;
433
+ break;
434
+ }
435
+ default: break;
436
+ }
437
+ }
438
+ return 0;
439
+ }
440
+
441
+ static int check_required_node_sel(RngVal* v, RngPattern* c,
442
+ LeptrisElement e, const AttrSel* sel);
443
+ static int check_required_list_sel(RngVal* v, RngPattern* head,
444
+ LeptrisElement e, const AttrSel* sel) {
445
+ for (RngPattern* c = head; c; c = c->next) {
446
+ if (!check_required_node_sel(v, c, e, sel)) return 0;
447
+ }
448
+ return 1;
449
+ }
450
+
451
+ static int check_required_node_sel(RngVal* v, RngPattern* c,
452
+ LeptrisElement e, const AttrSel* sel) {
453
+ {
320
454
  switch (c->kind) {
321
455
  case RNG_ATTRIBUTE: {
322
- if (c->any_name) break; /* wildcard: satisfiable */
456
+ if (c->any_name) break;
323
457
  const char* got = leptris_element_attribute(e, c->name);
324
458
  if (!got) {
325
459
  diag(v, LEPTRIS_DIAG_MISSING_REQUIRED_ATTR, e,
@@ -330,33 +464,121 @@ static int check_required_list(RngVal* v, RngPattern* head,
330
464
  break;
331
465
  }
332
466
  case RNG_GROUP: case RNG_INTERLEAVE: case RNG_ONE_OR_MORE:
333
- if (!check_required_list(v, c->first_child, e)) return 0;
467
+ if (!check_required_list_sel(v, c->first_child, e, sel))
468
+ return 0;
334
469
  break;
335
470
  case RNG_REF: {
336
- /* #1164: a required attribute behind <ref> is as
337
- * required as an inline one — metanorma bibdata
338
- * schemas are ref'd attribute groups throughout */
339
471
  RngDefine* d = find_define(v->g, c->name);
340
472
  if (d && d->body &&
341
- !check_required_list(v, d->body, e))
473
+ !check_required_list_sel(v, d->body, e, sel))
342
474
  return 0;
343
475
  break;
344
476
  }
345
- case RNG_CHOICE:
346
- /* Simplified: any alternative with all its attrs. */
347
- if (!check_required_list(v, c->first_child, e)) return 0;
477
+ case RNG_CHOICE: {
478
+ RngPattern* b = attr_sel_branch(sel, c);
479
+ if (b) {
480
+ if (!check_required_node_sel(v, b, e, sel)) return 0;
481
+ break;
482
+ }
483
+ int any_ok = 0;
484
+ for (RngPattern* alt = c->first_child; alt && !any_ok;
485
+ alt = alt->next) {
486
+ v->failed = 0;
487
+ v->err[0] = 0;
488
+ any_ok = check_required_list_sel(v, alt, e, sel);
489
+ }
490
+ if (!any_ok) return 0;
348
491
  break;
492
+ }
349
493
  default: break;
350
494
  }
351
495
  }
352
496
  return 1;
353
497
  }
354
498
 
355
- static int check_required_attrs(RngVal* v, RngPattern* p, LeptrisElement e) {
356
- return check_required_list(v, p->first_child, e);
499
+ /* Collect the attr-consuming CHOICE nodes through the content
500
+ * (refs expanded, depth-capped). */
501
+ static void collect_attr_choices(RngVal* v, RngPattern* p, int depth,
502
+ AttrSel* out) {
503
+ if (!p || depth > 24) return;
504
+ for (RngPattern* c = p->first_child; c; c = c->next) {
505
+ if (c->kind == RNG_CHOICE) {
506
+ int consumes = 0;
507
+ for (RngPattern* b = c->first_child; b && !consumes;
508
+ b = b->next)
509
+ if (b->kind == RNG_ATTRIBUTE) consumes = 1;
510
+ if (consumes && out->n < RNG_ATTR_SEL_MAX) {
511
+ out->e[out->n].choice = c;
512
+ out->e[out->n].branch = NULL;
513
+ out->n++;
514
+ }
515
+ }
516
+ if (c->kind == RNG_REF) {
517
+ RngDefine* d = find_define(v->g, c->name);
518
+ if (d && d->body) collect_attr_choices(v, d->body, depth + 1, out);
519
+ continue;
520
+ }
521
+ collect_attr_choices(v, c, depth + 1, out);
522
+ }
523
+ }
524
+
525
+ static int check_required_attrs_sel(RngVal* v, RngPattern* p,
526
+ LeptrisElement e, const AttrSel* sel) {
527
+ return check_required_list_sel(v, p->first_child, e, sel);
528
+ }
529
+
530
+ /* One candidate assignment: every instance attribute consumed, every
531
+ * requirement satisfied — under THIS selection. */
532
+ static int match_attrs_with_sel(RngVal* v, RngPattern* content,
533
+ LeptrisElement e, const AttrSel* sel) {
534
+ v->failed = 0;
535
+ v->err[0] = 0;
536
+ for (struct leptris_attribute* a =
537
+ leptris_element_get_first_attribute(e);
538
+ a; a = leptris_attr_next(a)) {
539
+ if (!pattern_consumes_attr_sel(v, content, e, attr_cname(a), sel))
540
+ return 0;
541
+ }
542
+ return check_required_attrs_sel(v, content, e, sel);
543
+ }
544
+
545
+ /* The bounded assignment search: one branch per attr-choice. */
546
+ static int attr_sel_search(RngVal* v, RngPattern* content,
547
+ LeptrisElement e, AttrSel* sel, int depth) {
548
+ if (depth > RNG_ATTR_SEARCH_MAX_DEPTH) return 0;
549
+ for (size_t i = 0; i < sel->n; i++) {
550
+ if (sel->e[i].branch) continue;
551
+ for (RngPattern* b = sel->e[i].choice->first_child; b;
552
+ b = b->next) {
553
+ sel->e[i].branch = b;
554
+ v->failed = 0;
555
+ v->err[0] = 0;
556
+ if (attr_sel_search(v, content, e, sel, depth + 1)) return 1;
557
+ sel->e[i].branch = NULL;
558
+ }
559
+ return 0;
560
+ }
561
+ return match_attrs_with_sel(v, content, e, sel);
562
+ }
563
+
564
+ static int check_required_attrs(RngVal* v, RngPattern* p,
565
+ LeptrisElement e) {
566
+ return check_required_list_sel(v, p->first_child, e, NULL);
357
567
  }
358
568
 
359
569
  static int match_attrs(RngVal* v, RngPattern* content, LeptrisElement e) {
570
+ /* ruby#2079: attr-consuming choices exist → whole-branch
571
+ * selection search. Otherwise the union path is exact. */
572
+ AttrSel sel;
573
+ memset(&sel, 0, sizeof(sel));
574
+ collect_attr_choices(v, content, 0, &sel);
575
+ if (sel.n > 0) {
576
+ if (attr_sel_search(v, content, e, &sel, 0)) return 1;
577
+ diag(v, LEPTRIS_DIAG_ATTR_NOT_ALLOWED, e,
578
+ "attribute combination matches no branch of the "
579
+ "attribute choice", NULL);
580
+ return 0;
581
+ }
360
582
  /* Every instance attribute must be consumed by the content. */
361
583
  for (struct leptris_attribute* a = leptris_element_get_first_attribute(e);
362
584
  a; a = leptris_attr_next(a)) {
@@ -492,44 +714,66 @@ static size_t match_seq(RngVal* v, RngPattern* p, LeptrisNodeRef* kids,
492
714
  return i;
493
715
  }
494
716
  case RNG_OPTIONAL: {
495
- size_t r = match_seq(v, p->first_child, kids, n, idx);
496
- if (r == (size_t)-1) {
497
- /* The branch is skippable: an inner mismatch is
498
- * speculative, not a failure — clear the latch like
499
- * ZERO_OR_MORE/CHOICE do, or the stuck failure
500
- * poisons every later match (regression since the
501
- * #878 vocabulary pass made silent mismatches
502
- * loud; optional-omitted documents validated
503
- * false). */
504
- v->failed = 0;
505
- v->err[0] = 0;
506
- return idx;
717
+ /* ruby-adjacent leptris#2078: the XML form flattens a
718
+ * multi-child wrapper body as siblings — an implicit
719
+ * group (Jing §4.10). Walk the WHOLE child chain; the
720
+ * old first_child-only hand dropped every sibling past
721
+ * the first (openmath2.rng's OMATP). */
722
+ size_t i = idx;
723
+ for (RngPattern* c = p->first_child; c; c = c->next) {
724
+ i = match_seq(v, c, kids, n, i);
725
+ if (i == (size_t)-1) {
726
+ /* The branch is skippable: an inner mismatch is
727
+ * speculative, not a failure — clear the latch like
728
+ * ZERO_OR_MORE/CHOICE do, or the stuck failure
729
+ * poisons every later match (regression since the
730
+ * #878 vocabulary pass made silent mismatches
731
+ * loud; optional-omitted documents validated
732
+ * false). */
733
+ v->failed = 0;
734
+ v->err[0] = 0;
735
+ return idx;
736
+ }
507
737
  }
508
- return r;
738
+ return i;
509
739
  }
510
740
  case RNG_ZERO_OR_MORE: {
511
741
  for (;;) {
512
- size_t r = match_seq(v, p->first_child, kids, n, idx);
513
- if (r == (size_t)-1 || r == idx) {
514
- v->failed = 0;
515
- v->err[0] = 0;
516
- return idx;
742
+ size_t i = idx;
743
+ for (RngPattern* c = p->first_child; c; c = c->next) {
744
+ i = match_seq(v, c, kids, n, i);
745
+ if (i == (size_t)-1) {
746
+ v->failed = 0;
747
+ v->err[0] = 0;
748
+ return idx;
749
+ }
517
750
  }
518
- idx = r;
751
+ if (i == idx) return idx;
752
+ idx = i;
519
753
  }
520
754
  }
521
755
  case RNG_ONE_OR_MORE: {
522
- size_t r = match_seq(v, p->first_child, kids, n, idx);
523
- if (r == (size_t)-1) return (size_t)-1;
524
- idx = r;
756
+ /* First iteration: the whole implicit-group body must
757
+ * match (#2078). */
758
+ size_t i = idx;
759
+ for (RngPattern* c = p->first_child; c; c = c->next) {
760
+ i = match_seq(v, c, kids, n, i);
761
+ if (i == (size_t)-1) return (size_t)-1;
762
+ }
763
+ /* Subsequent iterations: repeat the body while it makes
764
+ * progress. */
525
765
  for (;;) {
526
- r = match_seq(v, p->first_child, kids, n, idx);
527
- if (r == (size_t)-1 || r == idx) {
528
- v->failed = 0;
529
- v->err[0] = 0;
530
- return idx;
766
+ size_t j = i;
767
+ for (RngPattern* c = p->first_child; c; c = c->next) {
768
+ j = match_seq(v, c, kids, n, j);
769
+ if (j == (size_t)-1) {
770
+ v->failed = 0;
771
+ v->err[0] = 0;
772
+ return i;
773
+ }
531
774
  }
532
- idx = r;
775
+ if (j == i) return i;
776
+ i = j;
533
777
  }
534
778
  }
535
779
  case RNG_EMPTY: