leptris 1.9.346.0-ppc64le-linux → 1.9.349.0-ppc64le-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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b358fe96893b880528d42ba0a708615616aa446733e0f31c205f4ee4d46043fb
4
- data.tar.gz: f9b9115699471e36b0a9f9553d31a99c73ee1cc70a41d1629a6f4d27e4e524d5
3
+ metadata.gz: 4a5a0ec35167991f89cddabc3962c02db7a4b2d0d14f43dbc05005eb4c2dc444
4
+ data.tar.gz: 2c319c7725758e36a6ddd96a9998c0217018af4d3f499426762234c51cbcf3e9
5
5
  SHA512:
6
- metadata.gz: 887eee320fca9a248cc81d484cec01bfc79f3644385d5ed9b3306457a864f7c4d7fa312564600d2a6f009357443524f965c9bb2a136b2a3f9329f560f669f334
7
- data.tar.gz: 67495881774b7320b97f719b302b84db7882fb41c5b4154a0404090509a400666bb6ee903efdb22e168cafb382ecaad7291a7781c6b6b693203fcf8e044985a7
6
+ metadata.gz: 5905b71cf7c8c5417381d08c7e3b2ab01e289c4ac94d8d4e395a139f1e4ac5c0322bc9659c31e14cf03442550c05056e6cb803ce91e6c4bb1c39556ae27291fc
7
+ data.tar.gz: ba028358a0661bdbfb0adbfc50e5d26ec347ccf3197480e2a93bb3707e5260487a97721021a87bd0288953e77a98a33752d9fffe31a7d939b0b43797ac272b77
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.346"
11
+ LIBLEPTRIS_VERSION = "1.9.349"
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.346.0"
4
+ VERSION = "1.9.349.0"
5
5
  end
@@ -494,6 +494,17 @@ module Leptris
494
494
  [:leptris_plan_result, :size_t], :leptris_plan_result
495
495
  attach_function :leptris_plan_value_attribute,
496
496
  [:leptris_plan_result, :string], UTF8_STRING
497
+ # #1670 (libleptris 1.9.347): the ELEMENT value's attribute
498
+ # census + one-call flat read (ParallelArrays mirrors
499
+ # children_snapshot: NULL blobs size, retry at the returned
500
+ # capacity; (size_t)-1 = still too small). Engine-face
501
+ # coverage — the plan-value consumer port rides the #425/#426
502
+ # Descriptor round.
503
+ attach_function :leptris_plan_value_attribute_count,
504
+ [:leptris_plan_result], :size_t
505
+ attach_function :leptris_plan_value_attributes_snapshot,
506
+ [:leptris_plan_result, :pointer, :size_t, :pointer, :pointer,
507
+ :size_t, :pointer], :size_t
497
508
  # One-call flat child list: names blob with per-child offsets
498
509
  # (SIZE_MAX = NULL name, i.e. content runs), type tags, and
499
510
  # borrowed child handles. Returns 0 on success or the blob
@@ -18,16 +18,29 @@ class Leptris::XML::PlanValue
18
18
  Leptris::XML::FFI::PLAN_VALUE_SCALAR => :scalar,
19
19
  Leptris::XML::FFI::PLAN_VALUE_COLLECTION => :collection,
20
20
  Leptris::XML::FFI::PLAN_VALUE_RAW => :raw,
21
- Leptris::XML::FFI::PLAN_VALUE_CALLBACK => :callback,
21
+ Leptris::XML::FFI::PLAN_VALUE_CALLBACK => :callback
22
22
  }.freeze
23
23
  private_constant :VALUE_KINDS
24
24
 
25
- def initialize(ptr, owner: nil, plans: nil, plan: nil, counter: nil)
25
+ def initialize(ptr, owner: nil, plans: nil, plan: nil, counter: nil,
26
+ root: nil)
26
27
  @ptr = ptr
27
28
  @owner = owner
28
29
  @plans = plans
29
30
  @plan = plan
30
31
  @counter = counter || [0]
32
+ # #1673: child views borrow their pointer from the root's
33
+ # result arena — retaining the root here (transitively) keeps
34
+ # the whole-result handle alive for as long as any borrowed
35
+ # view exists. Without it a Ruby-3.3-timed GC can free the
36
+ # root mid-read (the ride's linux/3.3 segfault).
37
+ @root = root
38
+ end
39
+
40
+ # The whole-result owner this view borrows from (self for a
41
+ # root). Retained by every borrowed child via #at.
42
+ def lifetime_root
43
+ @root || self
31
44
  end
32
45
 
33
46
  # The engine value handle — the ROOT value's is the whole-result
@@ -75,18 +88,21 @@ class Leptris::XML::PlanValue
75
88
  def int_value
76
89
  out = ::FFI::MemoryPointer.new(:int64)
77
90
  return nil unless Leptris::XML::FFI.leptris_plan_value_int(@ptr, out).zero?
91
+
78
92
  out.read_int64
79
93
  end
80
94
 
81
95
  def float_value
82
96
  out = ::FFI::MemoryPointer.new(:double)
83
97
  return nil unless Leptris::XML::FFI.leptris_plan_value_float(@ptr, out).zero?
98
+
84
99
  out.read_double
85
100
  end
86
101
 
87
102
  def bool_value
88
103
  out = ::FFI::MemoryPointer.new(:int)
89
104
  return nil unless Leptris::XML::FFI.leptris_plan_value_bool(@ptr, out).zero?
105
+
90
106
  out.read_int != 0
91
107
  end
92
108
 
@@ -114,9 +130,11 @@ class Leptris::XML::PlanValue
114
130
  @counter[0] += 1
115
131
  ptr = Leptris::XML::FFI.leptris_plan_value_at(@ptr, i)
116
132
  return nil if ptr.null?
133
+
117
134
  child_plan = child_plan_for_position(ptr)
118
135
  Leptris::XML::PlanValue.new(ptr, plans: @plans, plan: child_plan,
119
- counter: @counter)
136
+ counter: @counter,
137
+ root: lifetime_root)
120
138
  end
121
139
 
122
140
  # ELEMENT: the whole child list in one FFI crossing — parallel
@@ -136,7 +154,8 @@ class Leptris::XML::PlanValue
136
154
  tags = ::FFI::MemoryPointer.new(:uint8, n)
137
155
  handles = ::FFI::MemoryPointer.new(:pointer, n)
138
156
  needed = Leptris::XML::FFI.leptris_plan_value_children_snapshot(
139
- @ptr, blob, cap, offsets, tags, handles)
157
+ @ptr, blob, cap, offsets, tags, handles
158
+ )
140
159
  break snapshot_build(blob, offsets, tags, handles, n) if needed.zero?
141
160
 
142
161
  cap = needed
@@ -187,7 +206,7 @@ class Leptris::XML::PlanValue
187
206
  name: name,
188
207
  type_tag: type_tag,
189
208
  attributes: kwarg_attributes,
190
- children: kwarg_children,
209
+ children: kwarg_children
191
210
  }
192
211
  when :collection
193
212
  kwarg_collection_children
@@ -212,7 +231,7 @@ class Leptris::XML::PlanValue
212
231
  name: name,
213
232
  type_tag: type_tag,
214
233
  attributes: collected_attributes,
215
- children: Array.new(count) { |i| at(i).to_ruby },
234
+ children: Array.new(count) { |i| at(i).to_ruby }
216
235
  }
217
236
  when :collection
218
237
  Array.new(count) { |i| at(i).to_ruby }
@@ -225,7 +244,6 @@ class Leptris::XML::PlanValue
225
244
 
226
245
  private
227
246
 
228
-
229
247
  def kwarg_attributes
230
248
  ((@plan && @plan[:attributes]) || []).each_with_object({}) do |row, result|
231
249
  v = attribute(row[:name])
@@ -234,18 +252,19 @@ class Leptris::XML::PlanValue
234
252
  end
235
253
 
236
254
  def kwarg_children
237
- rows = ((@plan && @plan[:children]) || [])
255
+ rows = (@plan && @plan[:children]) || []
238
256
  result = {}
239
257
  rows.each_with_index do |row, i|
240
258
  value = at(i)
241
259
  next if value.nil?
260
+
242
261
  key = row[:name]
243
262
  coerced = coerce_kwarg(value, row[:kind])
244
263
  result[key] = if result.key?(key)
245
- Array(result[key]) << coerced
246
- else
247
- coerced
248
- end
264
+ Array(result[key]) << coerced
265
+ else
266
+ coerced
267
+ end
249
268
  end
250
269
  result
251
270
  end
@@ -273,14 +292,23 @@ class Leptris::XML::PlanValue
273
292
 
274
293
  def typed_string_value_for(value)
275
294
  case value.type_tag
276
- when 1 then value.int_value || (Integer(value.string_value, 10) rescue value.string_value)
277
- when 2 then value.float_value || (Float(value.string_value) rescue value.string_value)
295
+ when 1 then value.int_value || begin
296
+ Integer(value.string_value, 10)
297
+ rescue StandardError
298
+ value.string_value
299
+ end
300
+ when 2 then value.float_value || begin
301
+ Float(value.string_value)
302
+ rescue StandardError
303
+ value.string_value
304
+ end
278
305
  when 3
279
306
  v = value.bool_value
280
307
  v.nil? ? typed_bool_fallback(value.string_value) : v
281
308
  else value.string_value
282
309
  end
283
310
  end
311
+
284
312
  # contract): 1=Integer, 2=Float, 3=boolean. Executed in-pass by
285
313
  # the engine (#1269a); on its soft-fail (non-numeric source) the
286
314
  # lenient Ruby path applies, then the raw String wins.
@@ -288,9 +316,17 @@ class Leptris::XML::PlanValue
288
316
  def typed_string_value
289
317
  case type_tag
290
318
  when 1
291
- int_value || (string_value && (Integer(string_value, 10) rescue string_value))
319
+ int_value || (string_value && begin
320
+ Integer(string_value, 10)
321
+ rescue StandardError
322
+ string_value
323
+ end)
292
324
  when 2
293
- float_value || (string_value && (Float(string_value) rescue string_value))
325
+ float_value || (string_value && begin
326
+ Float(string_value)
327
+ rescue StandardError
328
+ string_value
329
+ end)
294
330
  when 3
295
331
  v = bool_value
296
332
  v.nil? ? typed_bool_fallback(string_value) : v
@@ -331,14 +367,16 @@ class Leptris::XML::PlanValue
331
367
  ptr = handles.get_pointer(i * POINTER_SIZE)
332
368
  child_plan = child_plan_for_position(ptr, name: names[i])
333
369
  children[i] = Leptris::XML::PlanValue.new(ptr, plans: @plans,
334
- plan: child_plan,
335
- counter: @counter)
370
+ plan: child_plan,
371
+ counter: @counter,
372
+ root: lifetime_root)
336
373
  end
337
374
  [names, tag_values, children]
338
375
  end
339
376
 
340
377
  def child_plan_for_position(ptr, name: nil)
341
378
  return nil unless @plan
379
+
342
380
  rows = @plan[:children] || []
343
381
  row = rows.size == 1 ? rows.first : nil
344
382
  unless row
@@ -350,6 +388,7 @@ class Leptris::XML::PlanValue
350
388
  row = rows.find { |r| r[:name] == child_name }
351
389
  end
352
390
  return nil unless row && @plans
391
+
353
392
  row[:kind] == :nested ? @plans[row[:child_plan_index]] : nil
354
393
  end
355
394
 
@@ -358,6 +397,7 @@ class Leptris::XML::PlanValue
358
397
  r[:kind] == :nested && r[:name] == child_wire_name
359
398
  end
360
399
  return nil unless row && @plans
400
+
361
401
  @plans[row[:child_plan_index]]
362
402
  end
363
403
 
@@ -376,7 +416,11 @@ class Leptris::XML::PlanValue
376
416
  def cast_attribute(value, row)
377
417
  tag = Leptris::XML::Descriptor.type_tag_code(row)
378
418
  case tag
379
- when 1 then Integer(value, 10) rescue value
419
+ when 1 then begin
420
+ Integer(value, 10)
421
+ rescue StandardError
422
+ value
423
+ end
380
424
  when 2
381
425
  begin
382
426
  Float(value)
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.346
4
+ - libleptris v1.9.349
5
5
  - utf8proc v2.11.0
6
6
 
7
7
  Rebuild by hand:
@@ -1,5 +1,70 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.9.349] - 2026-10-13
4
+
5
+ ### Fixed
6
+
7
+ - **Plan arena: the window is owned by a wrapper
8
+ (leptris#1677, PR #1678).** The "multiple roots" early
9
+ return in leptris_plan_result_build neither restored the
10
+ thread-local current-arena window nor freed the arena -
11
+ the only return path that missed both. After it,
12
+ dp_cur_arena stayed pointing at the leaked arena for the
13
+ rest of the thread: every later malloc-lane mint (serialize
14
+ joins, dp_decls tables) rode the dead arena, and freeing
15
+ those interior pointers corrupted the heap - the ~50%
16
+ intermittent garbage/segfault the leptris-py suite hit
17
+ (in-process sequencing; ASAN-per-test could not see it).
18
+ The window and arena are now owned by a WRAPPER - restored
19
+ and freed unconditionally on every exit; the windowed body
20
+ touches neither. In-process regression spec: 50 rounds of
21
+ failing two-root build then walk + serialize, aborts at
22
+ round 0 pre-fix. Full suite 1994/1994.
23
+
24
+
25
+ ## [1.9.348] - 2026-10-13
26
+
27
+ ### Fixed
28
+
29
+ - **Plan walk: zero the arena-routed attribute arrays
30
+ (leptris#1673, PR #1675).** The #1670 walk arena returns
31
+ non-zeroed memory, but the walk's fixed-capacity attribute
32
+ arrays relied on calloc's NULL-fill: unbound attribute rows
33
+ left their slots unwritten, and the serialize lane's
34
+ declaration walk then read malloc-fill garbage through
35
+ attr_ns_prefixes - the segfault the 1.9.347 ride's
36
+ nested-capture specs hit (segv at 0x0 on Ruby 3.3/Linux/PGO
37
+ legs; allocation layout decided what landed in the
38
+ unwritten slots). The four arrays memset to zero at
39
+ allocation, restoring the calloc contract; dp_arena_alloc
40
+ stays raw by design. ASAN repro faulted pre-fix and passes
41
+ clean post-fix. Red-first spec pins both serialize faces on
42
+ the binding's nested-capture shape. Full suite 1992/1992.
43
+
44
+
45
+ ## [1.9.347] - 2026-10-13
46
+
47
+ ### Changed
48
+
49
+ - **Plan walk: arena-backed results + batch attribute face
50
+ (leptris#1670, PR #1671).** The ordered plan-lane walk
51
+ minted ~33K more allocations per parse than the
52
+ interpretive lane (the walk-side sibling of #408). The walk
53
+ and build lanes now mint every value struct, name/value
54
+ string, kids array, and attribute array from a root-owned
55
+ chunked bump arena (threaded via a TLS current-arena slot;
56
+ plan capture and serialization stay on malloc), and
57
+ leptris_plan_result_free releases the chunks wholesale.
58
+ Measured: 0 allocations inside the walk window on a
59
+ 5000-row walk + 10K-attribute read pass (malloc_logger).
60
+ New ABI: leptris_plan_value_attribute_count sizes the
61
+ buffers and leptris_plan_value_attributes_snapshot returns
62
+ ALL attribute names + values in ONE crossing (two-phase
63
+ capacity/retry contract) - a node with N attributes costs
64
+ O(1) crossings instead of O(N) mints. Red-first spec;
65
+ full suite 1990/1991-total green; descriptor ASAN-clean.
66
+
67
+
3
68
  ## [1.9.346] - 2026-10-12
4
69
 
5
70
  ### Fixed
@@ -3,7 +3,7 @@
3
3
  cmake_minimum_required(VERSION 3.20)
4
4
 
5
5
  project(leptris
6
- VERSION 1.9.346
6
+ VERSION 1.9.349
7
7
  DESCRIPTION "Fast XML parser and XPath evaluator in pure C"
8
8
  LANGUAGES C CXX
9
9
  )
@@ -257,6 +257,10 @@ LEPTRIS_API LeptrisPlanResult leptris_plan_walk(LeptrisDocument doc,
257
257
  LeptrisElement ctx,
258
258
  LeptrisPlan plan,
259
259
  LeptrisStatus* status);
260
+ /* Release a walk result. Pass the ROOT handle the walk returned —
261
+ * #1670 backs the whole tree with one root-owned bump arena, so
262
+ * the wholesale release is O(chunks); subtree handles share the
263
+ * arena and are dropped with it. */
260
264
  LEPTRIS_API void leptris_plan_result_free(LeptrisPlanResult result);
261
265
 
262
266
  /* #408 ask 2: build a standalone result natively from a flat op
@@ -376,6 +380,28 @@ LEPTRIS_API size_t leptris_plan_value_children_snapshot(
376
380
  LEPTRIS_API const char* leptris_plan_value_attribute(const LeptrisPlanResult v,
377
381
  const char* wire_name);
378
382
 
383
+ /* #1670: number of attributes carried on an ELEMENT value (0 for
384
+ * non-ELEMENT values and NULL). Sizes the attributes_snapshot
385
+ * buffers. */
386
+ LEPTRIS_API size_t leptris_plan_value_attribute_count(
387
+ const LeptrisPlanResult v);
388
+
389
+ /* #1670: ALL attribute names + values in ONE crossing (the
390
+ * per-name `leptris_plan_value_attribute` walk mints one
391
+ * FFI/wrapper round trip per attribute per node). ParallelArrays
392
+ * contract mirrors children_snapshot: call once with NULL blobs /
393
+ * caps 0 to size (returns the required per-blob byte capacity;
394
+ * both blobs need the same capacity), then call again with
395
+ * buffers of at least that capacity — 0 on success,
396
+ * (size_t)-1 when either buffer is still too small. Offsets are
397
+ * byte offsets into the respective blobs; entries are
398
+ * NUL-terminated. Attribute ORDER is the plan's attribute-row
399
+ * order (only bound attributes appear). NULL v returns 0. */
400
+ LEPTRIS_API size_t leptris_plan_value_attributes_snapshot(
401
+ const LeptrisPlanResult v,
402
+ char* names_blob, size_t names_cap, size_t* name_offsets,
403
+ char* values_blob, size_t values_cap, size_t* value_offsets);
404
+
379
405
  /* #1269a in-pass type execution (#1269a): for SCALAR/RAW values
380
406
  * whose producing row carried type_tag in {1=int, 2=float, 3=bool},
381
407
  * the walk parses the string once and stores typed values. The str
@@ -62,25 +62,104 @@ struct leptris_plan_result {
62
62
  * NULL; arrays NULL when the element has no attribute plan). */
63
63
  char** attr_ns_prefixes;
64
64
  char** attr_ns_uris;
65
+ /* #1670: the root-owned bump arena backing this subtree's
66
+ * memory. owner=1 marks the walk root — the only handle whose
67
+ * release frees the chunks (subtree handles share the arena). */
68
+ struct dp_arena* arena;
69
+ unsigned arena_owner : 1;
65
70
  };
66
71
 
72
+ /* ---- #1670: walk-result bump arena ------------------------------
73
+ * The walk + build lanes mint EVERYTHING a result tree owns (value
74
+ * structs, name/value strings, kids arrays, attribute arrays) from
75
+ * a chunked bump arena owned by the ROOT value; the wholesale
76
+ * leptris_plan_result_free releases the chunks. 33K calloc/strdup/
77
+ * realloc pairs per parse collapse to O(chunks). The arena is
78
+ * carried in a thread-local "current" slot set for the duration of
79
+ * leptris_plan_walk / leptris_plan_result_build, so the ~48 mint
80
+ * sites route through the same dp_strdup_n / calloc call shapes
81
+ * without per-site threading. Non-result lanes (plan capture,
82
+ * serialization) run outside the window and keep malloc. */
83
+ struct dp_arena {
84
+ struct dp_arena* next;
85
+ size_t used, cap;
86
+ /* char data[] follows the header */
87
+ };
88
+ #define DP_ARENA_CHUNK (16u * 1024u)
89
+
90
+ static struct dp_arena* dp_arena_new(size_t cap) {
91
+ struct dp_arena* a = (struct dp_arena*)malloc(sizeof(*a) + cap);
92
+ if (!a) return NULL;
93
+ a->next = NULL;
94
+ a->used = 0;
95
+ a->cap = cap;
96
+ return a;
97
+ }
98
+
99
+ static void dp_arena_free_all(struct dp_arena* head) {
100
+ while (head) {
101
+ struct dp_arena* n = head->next;
102
+ free(head);
103
+ head = n;
104
+ }
105
+ }
106
+
107
+ static void* dp_arena_alloc(struct dp_arena** head, size_t n) {
108
+ n = (n + 15u) & ~(size_t)15u; /* 16-aligned */
109
+ if (!n) n = 16;
110
+ for (struct dp_arena* a = *head; a; a = a->next)
111
+ if (a->cap - a->used >= n) {
112
+ void* p = (char*)(a + 1) + a->used;
113
+ a->used += n;
114
+ return p;
115
+ }
116
+ size_t cap = DP_ARENA_CHUNK;
117
+ if (n > cap) cap = n;
118
+ struct dp_arena* a = dp_arena_new(cap);
119
+ if (!a) return NULL;
120
+ a->next = *head;
121
+ *head = a;
122
+ void* p = (char*)(a + 1) + a->used;
123
+ a->used += n;
124
+ return p;
125
+ }
126
+
127
+ static LEPTRIS_THREAD_LOCAL struct dp_arena* dp_cur_arena;
128
+
67
129
  /* ---- string + node helpers ------------------------------------- */
68
130
 
69
131
  static char* dp_strdup_n(const char* s, size_t n) {
70
- char* p = (char*)malloc(n + 1);
71
- if (!p) return NULL;
132
+ char* p;
133
+ if (dp_cur_arena) {
134
+ p = (char*)dp_arena_alloc(&dp_cur_arena, n + 1);
135
+ if (!p) return NULL;
136
+ } else {
137
+ p = (char*)malloc(n + 1);
138
+ if (!p) return NULL;
139
+ }
72
140
  memcpy(p, s, n);
73
141
  p[n] = '\0';
74
142
  return p;
75
143
  }
76
144
 
145
+
77
146
  static char* dp_strdup(const char* s) {
78
147
  return s ? dp_strdup_n(s, strlen(s)) : NULL;
79
148
  }
80
149
 
150
+
81
151
  static struct leptris_plan_result* dp_value_new(LeptrisPlanValueKind k) {
82
- struct leptris_plan_result* v =
83
- (struct leptris_plan_result*)calloc(1, sizeof(*v));
152
+ struct leptris_plan_result* v;
153
+ if (dp_cur_arena) {
154
+ v = (struct leptris_plan_result*)
155
+ dp_arena_alloc(&dp_cur_arena, sizeof(*v));
156
+ if (!v) return NULL;
157
+ memset(v, 0, sizeof(*v));
158
+ v->arena = dp_cur_arena;
159
+ } else {
160
+ v = (struct leptris_plan_result*)calloc(1, sizeof(*v));
161
+ if (!v) return NULL;
162
+ }
84
163
  return v ? (v->kind = k, v) : NULL;
85
164
  }
86
165
 
@@ -88,9 +167,20 @@ static int dp_value_push(struct leptris_plan_result* parent,
88
167
  struct leptris_plan_result* kid) {
89
168
  if (parent->kid_count == parent->kid_cap) {
90
169
  size_t cap = parent->kid_cap ? parent->kid_cap * 2 : 4;
91
- struct leptris_plan_result** grown = (struct leptris_plan_result**)
92
- realloc(parent->kids, cap * sizeof(*grown));
93
- if (!grown) return 0;
170
+ struct leptris_plan_result** grown;
171
+ if (parent->arena) {
172
+ /* #1670: arena-backed growth — the old block stays in
173
+ * its chunk until the wholesale release. */
174
+ grown = (struct leptris_plan_result**)
175
+ dp_arena_alloc(&parent->arena, cap * sizeof(*grown));
176
+ if (!grown) return 0;
177
+ memcpy(grown, parent->kids,
178
+ parent->kid_count * sizeof(*grown));
179
+ } else {
180
+ grown = (struct leptris_plan_result**)
181
+ realloc(parent->kids, cap * sizeof(*grown));
182
+ if (!grown) return 0;
183
+ }
94
184
  parent->kids = grown;
95
185
  parent->kid_cap = cap;
96
186
  }
@@ -107,6 +197,13 @@ static int dp_value_set_str(struct leptris_plan_result* v,
107
197
 
108
198
  static void dp_result_free_rec(struct leptris_plan_result* v) {
109
199
  if (!v) return;
200
+ /* #1670: arena-backed subtrees release wholesale — only the
201
+ * walk ROOT frees the chunks; subtree handles share the arena
202
+ * and are dropped with it. */
203
+ if (v->arena) {
204
+ if (v->arena_owner) dp_arena_free_all(v->arena);
205
+ return;
206
+ }
110
207
  for (size_t i = 0; i < v->kid_count; i++) dp_result_free_rec(v->kids[i]);
111
208
  free(v->kids);
112
209
  for (size_t i = 0; i < v->attr_count; i++) {
@@ -583,8 +680,16 @@ static int dp_ns_binds(LeptrisElement elem, uint8_t ns_form,
583
680
  /* #1551: "prefix:local" join for emission spellings. */
584
681
  static char* dp_join_colon(const char* pfx, const char* local) {
585
682
  size_t pl = strlen(pfx), ll = strlen(local);
586
- char* j = (char*)malloc(pl + 1 + ll + 1);
587
- if (!j) return NULL;
683
+ char* j;
684
+ if (dp_cur_arena) {
685
+ /* #1670: walk-window joins ride the arena (LSan on Linux CI
686
+ * catches the malloc escape). */
687
+ j = (char*)dp_arena_alloc(&dp_cur_arena, pl + 1 + ll + 1);
688
+ if (!j) return NULL;
689
+ } else {
690
+ j = (char*)malloc(pl + 1 + ll + 1);
691
+ if (!j) return NULL;
692
+ }
588
693
  memcpy(j, pfx, pl);
589
694
  j[pl] = ':';
590
695
  memcpy(j + pl + 1, local, ll + 1);
@@ -668,14 +773,37 @@ static struct leptris_plan_result* dp_walk_element(LeptrisElement elem,
668
773
 
669
774
  /* Attribute channel */
670
775
  if (plan->attribute_count) {
671
- v->attr_names = (char**)calloc(plan->attribute_count,
672
- sizeof(char*));
673
- v->attr_values = (char**)calloc(plan->attribute_count,
674
- sizeof(char*));
675
- v->attr_ns_prefixes = (char**)calloc(plan->attribute_count,
676
- sizeof(char*));
677
- v->attr_ns_uris = (char**)calloc(plan->attribute_count,
678
- sizeof(char*));
776
+ /* #1670: fixed-capacity — arena-backed when the walk runs
777
+ * in its arena window. Arena memory is NOT zeroed: unbound
778
+ * attribute rows leave their slots unwritten and the
779
+ * serializers/lookup expect NULL there (#1673) — memset
780
+ * preserves the calloc contract. */
781
+ v->attr_names = (char**)(dp_cur_arena
782
+ ? dp_arena_alloc(&dp_cur_arena,
783
+ plan->attribute_count * sizeof(char*))
784
+ : calloc(plan->attribute_count, sizeof(char*)));
785
+ v->attr_values = (char**)(dp_cur_arena
786
+ ? dp_arena_alloc(&dp_cur_arena,
787
+ plan->attribute_count * sizeof(char*))
788
+ : calloc(plan->attribute_count, sizeof(char*)));
789
+ v->attr_ns_prefixes = (char**)(dp_cur_arena
790
+ ? dp_arena_alloc(&dp_cur_arena,
791
+ plan->attribute_count * sizeof(char*))
792
+ : calloc(plan->attribute_count, sizeof(char*)));
793
+ v->attr_ns_uris = (char**)(dp_cur_arena
794
+ ? dp_arena_alloc(&dp_cur_arena,
795
+ plan->attribute_count * sizeof(char*))
796
+ : calloc(plan->attribute_count, sizeof(char*)));
797
+ if (dp_cur_arena) {
798
+ memset(v->attr_names, 0,
799
+ plan->attribute_count * sizeof(char*));
800
+ memset(v->attr_values, 0,
801
+ plan->attribute_count * sizeof(char*));
802
+ memset(v->attr_ns_prefixes, 0,
803
+ plan->attribute_count * sizeof(char*));
804
+ memset(v->attr_ns_uris, 0,
805
+ plan->attribute_count * sizeof(char*));
806
+ }
679
807
  if (!v->attr_names || !v->attr_values || !v->attr_ns_prefixes ||
680
808
  !v->attr_ns_uris) {
681
809
  dp_result_free_rec(v);
@@ -765,7 +893,10 @@ static int dp_walk_children(LeptrisElement elem, const dp_plan* plan,
765
893
  unsigned char* claimed = NULL;
766
894
  /* #1552: bound[] marks every element a named row actually
767
895
  * bound (claimed[] is the #1272 predicate-partition subset).
768
- * Wildcard rows consult it after all named rows ran. */
896
+ * Wildcard rows consult it after all named rows ran.
897
+ * #1670 note: this per-element scratch stays malloc/free (its
898
+ * frees interleave with tree hand-offs) — only the RESULT TREE
899
+ * rides the walk arena. */
769
900
  unsigned char* bound = NULL;
770
901
  if (element_count > 0 && (plan->child_count > 0)) {
771
902
  claimed = (unsigned char*)calloc(
@@ -891,9 +1022,13 @@ static int dp_walk_children(LeptrisElement elem, const dp_plan* plan,
891
1022
  if (row->ns_prefix) {
892
1023
  /* The inner walk's own #1551 stamp may already
893
1024
  * hold this pair — free before overwriting or
894
- * the first strdup orphans. */
895
- free(v->ns_prefix);
896
- free(v->ns_uri);
1025
+ * the first strdup orphans. Arena-backed values
1026
+ * skip the free: the old strings ride their
1027
+ * chunks until the wholesale release (#1670). */
1028
+ if (!v->arena) {
1029
+ free(v->ns_prefix);
1030
+ free(v->ns_uri);
1031
+ }
897
1032
  v->ns_prefix = dp_strdup(row->ns_prefix);
898
1033
  v->ns_uri = dp_strdup(row->ns_uri ? row->ns_uri
899
1034
  : target->ns_uri);
@@ -1207,12 +1342,23 @@ LEPTRIS_API LeptrisPlanResult leptris_plan_walk(LeptrisDocument doc, LeptrisElem
1207
1342
  if (status) *status = LEPTRIS_ERROR_NULL_ARG;
1208
1343
  return NULL;
1209
1344
  }
1345
+ struct dp_arena* arena = dp_arena_new(DP_ARENA_CHUNK);
1346
+ if (!arena) {
1347
+ if (status) *status = LEPTRIS_ERROR_MEMORY;
1348
+ return NULL;
1349
+ }
1350
+ struct dp_arena* saved = dp_cur_arena;
1351
+ dp_cur_arena = arena;
1210
1352
  struct leptris_plan_result* r =
1211
1353
  dp_walk_element(ctx, &plan->plans[0], plan);
1354
+ dp_cur_arena = saved;
1212
1355
  if (!r) {
1356
+ dp_arena_free_all(arena);
1213
1357
  if (status) *status = LEPTRIS_ERROR_MEMORY;
1214
1358
  return NULL;
1215
1359
  }
1360
+ /* The walk root owns the chunks; subtree handles ride them. */
1361
+ if (r->arena == arena) r->arena_owner = 1;
1216
1362
  return r;
1217
1363
  }
1218
1364
 
@@ -1753,14 +1899,15 @@ LEPTRIS_API LeptrisDocument leptris_document_from_plan_result(
1753
1899
  return doc;
1754
1900
  }
1755
1901
 
1756
- LEPTRIS_API LeptrisPlanResult leptris_plan_result_build(
1902
+ /* #1677: the WRAPPER owns the TLS window and the arena — the
1903
+ * windowed body never restores or frees either, so no return path
1904
+ * can leave the thread-local stuck on a leaked arena. (The
1905
+ * multiple-roots early return did exactly that: every later
1906
+ * malloc-lane mint on the thread then rode the dead arena, and
1907
+ * freeing those interior pointers corrupted the heap.) */
1908
+ static struct leptris_plan_result* plan_result_build_windowed(
1757
1909
  LeptrisPlan plan, const leptris_plan_row_op* ops, size_t count,
1758
- LeptrisStatus* status) {
1759
- if (status) *status = LEPTRIS_OK;
1760
- if (!plan || !ops || count == 0) {
1761
- if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1762
- return NULL;
1763
- }
1910
+ LeptrisStatus* status, struct dp_arena* arena) {
1764
1911
  const struct leptris_plan* pool =
1765
1912
  (const struct leptris_plan*)plan;
1766
1913
  /* op stack of open elements; depth is program-bounded */
@@ -1780,9 +1927,9 @@ LEPTRIS_API LeptrisPlanResult leptris_plan_result_build(
1780
1927
  (op->row_index != UINT32_MAX &&
1781
1928
  op->row_index >= pool->plans[op->plan_index]
1782
1929
  .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);
1930
+ /* Mid-build failure: the arena holds every minted
1931
+ * value — release the chunks wholesale (the owner
1932
+ * flag isn't set on failure paths). */
1786
1933
  free(stack);
1787
1934
  if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1788
1935
  return NULL;
@@ -1805,8 +1952,9 @@ LEPTRIS_API LeptrisPlanResult leptris_plan_result_build(
1805
1952
  v->ns_uri = dp_strdup(cr->ns_uri);
1806
1953
  }
1807
1954
  if (sp == 0) {
1808
- if (root) { /* multiple roots */
1809
- dp_result_free_rec(v);
1955
+ if (root) {
1956
+ /* multiple roots: the partial tree rides the
1957
+ * arena — the WRAPPER frees it (#1677). */
1810
1958
  free(stack);
1811
1959
  if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1812
1960
  return NULL;
@@ -1825,9 +1973,9 @@ LEPTRIS_API LeptrisPlanResult leptris_plan_result_build(
1825
1973
  stack[sp++] = v;
1826
1974
  } else if (op->kind == LEPTRIS_PLAN_OP_SCALAR) {
1827
1975
  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);
1976
+ /* Mid-build failure: the arena holds every minted
1977
+ * value — release the chunks wholesale (the owner
1978
+ * flag isn't set on failure paths). */
1831
1979
  free(stack);
1832
1980
  if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1833
1981
  return NULL;
@@ -1836,7 +1984,6 @@ LEPTRIS_API LeptrisPlanResult leptris_plan_result_build(
1836
1984
  * reads cp->child_count right after, so an out-of-pool
1837
1985
  * plan_index here is an OOB read (leptris#1654). */
1838
1986
  if (op->plan_index >= pool->plan_count) {
1839
- if (root) dp_result_free_rec(root);
1840
1987
  free(stack);
1841
1988
  if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1842
1989
  return NULL;
@@ -1858,28 +2005,55 @@ LEPTRIS_API LeptrisPlanResult leptris_plan_result_build(
1858
2005
  }
1859
2006
  } else if (op->kind == LEPTRIS_PLAN_OP_ATTR) {
1860
2007
  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);
2008
+ /* Mid-build failure: the arena holds every minted
2009
+ * value — release the chunks wholesale (the owner
2010
+ * flag isn't set on failure paths). */
1864
2011
  free(stack);
1865
2012
  if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1866
2013
  return NULL;
1867
2014
  }
1868
2015
  struct leptris_plan_result* el = stack[sp - 1];
1869
- char** gn = (char**)realloc(
1870
- el->attr_names,
1871
- (el->attr_count + 1) * sizeof(char*));
1872
- char** gv = (char**)realloc(
1873
- el->attr_values,
1874
- (el->attr_count + 1) * sizeof(char*));
1875
- char** gp = (char**)realloc(
1876
- el->attr_ns_prefixes,
1877
- (el->attr_count + 1) * sizeof(char*));
1878
- char** gu = (char**)realloc(
1879
- el->attr_ns_uris,
1880
- (el->attr_count + 1) * sizeof(char*));
2016
+ char** gn;
2017
+ char** gv;
2018
+ char** gp;
2019
+ char** gu;
2020
+ if (el->arena) {
2021
+ /* #1670: arena growth — alloc new, copy, waste old
2022
+ * until the wholesale release. */
2023
+ gn = (char**)dp_arena_alloc(
2024
+ &el->arena, (el->attr_count + 1) * sizeof(char*));
2025
+ gv = (char**)dp_arena_alloc(
2026
+ &el->arena, (el->attr_count + 1) * sizeof(char*));
2027
+ gp = (char**)dp_arena_alloc(
2028
+ &el->arena, (el->attr_count + 1) * sizeof(char*));
2029
+ gu = (char**)dp_arena_alloc(
2030
+ &el->arena, (el->attr_count + 1) * sizeof(char*));
2031
+ if (gn) memcpy(gn, el->attr_names,
2032
+ el->attr_count * sizeof(char*));
2033
+ if (gv) memcpy(gv, el->attr_values,
2034
+ el->attr_count * sizeof(char*));
2035
+ if (gp) memcpy(gp, el->attr_ns_prefixes,
2036
+ el->attr_count * sizeof(char*));
2037
+ if (gu) memcpy(gu, el->attr_ns_uris,
2038
+ el->attr_count * sizeof(char*));
2039
+ } else {
2040
+ gn = (char**)realloc(
2041
+ el->attr_names,
2042
+ (el->attr_count + 1) * sizeof(char*));
2043
+ gv = (char**)realloc(
2044
+ el->attr_values,
2045
+ (el->attr_count + 1) * sizeof(char*));
2046
+ gp = (char**)realloc(
2047
+ el->attr_ns_prefixes,
2048
+ (el->attr_count + 1) * sizeof(char*));
2049
+ gu = (char**)realloc(
2050
+ el->attr_ns_uris,
2051
+ (el->attr_count + 1) * sizeof(char*));
2052
+ }
1881
2053
  if (!gn || !gv || !gp || !gu) {
1882
- free(gn); free(gv); free(gp); free(gu);
2054
+ if (!el->arena) {
2055
+ free(gn); free(gv); free(gp); free(gu);
2056
+ }
1883
2057
  goto oom;
1884
2058
  }
1885
2059
  el->attr_names = gn;
@@ -1897,9 +2071,9 @@ LEPTRIS_API LeptrisPlanResult leptris_plan_result_build(
1897
2071
  el->attr_count++;
1898
2072
  } else if (op->kind == LEPTRIS_PLAN_OP_END) {
1899
2073
  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);
2074
+ /* Mid-build failure: the arena holds every minted
2075
+ * value — release the chunks wholesale (the owner
2076
+ * flag isn't set on failure paths). */
1903
2077
  free(stack);
1904
2078
  if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1905
2079
  return NULL;
@@ -1910,19 +2084,48 @@ LEPTRIS_API LeptrisPlanResult leptris_plan_result_build(
1910
2084
  int balanced = (sp == 0 && root != NULL);
1911
2085
  free(stack);
1912
2086
  if (!balanced) {
1913
- dp_result_free_rec(root);
1914
2087
  if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1915
2088
  return NULL;
1916
2089
  }
1917
2090
  return (LeptrisPlanResult)root;
1918
2091
 
1919
2092
  oom:
1920
- if (root) dp_result_free_rec(root);
1921
2093
  free(stack);
1922
2094
  if (status) *status = LEPTRIS_ERROR_MEMORY;
1923
2095
  return NULL;
1924
2096
  }
1925
2097
 
2098
+ LEPTRIS_API LeptrisPlanResult leptris_plan_result_build(
2099
+ LeptrisPlan plan, const leptris_plan_row_op* ops, size_t count,
2100
+ LeptrisStatus* status) {
2101
+ if (status) *status = LEPTRIS_OK;
2102
+ if (!plan || !ops || count == 0) {
2103
+ if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
2104
+ return NULL;
2105
+ }
2106
+ /* #1670: every value/string/array minted below rides this
2107
+ * arena; the wholesale free releases all chunks at once.
2108
+ * #1677: window + arena owned HERE — restored/freed on every
2109
+ * exit, including the NULL paths the body takes. */
2110
+ struct dp_arena* arena = dp_arena_new(DP_ARENA_CHUNK);
2111
+ if (!arena) {
2112
+ if (status) *status = LEPTRIS_ERROR_MEMORY;
2113
+ return NULL;
2114
+ }
2115
+ struct dp_arena* saved_arena = dp_cur_arena;
2116
+ dp_cur_arena = arena;
2117
+ struct leptris_plan_result* r =
2118
+ plan_result_build_windowed(plan, ops, count, status, arena);
2119
+ dp_cur_arena = saved_arena;
2120
+ if (!r) {
2121
+ dp_arena_free_all(arena);
2122
+ return NULL;
2123
+ }
2124
+ /* The build root owns the chunks. */
2125
+ if (r->arena == arena) r->arena_owner = 1;
2126
+ return (LeptrisPlanResult)r;
2127
+ }
2128
+
1926
2129
  LEPTRIS_API void leptris_plan_result_free(LeptrisPlanResult result) {
1927
2130
  dp_result_free_rec((struct leptris_plan_result*)result);
1928
2131
  }
@@ -2013,6 +2216,45 @@ LEPTRIS_API const char* leptris_plan_value_attribute(const LeptrisPlanResult v,
2013
2216
  return NULL;
2014
2217
  }
2015
2218
 
2219
+ LEPTRIS_API size_t leptris_plan_value_attribute_count(
2220
+ const LeptrisPlanResult v) {
2221
+ return v ? v->attr_count : 0;
2222
+ }
2223
+
2224
+ LEPTRIS_API size_t leptris_plan_value_attributes_snapshot(
2225
+ const LeptrisPlanResult v,
2226
+ char* names_blob, size_t names_cap, size_t* name_offsets,
2227
+ char* values_blob, size_t values_cap, size_t* value_offsets) {
2228
+ if (!v) return 0;
2229
+ size_t nneed = 0, vneed = 0;
2230
+ for (size_t i = 0; i < v->attr_count; i++) {
2231
+ nneed += strlen(v->attr_names[i]) + 1;
2232
+ vneed += strlen(v->attr_values[i]) + 1;
2233
+ }
2234
+ size_t need = nneed > vneed ? nneed : vneed;
2235
+ /* Sizing call: NULL blobs (or zero caps) report the required
2236
+ * per-blob capacity — both blobs need the same. */
2237
+ if (!names_blob || !names_cap || !values_blob || !values_cap)
2238
+ return need;
2239
+ if (!name_offsets || !value_offsets) return (size_t)-1;
2240
+ if (nneed > names_cap || vneed > values_cap)
2241
+ return (size_t)-1;
2242
+ size_t noff = 0, voff = 0;
2243
+ for (size_t i = 0; i < v->attr_count; i++) {
2244
+ size_t nl = strlen(v->attr_names[i]);
2245
+ memcpy(names_blob + noff, v->attr_names[i], nl + 1);
2246
+ name_offsets[i] = noff;
2247
+ noff += nl + 1;
2248
+ size_t vl = strlen(v->attr_values[i]);
2249
+ memcpy(values_blob + voff, v->attr_values[i], vl + 1);
2250
+ value_offsets[i] = voff;
2251
+ voff += vl + 1;
2252
+ }
2253
+ name_offsets[v->attr_count] = noff;
2254
+ value_offsets[v->attr_count] = voff;
2255
+ return 0;
2256
+ }
2257
+
2016
2258
  /* ---- #1269a typed accessors ------------------------------------- */
2017
2259
 
2018
2260
  LEPTRIS_API int leptris_plan_value_int(const LeptrisPlanResult v,
@@ -1652,6 +1652,100 @@ TEST(Plan1565, NestedContentRowEmitsInlineText) {
1652
1652
  leptris_document_free(doc);
1653
1653
  }
1654
1654
 
1655
+ /* leptris#1673: nested-capture + serialize_EXT (the #425 indent
1656
+ * form) under the #1670 walk arenas — the binding's descriptor
1657
+ * suite segfaults here on Ruby 3.3 linux/PGO builds. Regression
1658
+ * pin at the C level. */
1659
+ TEST(Plan1673, NestedCaptureSerializeExtStaysAlive) {
1660
+ LeptrisStatus st = LEPTRIS_OK;
1661
+ const char* xml =
1662
+ "<w:fonts xmlns:w=\"urn:w\">"
1663
+ "<w:font name=\"A\"><w:sz>12</w:sz></w:font>"
1664
+ "<w:font name=\"B\"><w:sz>14</w:sz></w:font>"
1665
+ "</w:fonts>";
1666
+ LeptrisDocument doc = leptris_parse_string(xml, strlen(xml), &st);
1667
+ ASSERT_NE(doc, nullptr);
1668
+
1669
+ leptris_child_plan font_row[1] = {};
1670
+ font_row[0].wire_name = "font";
1671
+ font_row[0].kind = LEPTRIS_PLAN_KIND_NESTED;
1672
+ font_row[0].child_plan_index = 1;
1673
+ font_row[0].ns_form = LEPTRIS_PLAN_NS_EXACT;
1674
+ font_row[0].ns_uri = "urn:w";
1675
+ font_row[0].ns_prefix = "w";
1676
+
1677
+ leptris_element_plan font_plan[1] = {};
1678
+ font_plan[0].element_name = "font";
1679
+ font_plan[0].ns_form = LEPTRIS_PLAN_NS_EXACT;
1680
+ font_plan[0].ns_uri = "urn:w";
1681
+ font_plan[0].ns_prefix = "w";
1682
+ leptris_child_plan sz_row[1] = {};
1683
+ sz_row[0].wire_name = "sz";
1684
+ sz_row[0].kind = LEPTRIS_PLAN_KIND_SCALAR;
1685
+ sz_row[0].ns_form = LEPTRIS_PLAN_NS_EXACT;
1686
+ sz_row[0].ns_uri = "urn:w";
1687
+ sz_row[0].ns_prefix = "w";
1688
+ font_plan[0].child_count = 1;
1689
+ font_plan[0].child_plans = sz_row;
1690
+
1691
+ leptris_child_plan fonts_row[1] = {};
1692
+ fonts_row[0].wire_name = "font";
1693
+ fonts_row[0].kind = LEPTRIS_PLAN_KIND_NESTED;
1694
+ fonts_row[0].child_plan_index = 1;
1695
+ fonts_row[0].ns_form = LEPTRIS_PLAN_NS_EXACT;
1696
+ fonts_row[0].ns_uri = "urn:w";
1697
+ fonts_row[0].ns_prefix = "w";
1698
+
1699
+ leptris_element_plan plans[2] = {};
1700
+ plans[0].element_name = "fonts";
1701
+ plans[0].child_count = 1;
1702
+ plans[0].child_plans = fonts_row;
1703
+ plans[0].ns_form = LEPTRIS_PLAN_NS_EXACT;
1704
+ plans[0].ns_uri = "urn:w";
1705
+ plans[0].ns_prefix = "w";
1706
+ plans[1].element_name = "font";
1707
+ plans[1].child_count = 1;
1708
+ plans[1].child_plans = sz_row;
1709
+ plans[1].ns_form = LEPTRIS_PLAN_NS_EXACT;
1710
+ plans[1].ns_uri = "urn:w";
1711
+ plans[1].ns_prefix = "w";
1712
+
1713
+ leptris_plan_spec spec = {};
1714
+ spec.abi_version = leptris_plan_abi_version();
1715
+ spec.plan_count = 2;
1716
+ spec.plans = plans;
1717
+
1718
+ LeptrisPlan plan = leptris_plan_build(&spec, &st);
1719
+ ASSERT_NE(plan, nullptr);
1720
+ LeptrisPlanResult r =
1721
+ leptris_plan_walk(doc, leptris_document_root(doc), plan, &st);
1722
+ ASSERT_NE(r, nullptr);
1723
+
1724
+ /* compact form */
1725
+ char* out = leptris_plan_serialize(plan, r, &st);
1726
+ ASSERT_NE(out, nullptr);
1727
+ EXPECT_STREQ(out,
1728
+ "<w:fonts xmlns:w=\"urn:w\">"
1729
+ "<w:font><w:sz>12</w:sz></w:font>"
1730
+ "<w:font><w:sz>14</w:sz></w:font>"
1731
+ "</w:fonts>");
1732
+ leptris_free_string(out);
1733
+
1734
+ /* EXT form (the #425 crash path): indent > 0 */
1735
+ leptris_plan_serialize_options opts = {};
1736
+ opts.indent = 2;
1737
+ char* pretty = leptris_plan_serialize_ext(plan, r, &opts,
1738
+ sizeof(opts), &st);
1739
+ ASSERT_NE(pretty, nullptr);
1740
+ EXPECT_NE(strstr(pretty, "<w:sz>14</w:sz>"), nullptr);
1741
+ EXPECT_NE(strstr(pretty, "<w:font>"), nullptr);
1742
+ leptris_free_string(pretty);
1743
+
1744
+ leptris_plan_result_free(r);
1745
+ leptris_plan_free(plan);
1746
+ leptris_document_free(doc);
1747
+ }
1748
+
1655
1749
  TEST(Plan1551, TextAndAttributeValuesAreEscaped) {
1656
1750
  LeptrisStatus st = LEPTRIS_OK;
1657
1751
  const char* xml =
@@ -2384,3 +2478,233 @@ TEST(PlanBuild, ScalarOpRejectsOutOfPoolPlanIndex) {
2384
2478
  }
2385
2479
 
2386
2480
  } // namespace
2481
+
2482
+ /* leptris#1670: the batch attribute face — one crossing for ALL
2483
+ * attribute names + values of a node (the per-name
2484
+ * leptris_plan_value_attribute walk mints one FFI/wrapper round
2485
+ * trip per attribute per node). */
2486
+ /* leptris#1673: the #1670 walk arena returns NON-zeroed memory —
2487
+ * the walk's fixed-capacity attribute arrays relied on calloc's
2488
+ * NULL-fill for UNBOUND rows, so the serializer's declaration walk
2489
+ * read 0xbe garbage through attr_ns_prefixes. Zeroed at allocation
2490
+ * now; this spec pins both serialize faces on a prefixed nested
2491
+ * plan (the binding's "nested-capture serialization" shape). */
2492
+ TEST(Plan1673, SerializeFacesOnPrefixedNestedPlan) {
2493
+ LeptrisStatus st = LEPTRIS_OK;
2494
+ const char* xml =
2495
+ "<w:fonts xmlns:w=\"urn:w\">"
2496
+ "<w:font name=\"A\"><w:sz>12</w:sz></w:font></w:fonts>";
2497
+ LeptrisDocument doc = leptris_parse_string(xml, strlen(xml), &st);
2498
+ ASSERT_NE(doc, nullptr);
2499
+
2500
+ leptris_attr_plan font_attrs[1] = {};
2501
+ font_attrs[0].wire_name = "name";
2502
+ font_attrs[0].kind = LEPTRIS_PLAN_KIND_SCALAR;
2503
+ leptris_child_plan font_kids[1] = {};
2504
+ font_kids[0].wire_name = "sz";
2505
+ font_kids[0].kind = LEPTRIS_PLAN_KIND_SCALAR;
2506
+ font_kids[0].ns_form = LEPTRIS_PLAN_NS_EXACT;
2507
+ font_kids[0].ns_uri = "urn:w";
2508
+ font_kids[0].ns_prefix = "w";
2509
+ leptris_child_plan root_kids[1] = {};
2510
+ root_kids[0].wire_name = "font";
2511
+ root_kids[0].kind = LEPTRIS_PLAN_KIND_NESTED;
2512
+ root_kids[0].child_plan_index = 1;
2513
+ root_kids[0].ns_form = LEPTRIS_PLAN_NS_EXACT;
2514
+ root_kids[0].ns_uri = "urn:w";
2515
+ root_kids[0].ns_prefix = "w";
2516
+ leptris_element_plan plans[2] = {};
2517
+ plans[0].element_name = "fonts";
2518
+ plans[0].child_count = 1;
2519
+ plans[0].child_plans = root_kids;
2520
+ plans[0].ns_form = LEPTRIS_PLAN_NS_EXACT;
2521
+ plans[0].ns_uri = "urn:w";
2522
+ plans[0].ns_prefix = "w";
2523
+ plans[1].element_name = "font";
2524
+ plans[1].attribute_count = 1;
2525
+ plans[1].attribute_plans = font_attrs;
2526
+ plans[1].child_count = 1;
2527
+ plans[1].child_plans = font_kids;
2528
+ plans[1].ns_form = LEPTRIS_PLAN_NS_EXACT;
2529
+ plans[1].ns_uri = "urn:w";
2530
+ plans[1].ns_prefix = "w";
2531
+
2532
+ leptris_plan_spec spec = {};
2533
+ spec.abi_version = leptris_plan_abi_version();
2534
+ spec.plan_count = 2;
2535
+ spec.plans = plans;
2536
+ LeptrisPlan plan = leptris_plan_build(&spec, &st);
2537
+ ASSERT_NE(plan, nullptr);
2538
+ LeptrisPlanResult r =
2539
+ leptris_plan_walk(doc, leptris_document_root(doc), plan, &st);
2540
+ ASSERT_NE(r, nullptr);
2541
+
2542
+ char* out = leptris_plan_serialize(plan, r, &st);
2543
+ ASSERT_NE(out, nullptr);
2544
+ EXPECT_STREQ(
2545
+ out, "<w:fonts xmlns:w=\"urn:w\">"
2546
+ "<w:font name=\"A\"><w:sz>12</w:sz></w:font></w:fonts>");
2547
+ leptris_free_string(out);
2548
+
2549
+ leptris_plan_serialize_options opts = {};
2550
+ opts.indent = 2;
2551
+ char* pretty = leptris_plan_serialize_ext(plan, r, &opts,
2552
+ sizeof(opts), &st);
2553
+ ASSERT_NE(pretty, nullptr);
2554
+ EXPECT_STREQ(pretty,
2555
+ "<w:fonts xmlns:w=\"urn:w\">\n"
2556
+ " <w:font name=\"A\">\n"
2557
+ " <w:sz>12</w:sz>\n"
2558
+ " </w:font>\n"
2559
+ "</w:fonts>\n");
2560
+ leptris_free_string(pretty);
2561
+
2562
+ leptris_plan_result_free(r);
2563
+ leptris_plan_free(plan);
2564
+ leptris_document_free(doc);
2565
+ }
2566
+
2567
+ TEST(Plan1670, AttributesSnapshotOneCrossing) {
2568
+ LeptrisStatus st = LEPTRIS_OK;
2569
+ const char* xml =
2570
+ "<r w:a=\"1\" w:b=\"2\" w:c=\"3\""
2571
+ " xmlns:w=\"urn:w\"/>";
2572
+ LeptrisDocument doc = leptris_parse_string(xml, strlen(xml), &st);
2573
+ ASSERT_NE(doc, nullptr);
2574
+
2575
+ leptris_attr_plan attrs[3] = {};
2576
+ attrs[0].wire_name = "a";
2577
+ attrs[1].wire_name = "b";
2578
+ attrs[2].wire_name = "c";
2579
+ leptris_child_plan kids[1] = {};
2580
+ kids[0].wire_name = "i";
2581
+ kids[0].kind = LEPTRIS_PLAN_KIND_SCALAR;
2582
+ leptris_element_plan plans[2] = {};
2583
+ plans[0].element_name = "r";
2584
+ plans[0].attribute_count = 3;
2585
+ plans[0].attribute_plans = attrs;
2586
+ plans[0].child_count = 1;
2587
+ plans[0].child_plans = kids;
2588
+ plans[1].element_name = "i";
2589
+ leptris_plan_spec spec = {};
2590
+ spec.abi_version = leptris_plan_abi_version();
2591
+ spec.plan_count = 2;
2592
+ spec.plans = plans;
2593
+ LeptrisPlan plan = leptris_plan_build(&spec, &st);
2594
+ ASSERT_NE(plan, nullptr);
2595
+
2596
+ leptris_plan_row_op ops[] = {
2597
+ {LEPTRIS_PLAN_OP_ELEMENT, 0, UINT32_MAX, NULL, NULL, 0},
2598
+ {LEPTRIS_PLAN_OP_ATTR, 0, 0, "a", "1", 1},
2599
+ {LEPTRIS_PLAN_OP_ATTR, 0, 0, "b", "2", 1},
2600
+ {LEPTRIS_PLAN_OP_ATTR, 0, 0, "c", "3", 1},
2601
+ {LEPTRIS_PLAN_OP_SCALAR, 0, 0, NULL, "x", 1},
2602
+ {LEPTRIS_PLAN_OP_END, 0, 0, NULL, NULL, 0},
2603
+ };
2604
+ LeptrisPlanResult r = leptris_plan_result_build(plan, ops, 6, &st);
2605
+ ASSERT_NE(r, nullptr);
2606
+
2607
+ /* The count sizes the buffers. */
2608
+ ASSERT_EQ(leptris_plan_value_attribute_count(r), 3u);
2609
+
2610
+ /* Two-phase: NULL buffers report the per-blob capacity. */
2611
+ size_t need = leptris_plan_value_attributes_snapshot(
2612
+ r, NULL, 0, NULL, NULL, 0, NULL);
2613
+ EXPECT_GT(need, 0u);
2614
+
2615
+ std::vector<char> names(need), values(need);
2616
+ std::vector<size_t> noffs(4), voffs(4);
2617
+ EXPECT_EQ(leptris_plan_value_attributes_snapshot(
2618
+ r, names.data(), need, noffs.data(),
2619
+ values.data(), need, voffs.data()), 0u);
2620
+ /* Third phase retry with small caps -> the sentinel. */
2621
+ EXPECT_EQ(leptris_plan_value_attributes_snapshot(
2622
+ r, names.data(), 2, noffs.data(),
2623
+ values.data(), 2, voffs.data()),
2624
+ (size_t)-1);
2625
+
2626
+ /* Entries: plan row order, NUL-terminated, offset-addressed. */
2627
+ const char* expect_n[3] = {"a", "b", "c"};
2628
+ const char* expect_v[3] = {"1", "2", "3"};
2629
+ for (size_t i = 0; i < 3; i++) {
2630
+ EXPECT_STREQ(names.data() + noffs[i], expect_n[i]);
2631
+ EXPECT_STREQ(values.data() + voffs[i], expect_v[i]);
2632
+ /* The per-name face agrees with the snapshot. */
2633
+ EXPECT_STREQ(leptris_plan_value_attribute(r, expect_n[i]),
2634
+ expect_v[i]);
2635
+ }
2636
+ EXPECT_EQ(noffs[3], need);
2637
+ EXPECT_EQ(voffs[3], need);
2638
+
2639
+ /* A non-ELEMENT value reports zero attributes. */
2640
+ LeptrisPlanResult kid = leptris_plan_value_at(r, 0);
2641
+ ASSERT_NE(kid, nullptr);
2642
+ EXPECT_EQ(leptris_plan_value_attribute_count(kid), 0u);
2643
+ EXPECT_EQ(leptris_plan_value_attributes_snapshot(
2644
+ kid, names.data(), need, noffs.data(),
2645
+ values.data(), need, voffs.data()), 0u);
2646
+
2647
+ leptris_plan_result_free(r);
2648
+ leptris_plan_free(plan);
2649
+ leptris_document_free(doc);
2650
+ }
2651
+
2652
+ /* leptris#1677: the arena window must survive EVERY return path.
2653
+ * The multiple-roots early return in the build used to leave the
2654
+ * thread-local stuck on the leaked arena — every later serialize
2655
+ * in the SAME process then rode the dead arena and freed interior
2656
+ * pointers (heap corruption, garbage/UTF-8 crashes at ~50%). One
2657
+ * process, interleaved sequencing — the shape per-test isolation
2658
+ * hides. */
2659
+ TEST(Plan1677, WindowSurvivesMultipleRootsBuildFailure) {
2660
+ LeptrisStatus st = LEPTRIS_OK;
2661
+ const char* xml =
2662
+ "<r><i>x</i></r>";
2663
+ LeptrisDocument doc = leptris_parse_string(xml, strlen(xml), &st);
2664
+ ASSERT_NE(doc, nullptr);
2665
+
2666
+ leptris_child_plan kids[1] = {};
2667
+ kids[0].wire_name = "i";
2668
+ kids[0].kind = LEPTRIS_PLAN_KIND_SCALAR;
2669
+ leptris_element_plan plans[1] = {};
2670
+ plans[0].element_name = "r";
2671
+ plans[0].child_count = 1;
2672
+ plans[0].child_plans = kids;
2673
+ leptris_plan_spec spec = {};
2674
+ spec.abi_version = leptris_plan_abi_version();
2675
+ spec.plan_count = 1;
2676
+ spec.plans = plans;
2677
+ LeptrisPlan plan = leptris_plan_build(&spec, &st);
2678
+ ASSERT_NE(plan, nullptr);
2679
+
2680
+ for (int round = 0; round < 50; round++) {
2681
+ /* 1. the poisoner: a TWO-root op program — the build must
2682
+ * fail INVALID_ARG and restore the window. */
2683
+ leptris_plan_row_op two_roots[] = {
2684
+ {LEPTRIS_PLAN_OP_ELEMENT, 0, UINT32_MAX, NULL, NULL, 0},
2685
+ {LEPTRIS_PLAN_OP_END, 0, 0, NULL, NULL, 0},
2686
+ {LEPTRIS_PLAN_OP_ELEMENT, 0, UINT32_MAX, NULL, NULL, 0},
2687
+ {LEPTRIS_PLAN_OP_END, 0, 0, NULL, NULL, 0},
2688
+ };
2689
+ st = LEPTRIS_OK;
2690
+ EXPECT_EQ(leptris_plan_result_build(plan, two_roots, 4, &st),
2691
+ nullptr);
2692
+ EXPECT_EQ(st, LEPTRIS_ERROR_INVALID_ARG);
2693
+
2694
+ /* 2. the victim: walk + serialize in the SAME process —
2695
+ * with the window stuck this rides the leaked arena and
2696
+ * corrupts (pre-fix: ASAN invalid-free + garbage). */
2697
+ st = LEPTRIS_OK;
2698
+ LeptrisPlanResult r =
2699
+ leptris_plan_walk(doc, leptris_document_root(doc), plan, &st);
2700
+ ASSERT_NE(r, nullptr) << "round " << round;
2701
+ char* out = leptris_plan_serialize(plan, r, &st);
2702
+ ASSERT_NE(out, nullptr) << "round " << round;
2703
+ EXPECT_STREQ(out, "<r><i>x</i></r>") << "round " << round;
2704
+ leptris_free_string(out);
2705
+ leptris_plan_result_free(r);
2706
+ }
2707
+
2708
+ leptris_plan_free(plan);
2709
+ leptris_document_free(doc);
2710
+ }
@@ -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.346",
4
+ "version": "1.9.349",
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leptris
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.346.0
4
+ version: 1.9.349.0
5
5
  platform: ppc64le-linux
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-10-12 00:00:00.000000000 Z
11
+ date: 2026-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi