leptris 1.9.342.0 → 1.9.344.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d35a3680b1e44935421bd7896695114dae8c8038f9ec61250acf5b0f7a127fb
4
- data.tar.gz: 53ab93d7bf80c535f3dd7f1c5c8d4beeb3c58a3667a42bfce94a7810e93324a8
3
+ metadata.gz: 62d99dc0e68374b1d31c7ff4267fa3411fc89cf840d1a8be43a8d72d788f3266
4
+ data.tar.gz: 8fb1dc27250a8ea169674d90c0a3a76c3f40076cf225a8e03e78289743e732c1
5
5
  SHA512:
6
- metadata.gz: 4996a04e9d83156b0b5384d90f29299cc1f9c21cb9b51f2e627f2949d6dbd310440ecf4842fd66ddedb28093d430441731ecd3e4703390abb56b9f9d0ef9acbc
7
- data.tar.gz: c3d1a0e6a67cbef6cab74096f82d0d03d574c67c81515d0a1a46aa86042ed6b9045efd62c339fccb7ac89a645f8cc482c3928c8573221c0677de66d66ddc575f
6
+ metadata.gz: 526f2d52e34df4f2c70cebd7dba92d9fe203d2f42b5e9af712345a709e76cc91919f83d7b584cdd85fb7f8e13c67a31443230cb5dfeeec3830c5cde7885dd2ce
7
+ data.tar.gz: bf743b9e75c3f820939a6d77f31271131364ad63a25078a39ff932180ecb8241422891b1648303b8ac34a4d472c2f693c0d3c1dc91ab8eb3a6ab811730085d20
data/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ ## [1.9.344.0] - 2026-10-12
2
+
3
+ ### Fixed — engine sync (libleptris 1.9.343-1.9.344)
4
+
5
+ - **recover matches XML_PARSE_RECOVER (#1660)**: structural
6
+ malformations commit the best-effort tree — a close tag naming
7
+ an open ancestor closes intermediates implicitly, a stray in-tree
8
+ close is dropped, unclosed elements close at EOF (pinned for
9
+ both shapes). Residual edge reported upstream: trailing
10
+ content after the root close still fails the parse
11
+ (comment on leptris#1660).
12
+ - plan_result_build SCALAR ops validate plan_index (the OOB
13
+ shape from the #425/#426 line) and mid-build failures no
14
+ longer leak the partially built tree; GCC 14 compile fix.
15
+
1
16
  ## [1.9.342.0] - 2026-10-12
2
17
 
3
18
  ### Fixed — engine sync (libleptris 1.9.342)
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.342"
11
+ LIBLEPTRIS_VERSION = "1.9.344"
12
12
  # Vendored alongside libleptris for fn:normalize-unicode (TODO
13
13
  # .restructure/20): built per platform with a RELOCATABLE @rpath
14
14
  # install name, loaded by ffi.rb before libleptris so the
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Leptris
4
- VERSION = "1.9.342.0"
4
+ VERSION = "1.9.344.0"
5
5
  end
@@ -48,6 +48,28 @@ module Leptris::XML::XSD
48
48
  end
49
49
  end
50
50
 
51
+ # Deterministic drop (#362.3 / moxml): frees the compiled
52
+ # schema NOW instead of waiting for GC — long-lived validation
53
+ # services holding dozens of schemas stop parking engine-side
54
+ # trees for the process lifetime. The AutoPointer's GC
55
+ # finalizer is disarmed first (autorelease=false), so the free
56
+ # happens exactly once. Faces on a released schema raise.
57
+ #
58
+ # schema = Leptris::XML::XSD.compile_file(path)
59
+ # schema.release # => true
60
+ # schema.released? # => true
61
+ def release
62
+ return true if released?
63
+ @handle.autorelease = false
64
+ Leptris::XML::FFI.leptris_xsd_free(@handle)
65
+ @released = true
66
+ true
67
+ end
68
+
69
+ def released?
70
+ instance_variable_defined?(:@released) ? @released : false
71
+ end
72
+
51
73
  # Compile a schema from XML text. Raises Error when the text is
52
74
  # not usable at all (not well-formed, no root); schema-level
53
75
  # problems compile to a handle whose #error carries the detail
@@ -82,12 +104,14 @@ module Leptris::XML::XSD
82
104
  # Number of top-level schema declarations the compiler
83
105
  # recognized (elements, types, groups, ...).
84
106
  def declaration_count
107
+ check_released!
85
108
  Leptris::XML::FFI.leptris_xsd_declaration_count(@handle)
86
109
  end
87
110
 
88
111
  # The engine's schema-level error message, or nil when the
89
112
  # schema compiled cleanly.
90
113
  def error
114
+ check_released!
91
115
  msg = Leptris::XML::FFI.leptris_xsd_error(@handle)
92
116
  msg.nil? || msg.empty? ? nil : msg
93
117
  end
@@ -97,6 +121,7 @@ module Leptris::XML::XSD
97
121
  # name) — restriction chains validate every hop's facets,
98
122
  # cycle-guarded. Unknown type names raise (the -1 contract).
99
123
  def simple_valid?(type_name, lexical)
124
+ check_released!
100
125
  tri_state(
101
126
  Leptris::XML::FFI.leptris_xsd_simple_valid(
102
127
  @handle, type_name.to_s, lexical.to_s),
@@ -109,6 +134,7 @@ module Leptris::XML::XSD
109
134
  # and child content models through the Thompson NFA. Returns
110
135
  # true/false.
111
136
  def valid?(document)
137
+ check_released!
112
138
  tri_state(
113
139
  Leptris::XML::FFI.leptris_xsd_validate(
114
140
  @handle, document.c_ptr), "invalid document handle")
@@ -117,6 +143,7 @@ module Leptris::XML::XSD
117
143
  # The enumerated failure list of the last #validate run
118
144
  # (empty when valid; the NEXT validate replaces the list).
119
145
  def validate_errors(document)
146
+ check_released!
120
147
  valid?(document)
121
148
  Leptris::XML::FFI.leptris_xsd_error_count(@handle).times.map do |i|
122
149
  Leptris::XML::FFI.leptris_xsd_error_at(@handle, i)
@@ -129,6 +156,7 @@ module Leptris::XML::XSD
129
156
  # + effective namespace extracted per child). Unknown element
130
157
  # declarations raise (the -1 contract).
131
158
  def content_valid?(element_name, children)
159
+ check_released!
132
160
  kids = children.to_a
133
161
  nb, na = Leptris::XML::CStringArray.to_c(
134
162
  kids.map { |c| c.name.to_s })
@@ -142,6 +170,11 @@ module Leptris::XML::XSD
142
170
 
143
171
  private
144
172
 
173
+ def check_released!
174
+ raise Leptris::XML::UseAfterFreeError,
175
+ "schema released (moxml #362.3)" if released?
176
+ end
177
+
145
178
  # A child's effective namespace URI (nil = none) — read
146
179
  # through its own c_ptr, so docless wrappers work.
147
180
  def child_ns_uri(node)
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.342
4
+ - libleptris v1.9.344
5
5
  - utf8proc v2.11.0
6
6
 
7
7
  Rebuild by hand:
@@ -1,5 +1,47 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.9.344] - 2026-10-12
4
+
5
+ ### Fixed
6
+
7
+ - **recover: structural errors keep the best-effort tree
8
+ (leptris#1660, PR #1661).** #547's recover mode returned an
9
+ EMPTY document on any parse failure - the opposite of
10
+ XML_PARSE_RECOVER, for exactly the malformations recovery
11
+ exists for. The new LEPTRIS_PARSE_RECOVER flag (which
12
+ leptris_parse_options.recover maps to) gives libxml2
13
+ semantics: a close tag naming an open ancestor closes the
14
+ intermediates implicitly, a stray close is dropped, and
15
+ elements still open at EOF close - the tree commits in every
16
+ case. Each recovery records a parse diagnostic (two new
17
+ append-only kinds: MISMATCHED_TAG, UNCLOSED_ELEMENT) through
18
+ the #1200 channel; without the flag parses fail exactly as
19
+ before. Red-first ParseRecover specs; the #547
20
+ empty-document contract spec superseded by
21
+ RecoverKeepsBestEffortTree. Full suite 1983/1983.
22
+
23
+
24
+ ## [1.9.343] - 2026-10-12
25
+
26
+ ### Fixed
27
+
28
+ - **plan_result_build: SCALAR ops validate plan_index
29
+ (leptris#1654, PR #1658).** The ELEMENT branch validated the
30
+ pool bounds; the SCALAR branch dereferenced first — an
31
+ out-of-pool index was an OOB read (segfault or fabricated
32
+ output). SCALAR now rejects with LEPTRIS_ERROR_INVALID_ARG
33
+ like ELEMENT. The red spec crashes on the pre-fix build; it
34
+ also exposed (and the PR fixes) that every mid-build failure
35
+ return leaked the partially built value tree — caught by
36
+ LSan on the Linux CI ASAN lane.
37
+ - **GCC 14 compile fix (leptris#1656, PR #1658).**
38
+ dp_mint_element passed char** attribute arrays to the
39
+ const char** parameters of leptris_element_new_with_attributes
40
+ — GCC 14 errors (all manylinux/musllinux vendored builds);
41
+ clang only warned. Explicit casts at the call site; the
42
+ -Werror compile gate was red pre-fix, clean now.
43
+
44
+
3
45
  ## [1.9.342] - 2026-10-11
4
46
 
5
47
  ### Fixed
@@ -3,7 +3,7 @@
3
3
  cmake_minimum_required(VERSION 3.20)
4
4
 
5
5
  project(leptris
6
- VERSION 1.9.342
6
+ VERSION 1.9.344
7
7
  DESCRIPTION "Fast XML parser and XPath evaluator in pure C"
8
8
  LANGUAGES C CXX
9
9
  )
@@ -238,7 +238,14 @@ typedef enum {
238
238
  * - SKIP_SOURCE_POSITIONS: element source columns degrade to
239
239
  * zeros (line still resolves from the buffer). */
240
240
  LEPTRIS_PARSE_SKIP_DUP_DETECTION = 8u,
241
- LEPTRIS_PARSE_SKIP_SOURCE_POSITIONS = 16u
241
+ LEPTRIS_PARSE_SKIP_SOURCE_POSITIONS = 16u,
242
+ /* XML_PARSE_RECOVER parity (leptris#1660): structural errors
243
+ * keep the best-effort tree — a close tag matching an open
244
+ * ANCESTOR closes the intermediates implicitly, a stray close
245
+ * is dropped, unclosed elements close at EOF. Each recovery
246
+ * records a parse diagnostic (mismatched-tag / unclosed-element
247
+ * kind). Without the bit the parse fails as before. */
248
+ LEPTRIS_PARSE_RECOVER = 32u
242
249
  } LeptrisParseFlags;
243
250
 
244
251
  /* ============================================================================
@@ -278,6 +285,11 @@ typedef enum {
278
285
  * was not conformant (duplicate attribute, ...). Not a model
279
286
  * error; SAX-lane "recover" parity (#1200). */
280
287
  LEPTRIS_DIAG_RECOVER,
288
+ /* leptris#1660: structural recoveries — a close tag did not
289
+ * match the open element (dropped, or closed ancestors
290
+ * implicitly), and an element still open at EOF. Append-only. */
291
+ LEPTRIS_DIAG_MISMATCHED_TAG,
292
+ LEPTRIS_DIAG_UNCLOSED_ELEMENT,
281
293
  LEPTRIS_DIAG_COUNT_
282
294
  } LeptrisDiagKind;
283
295
 
@@ -1686,8 +1686,12 @@ static LeptrisElement dp_mint_element(LeptrisDocument doc,
1686
1686
  ? dp_join_colon(v->ns_prefix, local ? local : "")
1687
1687
  : dp_strdup(local ? local : "");
1688
1688
  if (!name) return NULL;
1689
+ /* char*[] reads as const char*[] element-wise; the DOUBLE
1690
+ * pointer conversion is the part C rejects (GCC 14 errors,
1691
+ * leptris#1656). */
1689
1692
  LeptrisElement el = leptris_element_new_with_attributes(
1690
- doc, name, v->attr_names, v->attr_values, v->attr_count);
1693
+ doc, name, (const char**)v->attr_names,
1694
+ (const char**)v->attr_values, v->attr_count);
1691
1695
  free(name);
1692
1696
  if (!el) return NULL;
1693
1697
  const dp_plan* kids_container = &poolp->plans[v->container_plan];
@@ -1776,6 +1780,9 @@ LEPTRIS_API LeptrisPlanResult leptris_plan_result_build(
1776
1780
  (op->row_index != UINT32_MAX &&
1777
1781
  op->row_index >= pool->plans[op->plan_index]
1778
1782
  .child_count)) {
1783
+ /* Mid-build failure: without this the partial
1784
+ * tree leaks (LSan on Linux CI catches it). */
1785
+ if (root) dp_result_free_rec(root);
1779
1786
  free(stack);
1780
1787
  if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1781
1788
  return NULL;
@@ -1818,6 +1825,18 @@ LEPTRIS_API LeptrisPlanResult leptris_plan_result_build(
1818
1825
  stack[sp++] = v;
1819
1826
  } else if (op->kind == LEPTRIS_PLAN_OP_SCALAR) {
1820
1827
  if (sp == 0) {
1828
+ /* Mid-build failure: without this the partial
1829
+ * tree leaks (LSan on Linux CI catches it). */
1830
+ if (root) dp_result_free_rec(root);
1831
+ free(stack);
1832
+ if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1833
+ return NULL;
1834
+ }
1835
+ /* The ELEMENT branch validates the pool bounds; SCALAR
1836
+ * reads cp->child_count right after, so an out-of-pool
1837
+ * plan_index here is an OOB read (leptris#1654). */
1838
+ if (op->plan_index >= pool->plan_count) {
1839
+ if (root) dp_result_free_rec(root);
1821
1840
  free(stack);
1822
1841
  if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1823
1842
  return NULL;
@@ -1839,6 +1858,9 @@ LEPTRIS_API LeptrisPlanResult leptris_plan_result_build(
1839
1858
  }
1840
1859
  } else if (op->kind == LEPTRIS_PLAN_OP_ATTR) {
1841
1860
  if (sp == 0 || !op->name) {
1861
+ /* Mid-build failure: without this the partial
1862
+ * tree leaks (LSan on Linux CI catches it). */
1863
+ if (root) dp_result_free_rec(root);
1842
1864
  free(stack);
1843
1865
  if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1844
1866
  return NULL;
@@ -1875,6 +1897,9 @@ LEPTRIS_API LeptrisPlanResult leptris_plan_result_build(
1875
1897
  el->attr_count++;
1876
1898
  } else if (op->kind == LEPTRIS_PLAN_OP_END) {
1877
1899
  if (sp == 0) {
1900
+ /* Mid-build failure: without this the partial
1901
+ * tree leaks (LSan on Linux CI catches it). */
1902
+ if (root) dp_result_free_rec(root);
1878
1903
  free(stack);
1879
1904
  if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1880
1905
  return NULL;
@@ -162,6 +162,9 @@ typedef struct {
162
162
  * as ENTITY_REF nodes; text runs split around them. Character
163
163
  * references (&#...;) still expand — they are not entities. */
164
164
  int keep_entity_refs;
165
+ /* LEPTRIS_PARSE_RECOVER (leptris#1660): structural errors keep
166
+ * the best-effort tree (XML_PARSE_RECOVER semantics). */
167
+ int recover;
165
168
  /* TODO 159 Phase G: parser-local last-attr cache for the element
166
169
  * currently being parsed. Eliminates the O(N) walk-to-find-tail
167
170
  * in dp_add_attr_inline — O(1) wiring per attr instead of O(N),
@@ -1379,7 +1382,8 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
1379
1382
  int owns_buffer,
1380
1383
  int drop_ws_text,
1381
1384
  int apply_dtd_attrs,
1382
- int keep_entity_refs) {
1385
+ int keep_entity_refs,
1386
+ int recover) {
1383
1387
 
1384
1388
  /* 2. Create arena-backed pool (TODO 183 Phase 3).
1385
1389
  *
@@ -1629,6 +1633,7 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
1629
1633
  p.probe_slack = 1; /* the scratch always carries zeroed slack */
1630
1634
  p.drop_ws_text = drop_ws_text;
1631
1635
  p.keep_entity_refs = keep_entity_refs;
1636
+ p.recover = recover;
1632
1637
  p.dtd = NULL;
1633
1638
  /* Only ever set to 1 in the loop; without the zero here the
1634
1639
  * field rides the stack (MSVC C4701) and doc->has_namespaces
@@ -2028,7 +2033,7 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
2028
2033
  close_local = colon + 1;
2029
2034
  close_local_len = close_len - (colon + 1 - close_start);
2030
2035
  }
2031
- if (open_len != close_local_len) goto fail;
2036
+ if (open_len != close_local_len) goto close_mismatch;
2032
2037
  /* Masked 64-bit compare for short names — one load
2033
2038
  * each side vs a libc memcmp call (the TODO 174 law:
2034
2039
  * setup dominates sub-8-byte compares; ~9% of the
@@ -2061,11 +2066,76 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
2061
2066
  uint64_t a, b;
2062
2067
  memcpy(&a, open_name, 8);
2063
2068
  memcpy(&b, close_local, 8);
2064
- if ((a & mask) != (b & mask)) goto fail;
2069
+ if ((a & mask) != (b & mask)) goto close_mismatch;
2065
2070
  } else if (memcmp(open_name, close_local,
2066
2071
  close_local_len) != 0) {
2067
- goto fail;
2072
+ goto close_mismatch;
2073
+ }
2074
+ if (0) {
2075
+ close_mismatch:
2076
+ /* leptris#1660: RECOVER keeps the best-effort tree.
2077
+ * A close tag naming an open ANCESTOR closes the
2078
+ * intermediates implicitly (libxml2 semantics); a
2079
+ * close matching nothing open is dropped. Either way
2080
+ * the recovery records a diagnostic. */
2081
+ if (!p.recover) goto fail;
2082
+ {
2083
+ int anc = -1;
2084
+ for (int d = (int)p.depth - 1; d >= 0; d--) {
2085
+ LeptrisElement ae = p.open_stack[d];
2086
+ const char* an = elem_name(ae);
2087
+ size_t al = (ae->name_len != 0xFF)
2088
+ ? (size_t)ae->name_len : strlen(an);
2089
+ if (al == close_local_len &&
2090
+ memcmp(an, close_local,
2091
+ close_local_len) == 0) {
2092
+ anc = d;
2093
+ break;
2094
+ }
2095
+ }
2096
+ if (anc >= 0) {
2097
+ while ((int)p.depth - 1 > anc) {
2098
+ LeptrisElement ce =
2099
+ p.open_stack[p.depth - 1];
2100
+ extern struct LeptrisDiag*
2101
+ leptris_diag_emit(
2102
+ struct LeptrisDiag**, int*, int*,
2103
+ LeptrisDiagKind, LeptrisNodeRef,
2104
+ const char*, ...);
2105
+ leptris_diag_emit(
2106
+ &p.doc->parse_diags,
2107
+ &p.doc->parse_diag_count,
2108
+ &p.doc->parse_diag_cap,
2109
+ LEPTRIS_DIAG_UNCLOSED_ELEMENT, NULL,
2110
+ "Element %s closed implicitly by </%.*s>",
2111
+ elem_name(ce),
2112
+ (int)close_local_len, close_local);
2113
+ if (p.line_offsets_ok) {
2114
+ leptris_elem_pos_close_at(
2115
+ p.doc,
2116
+ p.pos_slot_stack[p.depth - 1],
2117
+ (uint32_t)(p.pos - p.buf));
2118
+ }
2119
+ p.depth--;
2120
+ p.last_child_stack[p.depth] = NULL;
2121
+ }
2122
+ goto close_matched;
2123
+ }
2124
+ extern struct LeptrisDiag* leptris_diag_emit(
2125
+ struct LeptrisDiag**, int*, int*,
2126
+ LeptrisDiagKind, LeptrisNodeRef,
2127
+ const char*, ...);
2128
+ leptris_diag_emit(
2129
+ &p.doc->parse_diags,
2130
+ &p.doc->parse_diag_count,
2131
+ &p.doc->parse_diag_cap,
2132
+ LEPTRIS_DIAG_MISMATCHED_TAG, NULL,
2133
+ "Stray close tag </%.*s> dropped",
2134
+ (int)close_local_len, close_local);
2135
+ continue; /* p.pos is already past the '>' */
2136
+ }
2068
2137
  }
2138
+ close_matched:
2069
2139
  if (p.line_offsets_ok) {
2070
2140
  /* Round 10: fill the open element's journal slot in
2071
2141
  * place — no hash probe, and the recorded start offset
@@ -2271,7 +2341,31 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
2271
2341
  }
2272
2342
  }
2273
2343
 
2274
- if (p.depth != 0) goto fail;
2344
+ if (p.depth != 0) {
2345
+ if (!p.recover) goto fail;
2346
+ /* leptris#1660: elements still open at EOF close
2347
+ * implicitly; the tree commits. */
2348
+ while (p.depth > 0) {
2349
+ LeptrisElement ce = p.open_stack[p.depth - 1];
2350
+ extern struct LeptrisDiag* leptris_diag_emit(
2351
+ struct LeptrisDiag**, int*, int*,
2352
+ LeptrisDiagKind, LeptrisNodeRef,
2353
+ const char*, ...);
2354
+ leptris_diag_emit(
2355
+ &p.doc->parse_diags, &p.doc->parse_diag_count,
2356
+ &p.doc->parse_diag_cap,
2357
+ LEPTRIS_DIAG_UNCLOSED_ELEMENT, NULL,
2358
+ "Element %s still open at end of document",
2359
+ elem_name(ce));
2360
+ if (p.line_offsets_ok) {
2361
+ leptris_elem_pos_close_at(
2362
+ p.doc, p.pos_slot_stack[p.depth - 1],
2363
+ (uint32_t)(p.pos - p.buf));
2364
+ }
2365
+ p.depth--;
2366
+ p.last_child_stack[p.depth] = NULL;
2367
+ }
2368
+ }
2275
2369
  if (!p.root) goto fail;
2276
2370
 
2277
2371
  /* 6. Commit to doc. */
@@ -3288,14 +3382,16 @@ struct leptris_document* direct_parse(const char* xml, size_t len) {
3288
3382
  struct leptris_document* d = dp_il_try(xml, len, 0, 0, 0);
3289
3383
  if (d) return d;
3290
3384
  }
3291
- return direct_parse_internal((char*)xml, len, 0, 2, 0, 0, 0);
3385
+ return direct_parse_internal((char*)xml, len, 0, 2, 0, 0, 0, 0);
3292
3386
  }
3293
3387
 
3294
3388
  /* Public flagged entry (LEPTRIS_PARSE_DROP_WS_TEXT et al.). */
3295
3389
  struct leptris_document* direct_parse_flags(const char* xml, size_t len,
3296
3390
  unsigned parse_flags) {
3297
3391
  if (!xml || len == 0) return NULL;
3298
- {
3392
+ /* The interleaved lane has no recovery support (#1660) — RECOVER
3393
+ * parses take the default lane. */
3394
+ if (!(parse_flags & LEPTRIS_PARSE_RECOVER)) {
3299
3395
  struct leptris_document* d = dp_il_try(
3300
3396
  xml, len,
3301
3397
  (parse_flags & LEPTRIS_PARSE_DROP_WS_TEXT) ? 1 : 0,
@@ -3312,6 +3408,8 @@ struct leptris_document* direct_parse_flags(const char* xml, size_t len,
3312
3408
  (parse_flags & LEPTRIS_PARSE_DTDATTR)
3313
3409
  ? 1 : 0,
3314
3410
  (parse_flags & LEPTRIS_PARSE_KEEP_ENTITY_REFS)
3411
+ ? 1 : 0,
3412
+ (parse_flags & LEPTRIS_PARSE_RECOVER)
3315
3413
  ? 1 : 0);
3316
3414
  }
3317
3415
 
@@ -3319,5 +3417,5 @@ struct leptris_document* direct_parse_flags(const char* xml, size_t len,
3319
3417
  struct leptris_document* direct_parse_inplace(char* buf, size_t len) {
3320
3418
  if (!buf || len == 0) return NULL;
3321
3419
  buf[len] = '\0'; /* Ensure NUL termination */
3322
- return direct_parse_internal(buf, len, 0, 0, 0, 0, 0);
3420
+ return direct_parse_internal(buf, len, 0, 0, 0, 0, 0, 0);
3323
3421
  }
@@ -646,8 +646,15 @@ LEPTRIS_API LeptrisDocument leptris_parse_string_ex(const char* xml,
646
646
  if (options->strict_mode >= 0) g_leptris_strict_mode = options->strict_mode;
647
647
  if (options->max_depth > 0) g_leptris_max_depth = options->max_depth;
648
648
 
649
+ /* #1660: recover maps to the RECOVER parse flag — structural
650
+ * errors keep the best-effort tree with per-recovery parse
651
+ * diagnostics (leptris_document_parse_diag). The empty-document
652
+ * fallback below now only covers still-unrecoverable failures
653
+ * (e.g. encoding), preserving the #547 contract for them. */
654
+ unsigned flags = options->flags;
655
+ if (options->recover) flags |= LEPTRIS_PARSE_RECOVER;
649
656
  LeptrisDocument doc = leptris_parse_string_flags(xml, length,
650
- options->flags, status);
657
+ flags, status);
651
658
  g_leptris_strict_mode = saved_strict;
652
659
  g_leptris_max_depth = saved_depth;
653
660
  if (!doc && options->recover) {
@@ -2351,4 +2351,36 @@ TEST(Plan426, FromPlanResultMintsNativeDocument) {
2351
2351
  leptris_document_free(doc);
2352
2352
  }
2353
2353
 
2354
+ TEST(PlanBuild, ScalarOpRejectsOutOfPoolPlanIndex) {
2355
+ /* leptris#1654: SCALAR ops read pool->plans[plan_index] before
2356
+ * any bounds check — an out-of-pool index OOB-reads (crash or
2357
+ * fabricated output). Must fail with INVALID_ARG exactly like
2358
+ * the ELEMENT branch. */
2359
+ LeptrisStatus st = LEPTRIS_OK;
2360
+ leptris_child_plan kids[1] = {};
2361
+ kids[0].wire_name = "i";
2362
+ kids[0].kind = LEPTRIS_PLAN_KIND_COLLECTION;
2363
+ leptris_element_plan plans[1] = {};
2364
+ plans[0].element_name = "r";
2365
+ plans[0].child_count = 1;
2366
+ plans[0].child_plans = kids;
2367
+ leptris_plan_spec spec = {};
2368
+ spec.abi_version = leptris_plan_abi_version();
2369
+ spec.plan_count = 1;
2370
+ spec.plans = plans;
2371
+ LeptrisPlan plan = leptris_plan_build(&spec, &st);
2372
+ ASSERT_NE(plan, nullptr);
2373
+
2374
+ leptris_plan_row_op ops[] = {
2375
+ {LEPTRIS_PLAN_OP_ELEMENT, 0, UINT32_MAX, NULL, NULL, 0},
2376
+ {LEPTRIS_PLAN_OP_SCALAR, 9, 0, NULL, "x", 1},
2377
+ {LEPTRIS_PLAN_OP_END, 0, 0, NULL, NULL, 0},
2378
+ };
2379
+ LeptrisPlanResult r = leptris_plan_result_build(plan, ops, 3, &st);
2380
+ EXPECT_EQ(r, nullptr);
2381
+ EXPECT_EQ(st, LEPTRIS_ERROR_INVALID_ARG);
2382
+
2383
+ leptris_plan_free(plan);
2384
+ }
2385
+
2354
2386
  } // namespace
@@ -393,7 +393,11 @@ TEST(PublicSurface, RootlessWithPIsIsNotDropped) {
393
393
  /* Issue #547: ParseOptions.recover returns an empty document
394
394
  * instead of NULL on parse failure (moxml's libxml2 adapter does
395
395
  * the same; this makes the native API honest about it). */
396
- TEST(PublicSurface, RecoverReturnsEmptyDocument) {
396
+ TEST(PublicSurface, RecoverKeepsBestEffortTree) {
397
+ /* leptris#1660 (supersedes the #547 empty-document contract):
398
+ * recover keeps the best-effort tree for structural errors —
399
+ * unclosed elements close at EOF, each recovery records a
400
+ * diagnostic. */
397
401
  LeptrisParseOptions opts = {LEPTRIS_PARSE_DEFAULT, 0, 0, 0};
398
402
  opts.recover = 1;
399
403
  LeptrisStatus st = LEPTRIS_OK;
@@ -403,10 +407,11 @@ TEST(PublicSurface, RecoverReturnsEmptyDocument) {
403
407
  memset(bad, 0, sizeof(bad));
404
408
  memcpy(bad, "<root><unclosed>", 16);
405
409
  LeptrisDocument d = leptris_parse_string_ex(bad, 16, &opts, &st);
406
- ASSERT_NE(d, nullptr); /* recover: empty doc, not NULL */
407
- EXPECT_EQ(st, LEPTRIS_ERROR_PARSE);
408
- EXPECT_EQ(leptris_xpath_result_count(
409
- leptris_xpath_eval(d, nullptr, "count(//node())")), 0);
410
+ ASSERT_NE(d, nullptr);
411
+ LeptrisElement root = leptris_document_root(d);
412
+ ASSERT_NE(root, nullptr);
413
+ EXPECT_STREQ(leptris_element_name(root), "root");
414
+ EXPECT_GE(leptris_document_parse_diag_count(d), 1u);
410
415
  leptris_document_free(d);
411
416
  }
412
417
 
@@ -776,3 +776,77 @@ TEST(ParseDoctype, SecondDoctypeKeepsFirstModel) {
776
776
  EXPECT_STREQ(text, "first");
777
777
  leptris_document_free(d);
778
778
  }
779
+
780
+ /* leptris#1660: XML_PARSE_RECOVER parity — structural errors keep
781
+ * the best-effort tree instead of an empty document. Mismatched
782
+ * close: an ancestor match closes the intermediates implicitly; a
783
+ * true stray close is dropped. Unclosed at EOF: everything open
784
+ * closes. Each recovery records a diagnostic; without the flag the
785
+ * parse still fails. */
786
+ TEST(ParseRecover, MismatchedCloseKeepsTreeAndDropsStray) {
787
+ LeptrisStatus st = LEPTRIS_OK;
788
+ const char xml[] = "<root><a>x</b></root>";
789
+ LeptrisDocument doc = leptris_parse_string_flags(
790
+ xml, strlen(xml), LEPTRIS_PARSE_RECOVER, &st);
791
+ ASSERT_NE(doc, nullptr) << "status " << (int)st;
792
+ LeptrisElement root = leptris_document_root(doc);
793
+ ASSERT_NE(root, nullptr);
794
+ EXPECT_STREQ(leptris_element_name(root), "root");
795
+ LeptrisElement a = leptris_element_first_child_any(root);
796
+ ASSERT_NE(a, nullptr);
797
+ EXPECT_STREQ(leptris_element_name(a), "a");
798
+ ASSERT_NE(leptris_element_text(a), nullptr);
799
+ EXPECT_STREQ(leptris_element_text(a), "x");
800
+ /* Two recoveries: the stray </b> drops, and <a> closes
801
+ * implicitly when </root> names the ancestor (libxml2 reports
802
+ * both as well). */
803
+ EXPECT_EQ(leptris_document_parse_diag_count(doc), 2u);
804
+ leptris_document_free(doc);
805
+ }
806
+
807
+ TEST(ParseRecover, MismatchedCloseForAncestorClosesIntermediates) {
808
+ LeptrisStatus st = LEPTRIS_OK;
809
+ const char xml[] = "<root><a>x</root>";
810
+ LeptrisDocument doc = leptris_parse_string_flags(
811
+ xml, strlen(xml), LEPTRIS_PARSE_RECOVER, &st);
812
+ ASSERT_NE(doc, nullptr) << "status " << (int)st;
813
+ LeptrisElement root = leptris_document_root(doc);
814
+ ASSERT_NE(root, nullptr);
815
+ EXPECT_STREQ(leptris_element_name(root), "root");
816
+ LeptrisElement a = leptris_element_first_child_any(root);
817
+ ASSERT_NE(a, nullptr);
818
+ EXPECT_STREQ(leptris_element_name(a), "a");
819
+ ASSERT_NE(leptris_element_text(a), nullptr);
820
+ EXPECT_STREQ(leptris_element_text(a), "x");
821
+ leptris_document_free(doc);
822
+ }
823
+
824
+ TEST(ParseRecover, UnclosedAtEofClosesEverything) {
825
+ LeptrisStatus st = LEPTRIS_OK;
826
+ const char xml[] = "<root><a>x";
827
+ LeptrisDocument doc = leptris_parse_string_flags(
828
+ xml, strlen(xml), LEPTRIS_PARSE_RECOVER, &st);
829
+ ASSERT_NE(doc, nullptr) << "status " << (int)st;
830
+ LeptrisElement root = leptris_document_root(doc);
831
+ ASSERT_NE(root, nullptr);
832
+ EXPECT_STREQ(leptris_element_name(root), "root");
833
+ LeptrisElement a = leptris_element_first_child_any(root);
834
+ ASSERT_NE(a, nullptr);
835
+ EXPECT_STREQ(leptris_element_name(a), "a");
836
+ /* Both open elements (root and a) closed implicitly. */
837
+ EXPECT_EQ(leptris_document_parse_diag_count(doc), 2u);
838
+ leptris_document_free(doc);
839
+ }
840
+
841
+ TEST(ParseRecover, WithoutFlagStillFails) {
842
+ LeptrisStatus st = LEPTRIS_OK;
843
+ const char xml[] = "<root><a>x</b></root>";
844
+ LeptrisDocument doc = leptris_parse_string_flags(
845
+ xml, strlen(xml), LEPTRIS_PARSE_DEFAULT, &st);
846
+ EXPECT_EQ(doc, nullptr);
847
+ EXPECT_EQ(st, LEPTRIS_ERROR_PARSE);
848
+ const char unclosed[] = "<root><a>x";
849
+ doc = leptris_parse_string_flags(unclosed, strlen(unclosed),
850
+ LEPTRIS_PARSE_DEFAULT, &st);
851
+ EXPECT_EQ(doc, nullptr);
852
+ }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json",
3
3
  "name": "leptris",
4
- "version": "1.9.342",
4
+ "version": "1.9.344",
5
5
  "description": "High-performance XML parser and XPath 1.0 engine in C (libleptris)",
6
6
  "homepage": "https://github.com/leptris/leptris",
7
7
  "license": "MIT",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leptris
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.342.0
4
+ version: 1.9.344.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.