leptris 1.9.346.0-arm-linux → 1.9.351.0-arm-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 (50) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -0
  3. data/Rakefile +1 -1
  4. data/lib/leptris/version.rb +1 -1
  5. data/lib/leptris/xml/ffi.rb +11 -0
  6. data/lib/leptris/xml/plan_value.rb +63 -19
  7. data/lib/libleptris.so +0 -0
  8. data/vendor-src/README.md +1 -1
  9. data/vendor-src/libleptris/CHANGELOG.md +145 -0
  10. data/vendor-src/libleptris/CMakeLists.txt +1 -1
  11. data/vendor-src/libleptris/cli/commands/xquery.c +1 -1
  12. data/vendor-src/libleptris/src/include/leptris/descriptor.h +26 -0
  13. data/vendor-src/libleptris/src/leptris/common/simd_text.c +0 -12
  14. data/vendor-src/libleptris/src/leptris/common/simd_text.h +19 -0
  15. data/vendor-src/libleptris/src/leptris/common/simd_text_neon.c +0 -1
  16. data/vendor-src/libleptris/src/leptris/core.c +2 -5
  17. data/vendor-src/libleptris/src/leptris/descriptor/plan.c +300 -58
  18. data/vendor-src/libleptris/src/leptris/dom/root_doc_map.h +3 -0
  19. data/vendor-src/libleptris/src/leptris/flat/direct_parse.c +8 -33
  20. data/vendor-src/libleptris/src/leptris/flat/direct_parse.h +4 -0
  21. data/vendor-src/libleptris/src/leptris/html/html_parse.c +0 -2
  22. data/vendor-src/libleptris/src/leptris/leptris.c +6 -41
  23. data/vendor-src/libleptris/src/leptris/leptris_internal.h +14 -0
  24. data/vendor-src/libleptris/src/leptris/memory/pool.h +4 -0
  25. data/vendor-src/libleptris/src/leptris/xpath/evaluator.c +0 -2
  26. data/vendor-src/libleptris/src/leptris/xpath/evaluator_operators.c +2 -8
  27. data/vendor-src/libleptris/src/leptris/xpath/functions.c +1 -5
  28. data/vendor-src/libleptris/src/leptris/xpath/functions.h +29 -0
  29. data/vendor-src/libleptris/src/leptris/xpath/functions_ext31.c +1 -19
  30. data/vendor-src/libleptris/src/leptris/xpath/xpath_carrier.h +32 -0
  31. data/vendor-src/libleptris/src/leptris/xpath/xpath_public.c +1 -15
  32. data/vendor-src/libleptris/src/leptris/xquery/xquery.c +1 -2
  33. data/vendor-src/libleptris/src/leptris/xsd/compile.c +158 -3
  34. data/vendor-src/libleptris/src/leptris/xsd/validate.c +13 -9
  35. data/vendor-src/libleptris/src/leptris/xsd/xsd_internal.h +8 -0
  36. data/vendor-src/libleptris/src/leptris/xslt/xslt_exec.c +0 -6
  37. data/vendor-src/libleptris/src/leptris/xslt/xslt_functions.c +0 -2
  38. data/vendor-src/libleptris/src/leptris/xslt/xslt_pattern.c +0 -1
  39. data/vendor-src/libleptris/src/leptris/xslt/xslt_public.c +1 -2
  40. data/vendor-src/libleptris/test/abi/test_descriptor.cpp +324 -0
  41. data/vendor-src/libleptris/test/xquery/test_xquery.cpp +1 -1
  42. data/vendor-src/libleptris/test/xsd/libxml2-cases/fwd_ref_ext/lib/base.xsd +23 -0
  43. data/vendor-src/libleptris/test/xsd/libxml2-cases/fwd_ref_ext/lib/more.xsd +8 -0
  44. data/vendor-src/libleptris/test/xsd/libxml2-cases/fwd_ref_ext/root.xsd +10 -0
  45. data/vendor-src/libleptris/test/xsd/libxml2-cases/fwd_ref_ext/sub/wrap.xsd +3 -0
  46. data/vendor-src/libleptris/test/xsd/libxml2-cases/ns_simple_collision/m.xsd +14 -0
  47. data/vendor-src/libleptris/test/xsd/libxml2-cases/ns_simple_collision/w.xsd +14 -0
  48. data/vendor-src/libleptris/test/xsd/test_xsd_compile.cpp +70 -0
  49. data/vendor-src/libleptris/vcpkg.json +1 -1
  50. metadata +9 -2
@@ -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,
@@ -15,6 +15,9 @@ extern "C" {
15
15
 
16
16
  void leptris_root_doc_register(LeptrisElement root, struct leptris_document* doc);
17
17
  void leptris_root_doc_unregister(LeptrisElement root);
18
+ /* #1038: unregister every element registered for this document
19
+ * (fallback-path registrations). Returns the number removed. */
20
+ size_t leptris_root_doc_unregister_doc(struct leptris_document* doc);
18
21
  struct leptris_document* leptris_root_doc_lookup(LeptrisElement root);
19
22
  void leptris_root_doc_memo_invalidate(const struct leptris_document* doc);
20
23
  /* Test hook (#1242): the memo's current root, or NULL. */
@@ -37,11 +37,18 @@
37
37
  #include "../common/port.h"
38
38
  #include "../common/simd_text.h"
39
39
  #include "../dtd/model.h"
40
-
40
+ #include "../include/leptris/dtd.h" /* leptris_dtd_free is LEPTRIS_API */
41
+ #include "../dom/diag.h"
42
+ #include "../memory/pool.h"
41
43
  #include <string.h>
42
44
  #include <stdlib.h>
43
45
  #include <stdio.h>
44
46
 
47
+ /* File-scope externs: engine config TLS defined in core.c (declared
48
+ * in leptris_internal.h; this TU reads it without the umbrella) and
49
+ * a compact-mode hook. File scope, not block scope — block-scope
50
+ * externs break GCC LTO (leptris#1681). */
51
+ extern LEPTRIS_THREAD_LOCAL int g_leptris_max_depth;
45
52
  extern LEPTRIS_THREAD_LOCAL int g_leptris_strict_mode;
46
53
  extern void leptris_compact_set_current_document(struct leptris_document* doc);
47
54
  int leptris_element_add_namespace(struct leptris_element* elem,
@@ -636,10 +643,6 @@ static inline int dp_add_attr_inline(DParser* p, LeptrisElement elem,
636
643
  }
637
644
  if (dup) {
638
645
  if (p->doc) {
639
- extern struct LeptrisDiag* leptris_diag_emit(
640
- struct LeptrisDiag**, int*, int*,
641
- LeptrisDiagKind, LeptrisNodeRef,
642
- const char*, ...);
643
646
  char qname[130];
644
647
  size_t ql = attr->name_view.length < 129
645
648
  ? attr->name_view.length : 129;
@@ -889,7 +892,6 @@ static LEPTRIS_NOINLINE int dp_parse_doctype(char** pos_io, char* end,
889
892
  subset_start, (size_t)(subset_end - subset_start),
890
893
  pool);
891
894
  if (parsed) {
892
- extern void leptris_dtd_free(struct LeptrisDTD*);
893
895
  if (dtd) {
894
896
  /* a second <!DOCTYPE: the first model wins (the
895
897
  * duplicate-attribute keep-first rule); the
@@ -1597,7 +1599,6 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
1597
1599
  p.pool = pool;
1598
1600
  p.doc = doc;
1599
1601
  {
1600
- extern LEPTRIS_THREAD_LOCAL int g_leptris_max_depth;
1601
1602
  size_t dcap = g_leptris_max_depth > 0
1602
1603
  ? (size_t)g_leptris_max_depth : (size_t)DP_MAX_DEPTH;
1603
1604
  if (!dp_stacks_init(&p, dcap)) goto fail;
@@ -1979,7 +1980,6 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
1979
1980
  /* Respect custom depth limit (g_leptris_max_depth)
1980
1981
  * when set by the caller; fall back to the
1981
1982
  * compile-time default otherwise. */
1982
- extern LEPTRIS_THREAD_LOCAL int g_leptris_max_depth;
1983
1983
  int max_depth = g_leptris_max_depth > 0
1984
1984
  ? g_leptris_max_depth : DP_MAX_DEPTH;
1985
1985
  if (p.depth >= max_depth) goto fail;
@@ -2097,11 +2097,6 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
2097
2097
  while ((int)p.depth - 1 > anc) {
2098
2098
  LeptrisElement ce =
2099
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
2100
  leptris_diag_emit(
2106
2101
  &p.doc->parse_diags,
2107
2102
  &p.doc->parse_diag_count,
@@ -2121,10 +2116,6 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
2121
2116
  }
2122
2117
  goto close_matched;
2123
2118
  }
2124
- extern struct LeptrisDiag* leptris_diag_emit(
2125
- struct LeptrisDiag**, int*, int*,
2126
- LeptrisDiagKind, LeptrisNodeRef,
2127
- const char*, ...);
2128
2119
  leptris_diag_emit(
2129
2120
  &p.doc->parse_diags,
2130
2121
  &p.doc->parse_diag_count,
@@ -2347,10 +2338,6 @@ static struct leptris_document* direct_parse_internal(char* buf, size_t len,
2347
2338
  * implicitly; the tree commits. */
2348
2339
  while (p.depth > 0) {
2349
2340
  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
2341
  leptris_diag_emit(
2355
2342
  &p.doc->parse_diags, &p.doc->parse_diag_count,
2356
2343
  &p.doc->parse_diag_cap,
@@ -2493,7 +2480,6 @@ fail:
2493
2480
  * RECOVER diag (e.g. duplicate attribute) can precede the
2494
2481
  * failure — nightly-fuzz leak-75e0ee81, 2304 bytes. */
2495
2482
  {
2496
- extern void leptris_diag_free(struct LeptrisDiag**, int*, int*);
2497
2483
  leptris_diag_free(&doc->parse_diags, &doc->parse_diag_count,
2498
2484
  &doc->parse_diag_cap);
2499
2485
  }
@@ -2503,7 +2489,6 @@ fail:
2503
2489
  * assigns doc->dtd; without this free the model leaks
2504
2490
  * (nightly-fuzz leak-e3246602, 8 bytes). */
2505
2491
  {
2506
- extern void leptris_dtd_free(struct LeptrisDTD*);
2507
2492
  if (p.dtd) leptris_dtd_free(p.dtd);
2508
2493
  }
2509
2494
  free(doc->line_breaks);
@@ -2746,7 +2731,6 @@ static int il_scan_body(char* s, size_t len, int drop_ws, IlCtx* c,
2746
2731
  c->last_child[c->depth - 1] = ri;
2747
2732
 
2748
2733
  if (!r.self_closing) {
2749
- extern LEPTRIS_THREAD_LOCAL int g_leptris_max_depth;
2750
2734
  int max_depth = g_leptris_max_depth > 0
2751
2735
  ? g_leptris_max_depth : DP_MAX_DEPTH;
2752
2736
  if (c->depth >= max_depth) return 0;
@@ -2856,7 +2840,6 @@ static int il_scan_body(char* s, size_t len, int drop_ws, IlCtx* c,
2856
2840
  int leptris_il_scan(char* s, size_t len, int drop_ws, IlCtx* c,
2857
2841
  uint32_t* root_out, size_t* trail_off,
2858
2842
  size_t* trail_len) {
2859
- extern LEPTRIS_THREAD_LOCAL int g_leptris_max_depth;
2860
2843
  size_t dcap = g_leptris_max_depth > 0
2861
2844
  ? (size_t)g_leptris_max_depth : (size_t)LEPTRIS_IL_MAX_DEPTH;
2862
2845
  if (!il_stacks_init(c, dcap)) return 0;
@@ -2872,10 +2855,6 @@ int leptris_il_scan(char* s, size_t len, int drop_ws, IlCtx* c,
2872
2855
  static struct leptris_document* dp_il_build(
2873
2856
  char* scratch, size_t len, IlCtx* c, uint32_t root_rec,
2874
2857
  size_t trail_off, size_t trail_len, int drop_ws) {
2875
- extern LeptrisArena* leptris_arena_create(size_t);
2876
- extern LeptrisMemoryPool* leptris_pool_create_arena_backed(
2877
- LeptrisArena*, int);
2878
- extern void leptris_arena_destroy(LeptrisArena*);
2879
2858
 
2880
2859
  /* scratch (with its NUL runway) is caller-built and becomes the
2881
2860
  * document's parse_scratch; pristine is the immutable copy. */
@@ -3113,10 +3092,6 @@ static struct leptris_document* dp_il_build(
3113
3092
  }
3114
3093
  if (dup) {
3115
3094
  if (doc) {
3116
- extern struct LeptrisDiag* leptris_diag_emit(
3117
- struct LeptrisDiag**, int*, int*,
3118
- LeptrisDiagKind, LeptrisNodeRef,
3119
- const char*, ...);
3120
3095
  char qn[130];
3121
3096
  size_t ql = a->name_len < 129
3122
3097
  ? a->name_len : 129;
@@ -38,4 +38,8 @@ struct leptris_document* direct_parse(const char* xml, size_t len);
38
38
  */
39
39
  struct leptris_document* direct_parse_inplace(char* buf, size_t len);
40
40
 
41
+ /* Flags variant — carries LEPTRIS_PARSE_* flags (recover mode #1660). */
42
+ struct leptris_document* direct_parse_flags(const char* xml, size_t len,
43
+ unsigned parse_flags);
44
+
41
45
  #endif /* LEPTRIS_FLAT_DIRECT_PARSE_H */