leptris 1.9.355.0-arm-linux → 1.9.359.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 (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/README.adoc +13 -5
  4. data/Rakefile +1 -1
  5. data/ext/leptris/native/native.c +383 -5
  6. data/lib/leptris/version.rb +1 -1
  7. data/lib/leptris/xml/cdata.rb +3 -0
  8. data/lib/leptris/xml/comment.rb +3 -0
  9. data/lib/leptris/xml/declaration.rb +59 -0
  10. data/lib/leptris/xml/doc_type.rb +33 -1
  11. data/lib/leptris/xml/document.rb +74 -2
  12. data/lib/leptris/xml/element.rb +21 -1
  13. data/lib/leptris/xml/entity_reference.rb +3 -0
  14. data/lib/leptris/xml/native.so +0 -0
  15. data/lib/leptris/xml/node_set.rb +22 -0
  16. data/lib/leptris/xml/processing_instruction.rb +3 -0
  17. data/lib/leptris/xml/sax/parser.rb +102 -78
  18. data/lib/leptris/xml/text.rb +4 -0
  19. data/lib/leptris/xml.rb +14 -0
  20. data/lib/libleptris.so +0 -0
  21. data/scripts/gem_smoke.rb +6 -3
  22. data/vendor-src/README.md +1 -1
  23. data/vendor-src/libleptris/CHANGELOG.md +134 -0
  24. data/vendor-src/libleptris/CMakeLists.txt +1 -1
  25. data/vendor-src/libleptris/TODO.xslt-full/REMAINING.md +37 -18
  26. data/vendor-src/libleptris/cli/commands/validate.c +15 -2
  27. data/vendor-src/libleptris/src/include/leptris.h +60 -4
  28. data/vendor-src/libleptris/src/leptris/dom/element.c +8 -0
  29. data/vendor-src/libleptris/src/leptris/dtd/content_check.c +280 -144
  30. data/vendor-src/libleptris/src/leptris/dtd/parser.c +17 -4
  31. data/vendor-src/libleptris/src/leptris/rng/parse.c +53 -8
  32. data/vendor-src/libleptris/src/leptris/rng/validate.c +63 -21
  33. data/vendor-src/libleptris/src/leptris/xinclude/xinclude.c +304 -12
  34. data/vendor-src/libleptris/src/leptris/xpath/evaluator.c +30 -0
  35. data/vendor-src/libleptris/src/leptris/xpath/evaluator.h +11 -0
  36. data/vendor-src/libleptris/src/leptris/xpath/evaluator_axes.c +3 -3
  37. data/vendor-src/libleptris/src/leptris/xpath/evaluator_path.c +4 -20
  38. data/vendor-src/libleptris/src/leptris/xpath/parser.c +4 -1
  39. data/vendor-src/libleptris/src/leptris/xpath/vm.c +41 -11
  40. data/vendor-src/libleptris/src/leptris/xpath/xpath_compiled.c +4 -3
  41. data/vendor-src/libleptris/src/leptris/xpath/xpath_public.c +2 -1
  42. data/vendor-src/libleptris/src/leptris/xsd/compile.c +981 -38
  43. data/vendor-src/libleptris/src/leptris/xsd/content_model.c +15 -3
  44. data/vendor-src/libleptris/src/leptris/xsd/validate.c +27 -9
  45. data/vendor-src/libleptris/src/leptris/xsd/xsd_internal.h +1 -0
  46. data/vendor-src/libleptris/test/cli/test_cli.cpp +50 -14
  47. data/vendor-src/libleptris/test/dom/test_dom.cpp +27 -0
  48. data/vendor-src/libleptris/test/dtd/test_dtd_validate.cpp +80 -4
  49. data/vendor-src/libleptris/test/rng/include-cases/attr-text-choice.rng +18 -0
  50. data/vendor-src/libleptris/test/rng/include-cases/dup-inc-mid.rng +7 -0
  51. data/vendor-src/libleptris/test/rng/include-cases/dup-inc-shared.rng +5 -0
  52. data/vendor-src/libleptris/test/rng/include-cases/dup-inc-top.rng +6 -0
  53. data/vendor-src/libleptris/test/rng/test_rng.cpp +101 -0
  54. data/vendor-src/libleptris/test/xinclude/test_xinclude.cpp +261 -0
  55. data/vendor-src/libleptris/test/xpath/test_xpath.cpp +169 -0
  56. data/vendor-src/libleptris/test/xpath/test_xpath_conformance.cpp +8 -1
  57. data/vendor-src/libleptris/test/xsd/test_xsd_compile.cpp +128 -11
  58. data/vendor-src/libleptris/vcpkg.json +1 -1
  59. metadata +7 -2
@@ -107,170 +107,292 @@ static int cm_peek_token(CMLexer* lx, CMTokenType* type, char name_buf[64]) {
107
107
  return 0;
108
108
  }
109
109
 
110
- /* Forward decls for the recursive matcher. */
111
- static int match_seq(CMLexer* lx, const char** child_names, size_t* child_idx,
112
- size_t child_count, const char* model_name,
113
- char* out_msg, size_t msg_size);
110
+ /* ---- the matcher (TODO.hygiene/02 closeout) --------------------------
111
+ *
112
+ * A backtracking recursive-descent evaluator over the model with the
113
+ * child list as input stream. State = (lexer position, child index);
114
+ * every trial saves/restores both, so choices and repetitions reject
115
+ * a branch without consuming children:
116
+ *
117
+ * expr := sequence ('|' sequence)* — first match wins
118
+ * sequence := term (',' term)*
119
+ * term := ( NAME | '(' expr ')' ) occurrence?
120
+ * occurrence := '' | '?' | '*' | '+'
121
+ *
122
+ * Mixed models (`(#PCDATA | a | b)*`) are validated separately: the
123
+ * alternation names form a SET, every element child must belong to
124
+ * it, text is always allowed; `(#PCDATA)` without `*` allows no
125
+ * element children at all (XML 1.0 §3.2.2, the mixed grammar). */
126
+
127
+ typedef struct {
128
+ const char** names; /* element children only */
129
+ size_t count;
130
+ size_t idx;
131
+ CMLexer lx;
132
+ const char* model_name;
133
+ char* out_msg;
134
+ size_t msg_size;
135
+ } CMState;
136
+
137
+ static int cm_match_expr(CMState* st);
138
+
139
+ /* Match ONE instance of the group whose '(' sits at group_start.
140
+ * On success the lexer lands just past the group's ')'; on failure
141
+ * the state is restored. */
142
+ static int cm_try_group_once(CMState* st, const char* group_start) {
143
+ CMLexer save_lx = st->lx;
144
+ size_t save_idx = st->idx;
145
+
146
+ st->lx.src = group_start + 1; /* past '(' */
147
+ if (!cm_match_expr(st)) goto fail;
148
+ cm_skip_ws(&st->lx);
149
+ if (st->lx.src >= st->lx.end || *st->lx.src != ')') {
150
+ if (st->out_msg && st->msg_size)
151
+ snprintf(st->out_msg, st->msg_size,
152
+ "Content model of '%s' expected ')'",
153
+ st->model_name);
154
+ goto fail;
155
+ }
156
+ st->lx.src++;
157
+ return 1;
158
+ fail:
159
+ st->lx = save_lx;
160
+ st->idx = save_idx;
161
+ return 0;
162
+ }
163
+
164
+ /* term := ( NAME | '(' expr ')' ) occurrence? */
165
+ static int cm_match_term(CMState* st) {
166
+ cm_skip_ws(&st->lx);
167
+ int c = cm_peek(&st->lx);
168
+ if (c < 0) return 0;
169
+
170
+ if (c == '(') {
171
+ const char* group_start = st->lx.src;
172
+ /* occurrence needs the token AFTER ')': peek the balanced
173
+ * close by trying once first — the occurrence layer below
174
+ * re-enters via cm_try_group_once, so this match consumes
175
+ * exactly one instance. */
176
+ if (!cm_try_group_once(st, group_start)) return 0;
177
+ cm_skip_ws(&st->lx);
178
+ char occurrence = 1;
179
+ if (st->lx.src < st->lx.end) {
180
+ if (*st->lx.src == '?') { st->lx.src++; occurrence = '?'; }
181
+ else if (*st->lx.src == '*') { st->lx.src++; occurrence = '*'; }
182
+ else if (*st->lx.src == '+') { st->lx.src++; occurrence = '+'; }
183
+ }
184
+ if (occurrence == 1 || occurrence == '?') return 1;
185
+ /* '*' / '+': trial further instances while they match and
186
+ * make progress (a zero-width body terminates the loop) */
187
+ for (;;) {
188
+ CMLexer save_lx = st->lx;
189
+ size_t save_idx = st->idx;
190
+ if (!cm_try_group_once(st, group_start)) {
191
+ st->lx = save_lx; /* the trailing occurrence token? no —
192
+ try_group_once restores to before
193
+ the retry; keep the successful
194
+ state from before this trial */
195
+ st->lx = save_lx;
196
+ st->idx = save_idx;
197
+ return 1;
198
+ }
199
+ if (st->idx == save_idx) {
200
+ /* zero-width instance: stop, state stays at the last
201
+ * successful position (already equal) */
202
+ return 1;
203
+ }
204
+ }
205
+ }
206
+
207
+ if (c == '#') {
208
+ /* #PCDATA handled by the mixed path; a bare one here is a
209
+ * malformed model for element-only matching */
210
+ if (st->out_msg && st->msg_size)
211
+ snprintf(st->out_msg, st->msg_size,
212
+ "Content model of '%s' mixes #PCDATA with element "
213
+ "content", st->model_name);
214
+ return 0;
215
+ }
114
216
 
115
- /* Match a single particle (NAME?occurrence) against one or more
116
- * children, advancing *child_idx as needed. Returns 1 on success,
117
- * 0 on mismatch. */
118
- static int match_particle(CMLexer* lx, const char** child_names,
119
- size_t* child_idx, size_t child_count,
120
- int allow_empty, const char* model_name,
121
- char* out_msg, size_t msg_size) {
122
- CMTokenType tok;
123
217
  char name[64];
124
- if (!cm_peek_token(lx, &tok, name) || tok != CMT_NAME) {
125
- snprintf(out_msg, msg_size,
126
- "Content model of '%s' expected a name", model_name);
218
+ CMTokenType tok;
219
+ if (!cm_peek_token(&st->lx, &tok, name) || tok != CMT_NAME) {
220
+ if (st->out_msg && st->msg_size)
221
+ snprintf(st->out_msg, st->msg_size,
222
+ "Content model of '%s' expected a name",
223
+ st->model_name);
127
224
  return 0;
128
225
  }
129
- cm_skip_ws(lx);
130
- /* Consume the name token by advancing past it. */
131
- while (lx->src < lx->end && !isspace((unsigned char)*lx->src) &&
132
- *lx->src != '(' && *lx->src != ')' && *lx->src != ',' &&
133
- *lx->src != '|' && *lx->src != '?' && *lx->src != '*' &&
134
- *lx->src != '+') {
135
- lx->src++;
226
+ while (st->lx.src < st->lx.end &&
227
+ !isspace((unsigned char)*st->lx.src) &&
228
+ *st->lx.src != '(' && *st->lx.src != ')' &&
229
+ *st->lx.src != ',' && *st->lx.src != '|' &&
230
+ *st->lx.src != '?' && *st->lx.src != '*' &&
231
+ *st->lx.src != '+') {
232
+ st->lx.src++;
136
233
  }
137
- /* Optional occurrence modifier. */
138
- cm_skip_ws(lx);
139
- int occurrence = 1; /* default: exactly one */
140
- if (lx->src < lx->end) {
141
- if (*lx->src == '?') { lx->src++; occurrence = '?'; }
142
- else if (*lx->src == '*') { lx->src++; occurrence = '*'; }
143
- else if (*lx->src == '+') { lx->src++; occurrence = '+'; }
234
+ cm_skip_ws(&st->lx);
235
+ char occurrence = 1;
236
+ if (st->lx.src < st->lx.end) {
237
+ if (*st->lx.src == '?') { st->lx.src++; occurrence = '?'; }
238
+ else if (*st->lx.src == '*') { st->lx.src++; occurrence = '*'; }
239
+ else if (*st->lx.src == '+') { st->lx.src++; occurrence = '+'; }
144
240
  }
145
241
 
146
- int matched_any = 0;
147
- /* * and + consume EVERY run of matching children — a single
148
- * compare left the run's tail unconsumed and failed valid
149
- * documents like <r><item/><item/></r> against (item+). */
150
- while (*child_idx < child_count &&
151
- strcmp(child_names[*child_idx], name) == 0) {
152
- (*child_idx)++;
153
- matched_any = 1;
242
+ size_t matched = 0;
243
+ while (st->idx < st->count &&
244
+ strcmp(st->names[st->idx], name) == 0) {
245
+ st->idx++;
246
+ matched++;
154
247
  if (occurrence == 1 || occurrence == '?') break;
155
248
  }
156
-
157
249
  switch (occurrence) {
158
250
  case '?':
159
251
  case '*':
160
- return 1; /* optional / zero-or-more — match even if missing */
252
+ return 1;
161
253
  case '+':
162
- if (matched_any) return 1;
163
- snprintf(out_msg, msg_size,
164
- "Element '%s' content model requires one or more '%s'",
165
- model_name, name);
254
+ if (matched) return 1;
255
+ if (st->out_msg && st->msg_size)
256
+ snprintf(st->out_msg, st->msg_size,
257
+ "Element '%s' content model requires one or "
258
+ "more '%s'", st->model_name, name);
166
259
  return 0;
167
- default: /* exactly one */
168
- if (matched_any) return 1;
169
- if (allow_empty && *child_idx >= child_count) {
170
- /* The next child would have been missing, but the
171
- * caller is OK with empty if optional follows. */
172
- return 1;
173
- }
174
- snprintf(out_msg, msg_size,
175
- "Element '%s' content model requires '%s'",
176
- model_name, name);
260
+ default:
261
+ if (matched) return 1;
262
+ if (st->out_msg && st->msg_size)
263
+ snprintf(st->out_msg, st->msg_size,
264
+ "Element '%s' content model requires '%s'",
265
+ st->model_name, name);
177
266
  return 0;
178
267
  }
179
268
  }
180
269
 
181
- /* Match a parenthesized group: ( seq | choice )?occurrence. */
182
- static int match_group(CMLexer* lx, const char** child_names,
183
- size_t* child_idx, size_t child_count,
184
- int allow_empty, const char* model_name,
185
- char* out_msg, size_t msg_size) {
186
- /* Consume '(' */
187
- if (lx->src >= lx->end || *lx->src != '(') {
188
- snprintf(out_msg, msg_size,
189
- "Content model of '%s' expected '('", model_name);
190
- return 0;
270
+ /* sequence := term (',' term)* */
271
+ static int cm_match_sequence(CMState* st) {
272
+ if (!cm_match_term(st)) return 0;
273
+ for (;;) {
274
+ cm_skip_ws(&st->lx);
275
+ if (st->lx.src >= st->lx.end || *st->lx.src != ',') return 1;
276
+ st->lx.src++;
277
+ if (!cm_match_term(st)) return 0;
191
278
  }
192
- lx->src++;
193
- cm_skip_ws(lx);
279
+ }
194
280
 
195
- /* Parse sequence/choice. We support both via recursive descent
196
- * over particles separated by `,` or `|`. */
197
- /* First term */
198
- if (!match_particle(lx, child_names, child_idx, child_count,
199
- allow_empty, model_name, out_msg, msg_size)) {
200
- return 0;
281
+ /* Lexically walk past one unmatched alternative (balanced parens,
282
+ * stopping at the depth-0 '|' or ')'). */
283
+ static void cm_skip_alternative(CMLexer* lx) {
284
+ int depth = 0;
285
+ while (lx->src < lx->end) {
286
+ char c = *lx->src;
287
+ if (c == '(') depth++;
288
+ else if (c == ')') {
289
+ if (depth == 0) return;
290
+ depth--;
291
+ } else if (c == '|' && depth == 0) return;
292
+ lx->src++;
293
+ }
294
+ }
295
+
296
+ /* expr := sequence ('|' sequence)* — first match wins; failed
297
+ * alternatives restore the state, and after a SUCCESS the remaining
298
+ * alternatives are skipped lexically so the enclosing group's ')'
299
+ * check lands on the real close. */
300
+ static int cm_match_expr(CMState* st) {
301
+ CMLexer save_lx = st->lx;
302
+ size_t save_idx = st->idx;
303
+ if (cm_match_sequence(st)) {
304
+ for (;;) {
305
+ cm_skip_ws(&st->lx);
306
+ if (st->lx.src >= st->lx.end || *st->lx.src != '|') break;
307
+ st->lx.src++;
308
+ cm_skip_alternative(&st->lx);
309
+ }
310
+ return 1;
201
311
  }
202
- /* Following terms (sequence or choice) */
203
312
  for (;;) {
204
- cm_skip_ws(lx);
205
- if (lx->src >= lx->end) break;
206
- if (*lx->src == ')') break;
207
- if (*lx->src == ',') {
208
- lx->src++;
209
- cm_skip_ws(lx);
210
- if (!match_particle(lx, child_names, child_idx, child_count,
211
- 0, model_name, out_msg, msg_size)) {
212
- return 0;
213
- }
214
- } else if (*lx->src == '|') {
215
- /* Choice: try to match the current alternative; if it
216
- * doesn't match the current child, backtrack to before
217
- * THIS particle and try the alternative. For chained `|`,
218
- * the matcher already accepted one of the earlier alts. */
219
- lx->src++;
220
- cm_skip_ws(lx);
221
- /* Save BEFORE the current particle (we already consumed
222
- * it, so we need to track from where we entered this
223
- * iteration). Since match_particle always advances on
224
- * success even for `?`/`*`, we instead check: if the
225
- * child at the current position did NOT match the
226
- * previous particle, roll back by re-running the alt. */
227
- CMLexer save = *lx;
228
- size_t save_idx = *child_idx;
229
- int alt_ok = match_particle(lx, child_names, child_idx,
230
- child_count, allow_empty,
231
- model_name, out_msg, msg_size);
232
- if (!alt_ok) {
233
- /* Alternative didn't match — try skipping it (treat
234
- * as optional). This happens when the previous
235
- * alternative matched. */
236
- *lx = save;
237
- *child_idx = save_idx;
238
- while (lx->src < lx->end && *lx->src != ')') lx->src++;
313
+ cm_skip_ws(&st->lx);
314
+ if (st->lx.src >= st->lx.end || *st->lx.src != '|') {
315
+ st->lx = save_lx;
316
+ st->idx = save_idx;
317
+ return 0;
318
+ }
319
+ st->lx.src++;
320
+ if (cm_match_sequence(st)) {
321
+ for (;;) {
322
+ cm_skip_ws(&st->lx);
323
+ if (st->lx.src >= st->lx.end || *st->lx.src != '|') break;
324
+ st->lx.src++;
325
+ cm_skip_alternative(&st->lx);
239
326
  }
240
- } else {
241
- break;
327
+ return 1;
242
328
  }
329
+ /* this alternative failed — restore and try the next one */
243
330
  }
244
- /* Consume ')' */
245
- if (lx->src >= lx->end || *lx->src != ')') {
246
- snprintf(out_msg, msg_size,
247
- "Content model of '%s' expected ')'", model_name);
331
+ }
332
+
333
+ /* Mixed model: `(#PCDATA)` or `(#PCDATA|n1|n2...)*`. */
334
+ static int cm_match_mixed(const char* model, CMState* st) {
335
+ /* collect the allowed element names between '(' and ')' */
336
+ struct { const char* s; size_t len; } names[64];
337
+ size_t n = 0;
338
+ const char* p = strchr(model, '(');
339
+ const char* close = strrchr(model, ')');
340
+ if (!p || !close || close < p) {
341
+ if (st->out_msg && st->msg_size)
342
+ snprintf(st->out_msg, st->msg_size,
343
+ "Content model of '%s' is malformed",
344
+ st->model_name);
248
345
  return 0;
249
346
  }
250
- lx->src++;
251
- /* Optional outer-group occurrence modifier. */
252
- cm_skip_ws(lx);
253
- if (lx->src < lx->end) {
254
- if (*lx->src == '?') { lx->src++; return 1; } /* outer (a|b)? — allowed */
255
- if (*lx->src == '*') { lx->src++; return 1; } /* outer (a|b)* */
256
- if (*lx->src == '+') { lx->src++; return 1; } /* outer (a|b)+ — at least one alt */
347
+ int star = 0;
348
+ {
349
+ const char* q = close + 1;
350
+ while (*q && isspace((unsigned char)*q)) q++;
351
+ star = (*q == '*');
257
352
  }
258
- return 1;
259
- }
260
-
261
- /* Top-level: parse the model and match against children. */
262
- static int match_seq(CMLexer* lx, const char** child_names, size_t* child_idx,
263
- size_t child_count, const char* model_name,
264
- char* out_msg, size_t msg_size) {
265
- /* If model starts with '(', parse a group. */
266
- cm_skip_ws(lx);
267
- if (lx->src < lx->end && *lx->src == '(') {
268
- return match_group(lx, child_names, child_idx, child_count,
269
- 0, model_name, out_msg, msg_size);
353
+ p++;
354
+ while (p < close) {
355
+ while (p < close && (isspace((unsigned char)*p) || *p == '|')) p++;
356
+ if (p >= close) break;
357
+ const char* s = p;
358
+ while (p < close && *p != '|' && !isspace((unsigned char)*p)) p++;
359
+ size_t len = (size_t)(p - s);
360
+ if (len == 7 && strncmp(s, "#PCDATA", 7) == 0) continue;
361
+ if (n < 64) {
362
+ /* borrowed (ptr,len): model text is not NUL-terminated
363
+ * at the name boundary */
364
+ names[n].s = s;
365
+ names[n].len = len;
366
+ n++;
367
+ }
368
+ }
369
+ for (size_t i = 0; i < st->count; i++) {
370
+ int allowed = 0;
371
+ for (size_t j = 0; j < n; j++) {
372
+ if (strlen(st->names[i]) == names[j].len &&
373
+ strncmp(st->names[i], names[j].s, names[j].len) == 0) {
374
+ allowed = 1;
375
+ break;
376
+ }
377
+ }
378
+ if (!allowed) {
379
+ if (st->out_msg && st->msg_size)
380
+ snprintf(st->out_msg, st->msg_size,
381
+ "Element '%s' child '%s' is not allowed by the "
382
+ "mixed content model", st->model_name,
383
+ st->names[i]);
384
+ return 0;
385
+ }
270
386
  }
271
- /* Single particle (NAME?occurrence). */
272
- return match_particle(lx, child_names, child_idx, child_count,
273
- 0, model_name, out_msg, msg_size);
387
+ if (!star && st->count > 0) {
388
+ if (st->out_msg && st->msg_size)
389
+ snprintf(st->out_msg, st->msg_size,
390
+ "Element '%s' mixed content model allows no element "
391
+ "children", st->model_name);
392
+ return 0;
393
+ }
394
+ st->idx = st->count;
395
+ return 1;
274
396
  }
275
397
 
276
398
  /* Public entry point: match actual children against a content model
@@ -282,17 +404,31 @@ int leptris_content_model_match(const char* model, const char* elem_name,
282
404
  if (out_msg && msg_size) snprintf(out_msg, msg_size, "NULL model or children");
283
405
  return 0;
284
406
  }
285
- CMLexer lx = { .src = model, .end = model + strlen(model) };
286
- size_t child_idx = 0;
287
- int rc = match_seq(&lx, child_names, &child_idx, child_count,
288
- elem_name, out_msg, msg_size);
289
- if (rc != 1) return 0;
290
- /* All children must be consumed. */
291
- if (child_idx != child_count) {
407
+ CMState st = {0};
408
+ st.names = child_names;
409
+ st.count = child_count;
410
+ st.idx = 0;
411
+ st.lx = (CMLexer){ .src = model, .end = model + strlen(model) };
412
+ st.model_name = elem_name;
413
+ st.out_msg = out_msg;
414
+ st.msg_size = msg_size;
415
+
416
+ if (strstr(model, "#PCDATA")) return cm_match_mixed(model, &st);
417
+
418
+ if (!cm_match_expr(&st)) {
419
+ if (out_msg && msg_size && out_msg[0] == 0) {
420
+ snprintf(out_msg, msg_size,
421
+ "Element '%s' does not match its content model",
422
+ elem_name);
423
+ }
424
+ return 0;
425
+ }
426
+ if (st.idx != st.count) {
292
427
  if (out_msg && msg_size) {
293
428
  snprintf(out_msg, msg_size,
294
- "Element '%s' has %zu child(ren) but content model accepts %zu",
295
- elem_name, child_count, child_idx);
429
+ "Element '%s' has %zu child(ren) but the content "
430
+ "model accepts %zu",
431
+ elem_name, child_count, st.idx);
296
432
  }
297
433
  return 0;
298
434
  }
@@ -251,7 +251,6 @@ static DTDElementDecl* dtd_parse_element(DTDParser* p) {
251
251
  depth--;
252
252
  dtd_advance(p);
253
253
  if (depth == 0) { closed = 1; break; }
254
- dtd_advance(p);
255
254
  continue;
256
255
  }
257
256
  dtd_advance(p);
@@ -260,6 +259,15 @@ static DTDElementDecl* dtd_parse_element(DTDParser* p) {
260
259
  * malformed declaration — the NULL-on-error contract. */
261
260
  if (!closed) return NULL;
262
261
 
262
+ /* The occurrence modifier rides the group: `(a|b)*` stored
263
+ * WITHOUT the * made every mixed/repeated model validate as
264
+ * required-once (#1709-follow-up probe). */
265
+ if (!dtd_at_end(p) &&
266
+ (dtd_peek(p) == '*' || dtd_peek(p) == '+' ||
267
+ dtd_peek(p) == '?')) {
268
+ dtd_advance(p);
269
+ }
270
+
263
271
  size_t len = p->pos - start;
264
272
  elem->content_model = (char*)leptris_pool_alloc(p->pool, len + 1);
265
273
  if (elem->content_model) {
@@ -583,9 +591,14 @@ static void dtd_parse_into(LeptrisDTD* dtd, const char* dtd_content,
583
591
  * content models, ATTLIST lists — legal in external subsets,
584
592
  * accepted leniently everywhere). ENTITY declarations are
585
593
  * excluded: their VALUES may legitimately contain '%'. */
586
- if (dtd_match(&parser, "<!ELEMENT") ||
587
- dtd_match(&parser, "<!ATTLIST") ||
588
- dtd_match(&parser, "<!NOTATION")) {
594
+ /* PEEK (do not consume): the substitution path re-parses the
595
+ * FULL declaration text — capturing decl_start after dtd_match
596
+ * would cut off the keyword and the reparse would register a
597
+ * garbage model (mixed models via %pe; validated as if
598
+ * #PCDATA were required). */
599
+ if (strncmp(parser.pos, "<!ELEMENT", 9) == 0 ||
600
+ strncmp(parser.pos, "<!ATTLIST", 9) == 0 ||
601
+ strncmp(parser.pos, "<!NOTATION", 10) == 0) {
589
602
  const char* decl_start = parser.pos;
590
603
  const char* scan = parser.pos;
591
604
  while (scan < parser.end && *scan != '>') scan++;
@@ -19,12 +19,17 @@
19
19
  * carry backslashes; strrchr('/') alone left the filename in the
20
20
  * "directory" and every relative href resolved against a bogus
21
21
  * base). Returns s. */
22
- static char* rng_dir_of(char* s) {
22
+ /* Cut the last path segment off `s`. Returns 0 when s carries no
23
+ * separator (a bare relative filename) — callers must then use "."
24
+ * as the base, or the FILENAME becomes the include directory and
25
+ * every relative include of the schema fails (#1709 follow-up). */
26
+ static int rng_dir_of(char* s) {
23
27
  char* slash = strrchr(s, '/');
24
28
  char* bslash = strrchr(s, '\\');
25
29
  if (bslash && (!slash || bslash > slash)) slash = bslash;
26
- if (slash) *slash = 0;
27
- return s;
30
+ if (!slash) return 0;
31
+ *slash = 0;
32
+ return 1;
28
33
  }
29
34
 
30
35
  static RngPattern* pat_new(RngPatternKind kind) {
@@ -255,6 +260,35 @@ static void merge_start(RngGrammar* g, RngPattern* body) {
255
260
  g->start = ch;
256
261
  }
257
262
 
263
+ /* Deep-copy a pattern subtree. The include merge used to MOVE the
264
+ * included grammar's body into the including grammar — but an
265
+ * include reachable through TWO paths (relaton's basicdoc: direct
266
+ * AND via biblio) leaves both grammars' define entries pointing at
267
+ * ONE shared tree; the second path's merge then mutates/frees nodes
268
+ * the first path still walks (leptris#1709). Name-based ref
269
+ * resolution (find_define at validate time) makes the copy safe —
270
+ * REF patterns carry only their name. */
271
+ static char* rng_strdup_or_null(const char* s) {
272
+ return s ? leptris_strdup(s) : NULL;
273
+ }
274
+ static RngPattern* rng_pattern_deep_copy(const RngPattern* p) {
275
+ if (!p) return NULL;
276
+ RngPattern* c = pat_new(p->kind);
277
+ c->first_child = rng_pattern_deep_copy(p->first_child);
278
+ c->next = rng_pattern_deep_copy(p->next);
279
+ c->name = rng_strdup_or_null(p->name);
280
+ c->ns = rng_strdup_or_null(p->ns);
281
+ c->datatype = rng_strdup_or_null(p->datatype);
282
+ c->datatype_lib = rng_strdup_or_null(p->datatype_lib);
283
+ c->value = rng_strdup_or_null(p->value);
284
+ /* define: name-based resolution at validate time relinks; the
285
+ * copied subtree must not share the source grammar's (freed)
286
+ * define entries. */
287
+ c->define = NULL;
288
+ c->any_name = p->any_name;
289
+ return c;
290
+ }
291
+
258
292
  /* INTERLEAVE and CHOICE are associative: a merge wrapper whose
259
293
  * child list already carries a same-kind combinator flattens into
260
294
  * the parent scope. The validator's single-pass interleave walk has
@@ -399,7 +433,7 @@ static int handle_include(RngGrammar* g, LeptrisElement inc,
399
433
  }
400
434
  char nbase[1024];
401
435
  snprintf(nbase, sizeof(nbase), "%s", path);
402
- rng_dir_of(nbase);
436
+ if (!rng_dir_of(nbase)) snprintf(nbase, sizeof(nbase), ".");
403
437
 
404
438
  char ierr[256];
405
439
  ierr[0] = 0;
@@ -516,7 +550,16 @@ static int handle_include(RngGrammar* g, LeptrisElement inc,
516
550
  while (d) {
517
551
  RngDefine* next = d->next;
518
552
  RngPattern* wrap = pat_new(RNG_GROUP);
519
- wrap->first_child = d->body;
553
+ /* COPY, not move: the included grammar may be reachable
554
+ * through another include path whose defines still walk
555
+ * this tree (leptris#1709, adoption-by-copy). The original
556
+ * tree is solely owned by THIS include-parse instance —
557
+ * free it, or every include hop leaks its define bodies
558
+ * (Linux-ASAN/LSan: 80-byte RngPattern trees per merged
559
+ * define). */
560
+ wrap->first_child = rng_pattern_deep_copy(d->body);
561
+ rng_pattern_free(d->body);
562
+ d->body = NULL;
520
563
  if (!add_define(g, leptris_strdup(d->name),
521
564
  d->combine ? leptris_strdup(d->combine) : NULL,
522
565
  wrap, err, errsz)) {
@@ -525,6 +568,7 @@ static int handle_include(RngGrammar* g, LeptrisElement inc,
525
568
  free(d);
526
569
  while (next) {
527
570
  RngDefine* n2 = next->next;
571
+ rng_pattern_free(next->body);
528
572
  free(next->name);
529
573
  free(next->combine);
530
574
  free(next);
@@ -646,7 +690,7 @@ static int resolve_external_refs(RngGrammar* g, RngPattern** slot,
646
690
  }
647
691
  char nbase[1024];
648
692
  snprintf(nbase, sizeof(nbase), "%s", path);
649
- rng_dir_of(nbase);
693
+ if (!rng_dir_of(nbase)) snprintf(nbase, sizeof(nbase), ".");
650
694
  struct leptris_relaxng* ex = rng_parse_doc(edoc, nbase[0] ? nbase : NULL);
651
695
  leptris_document_free(edoc);
652
696
  if (!ex || !ex->grammar || ex->error) {
@@ -861,10 +905,11 @@ struct leptris_relaxng* rng_parse_file(const char* path) {
861
905
  return NULL;
862
906
  }
863
907
 
864
- /* Include hrefs resolve relative to the schema file's directory. */
908
+ /* Include hrefs resolve relative to the schema file's directory;
909
+ * a bare relative filename resolves its includes against "." */
865
910
  char base[512];
866
911
  snprintf(base, sizeof(base), "%s", path);
867
- rng_dir_of(base);
912
+ if (!rng_dir_of(base)) snprintf(base, sizeof(base), ".");
868
913
 
869
914
  struct leptris_relaxng* rng =
870
915
  rng_parse_doc(doc, base[0] ? base : NULL);