nokogiri 1.19.3-java → 1.19.5-java

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 (46) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE-DEPENDENCIES.md +29 -0
  3. data/ext/java/nokogiri/XmlAttr.java +3 -0
  4. data/ext/java/nokogiri/XmlDocument.java +5 -1
  5. data/ext/java/nokogiri/XmlNode.java +69 -38
  6. data/ext/java/nokogiri/XmlNodeSet.java +19 -25
  7. data/ext/java/nokogiri/XmlReader.java +4 -1
  8. data/ext/java/nokogiri/XmlSchema.java +22 -9
  9. data/ext/java/nokogiri/XmlXpathContext.java +5 -15
  10. data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +5 -7
  11. data/ext/java/nokogiri/internals/NokogiriHelpers.java +68 -0
  12. data/ext/java/nokogiri/internals/NokogiriXPathContext.java +37 -0
  13. data/ext/nokogiri/extconf.rb +1 -0
  14. data/ext/nokogiri/gumbo.c +83 -6
  15. data/ext/nokogiri/html4_document.c +10 -3
  16. data/ext/nokogiri/libxml2_polyfill.c +12 -0
  17. data/ext/nokogiri/nokogiri.c +90 -5
  18. data/ext/nokogiri/xml_attr.c +28 -23
  19. data/ext/nokogiri/xml_document.c +64 -30
  20. data/ext/nokogiri/xml_node.c +151 -52
  21. data/ext/nokogiri/xml_node_set.c +10 -10
  22. data/ext/nokogiri/xml_reader.c +198 -107
  23. data/ext/nokogiri/xml_sax_parser.c +3 -3
  24. data/ext/nokogiri/xml_sax_parser_context.c +38 -7
  25. data/ext/nokogiri/xml_sax_push_parser.c +12 -0
  26. data/ext/nokogiri/xml_xpath_context.c +135 -27
  27. data/ext/nokogiri/xslt_stylesheet.c +123 -27
  28. data/lib/nokogiri/css/parser.rb +35 -18
  29. data/lib/nokogiri/css/parser.y +35 -17
  30. data/lib/nokogiri/css/xpath_visitor.rb +71 -28
  31. data/lib/nokogiri/html5/document.rb +9 -0
  32. data/lib/nokogiri/html5/document_fragment.rb +16 -1
  33. data/lib/nokogiri/html5/node.rb +1 -1
  34. data/lib/nokogiri/html5.rb +16 -0
  35. data/lib/nokogiri/nokogiri.jar +0 -0
  36. data/lib/nokogiri/version/constant.rb +1 -1
  37. data/lib/nokogiri/version/info.rb +10 -0
  38. data/lib/nokogiri/xml/attr.rb +2 -0
  39. data/lib/nokogiri/xml/attribute_decl.rb +2 -1
  40. data/lib/nokogiri/xml/builder.rb +24 -32
  41. data/lib/nokogiri/xml/document.rb +4 -2
  42. data/lib/nokogiri/xml/dtd.rb +2 -1
  43. data/lib/nokogiri/xml/element_decl.rb +2 -1
  44. data/lib/nokogiri/xml/entity_decl.rb +2 -1
  45. data/lib/nokogiri/xml/node.rb +65 -12
  46. metadata +3 -2
data/ext/nokogiri/gumbo.c CHANGED
@@ -113,6 +113,71 @@ set_line(xmlNodePtr node, size_t line)
113
113
  }
114
114
  }
115
115
 
116
+ // This function is essentially xmlNewNsProp, but we skip the full list traversal to append by
117
+ // providing the last property in the linked list as a parameter.
118
+ static xmlAttrPtr
119
+ append_property(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, const xmlChar *value, xmlAttrPtr last_prop)
120
+ {
121
+ xmlAttrPtr cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
122
+ if (cur == NULL) {
123
+ return NULL;
124
+ }
125
+ memset(cur, 0, sizeof(xmlAttr));
126
+ cur->type = XML_ATTRIBUTE_NODE;
127
+ cur->parent = node;
128
+ xmlDocPtr doc = node->doc;
129
+ cur->doc = doc;
130
+ cur->ns = ns;
131
+
132
+ if ((doc != NULL) && (doc->dict != NULL)) {
133
+ cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
134
+ } else {
135
+ cur->name = xmlStrdup(name);
136
+ }
137
+ if (cur->name == NULL) {
138
+ goto error;
139
+ }
140
+
141
+ if (value != NULL) {
142
+ cur->children = xmlNewDocText(doc, value);
143
+ if (cur->children == NULL) {
144
+ goto error;
145
+ }
146
+ cur->last = NULL;
147
+ xmlNodePtr tmp = cur->children;
148
+ while (tmp != NULL) {
149
+ tmp->parent = (xmlNodePtr) cur;
150
+ if (tmp->next == NULL) {
151
+ cur->last = tmp;
152
+ }
153
+ tmp = tmp->next;
154
+ }
155
+
156
+ if (doc != NULL) {
157
+ int res = xmlIsID(doc, node, cur);
158
+ if (res < 0) {
159
+ goto error;
160
+ }
161
+ if ((res == 1) && (xmlAddIDSafe(cur, value) < 0)) {
162
+ goto error;
163
+ }
164
+ }
165
+ }
166
+
167
+ if (node->properties == NULL) {
168
+ node->properties = cur;
169
+ } else {
170
+ last_prop->next = cur;
171
+ cur->prev = last_prop;
172
+ }
173
+
174
+ return cur;
175
+
176
+ error:
177
+ xmlFreeProp(cur);
178
+ return (NULL);
179
+ }
180
+
116
181
  // Construct an XML tree rooted at xml_output_node from the Gumbo tree rooted
117
182
  // at gumbo_node.
118
183
  static void
@@ -200,6 +265,7 @@ build_tree(
200
265
  xmlAddChild(xml_node, xml_child);
201
266
 
202
267
  // Add the attributes.
268
+ xmlAttrPtr last_prop = NULL;
203
269
  const GumboVector *attrs = &gumbo_child->v.element.attributes;
204
270
  for (size_t i = 0; i < attrs->length; i++) {
205
271
  const GumboAttribute *attr = attrs->data[i];
@@ -220,7 +286,9 @@ build_tree(
220
286
  default:
221
287
  ns = NULL;
222
288
  }
223
- xmlNewNsProp(xml_child, ns, (const xmlChar *)attr->name, (const xmlChar *)attr->value);
289
+
290
+ // We micromanage the attribute list for performance reasons.
291
+ last_prop = append_property(xml_child, ns, (const xmlChar *)attr->name, (const xmlChar *)attr->value, last_prop);
224
292
  }
225
293
 
226
294
  // Add children for this element.
@@ -373,12 +441,20 @@ parse_continue(VALUE parse_args)
373
441
  } else {
374
442
  doc = new_html_doc(NULL, NULL, NULL);
375
443
  }
376
- args->doc = doc; // Make sure doc gets cleaned up if an error is thrown.
444
+
445
+ // We're about to build the libxml2 tree structure from the Gumbo tree
446
+ // structure. If an exception is raised during tree building, control passes
447
+ // to `parse_cleanup()` which needs to free the newly allocated `xmlDoc`,
448
+ // `doc`. If the tree is successfully constructed, then
449
+ // `noko_xml_document_wrap()` will create a new Ruby object which will own
450
+ // `doc`. In that case, `parse_cleanup()` should not free `doc`.
451
+ args->doc = doc;
377
452
  build_tree(doc, (xmlNodePtr)doc, output->document);
453
+ args->doc = NULL;
378
454
  VALUE rdoc = noko_xml_document_wrap(args->klass, doc);
455
+
379
456
  rb_iv_set(rdoc, "@url", args->url_or_frag);
380
457
  rb_iv_set(rdoc, "@quirks_mode", INT2NUM(output->document->v.document.doc_type_quirks_mode));
381
- args->doc = NULL; // The Ruby runtime now owns doc so don't delete it.
382
458
  add_errors(output, rdoc, args->input, args->url_or_frag);
383
459
  return rdoc;
384
460
  }
@@ -512,6 +588,7 @@ error:
512
588
  continue;
513
589
  }
514
590
  VALUE element_name = rb_funcall(node, name, 0);
591
+ Check_Type(element_name, T_STRING);
515
592
  if (RSTRING_LEN(element_name) == 4
516
593
  && !st_strcasecmp(RSTRING_PTR(element_name), "form")
517
594
  && lookup_namespace(node, false) == GUMBO_NAMESPACE_HTML) {
@@ -568,7 +645,7 @@ error:
568
645
  .output = output,
569
646
  .input = tags,
570
647
  .url_or_frag = doc_fragment,
571
- .doc = (xmlDocPtr)extract_xml_node(doc),
648
+ .doc = NULL,
572
649
  };
573
650
  rb_ensure(fragment_continue, (VALUE)(&args), parse_cleanup, (VALUE)(&args));
574
651
  return Qnil;
@@ -580,9 +657,9 @@ fragment_continue(VALUE parse_args)
580
657
  ParseArgs *args = (ParseArgs *)parse_args;
581
658
  GumboOutput *output = args->output;
582
659
  VALUE doc_fragment = args->url_or_frag;
583
- xmlDocPtr xml_doc = args->doc;
660
+ VALUE doc = rb_funcall(doc_fragment, rb_intern_const("document"), 0);
661
+ xmlDocPtr xml_doc = (xmlDocPtr)extract_xml_node(doc);
584
662
 
585
- args->doc = NULL; // The Ruby runtime owns doc so make sure we don't delete it.
586
663
  xmlNodePtr xml_frag = extract_xml_node(doc_fragment);
587
664
  build_tree(xml_doc, xml_frag, output->root);
588
665
  rb_iv_set(doc_fragment, "@quirks_mode", INT2NUM(output->document->v.document.doc_type_quirks_mode));
@@ -18,8 +18,8 @@ rb_html_document_s_new(int argc, VALUE *argv, VALUE klass)
18
18
  htmlDocPtr doc;
19
19
 
20
20
  rb_scan_args(argc, argv, "0*", &rest);
21
- uri = rb_ary_entry(rest, (long)0);
22
- external_id = rb_ary_entry(rest, (long)1);
21
+ uri = rb_ary_entry(rest, (rb_len_t)0);
22
+ external_id = rb_ary_entry(rest, (rb_len_t)1);
23
23
 
24
24
  doc = htmlNewDoc(
25
25
  RTEST(uri) ? (const xmlChar *)StringValueCStr(uri) : NULL,
@@ -48,10 +48,17 @@ rb_html_document_s_read_io(VALUE klass, VALUE rb_io, VALUE rb_url, VALUE rb_enco
48
48
 
49
49
  xmlSetStructuredErrorFunc((void *)rb_error_list, noko__error_array_pusher);
50
50
 
51
- c_doc = htmlReadIO(noko_io_read, noko_io_close, (void *)rb_io, c_url, c_encoding, options);
51
+ noko_io_read_reset_exception();
52
+ c_doc = htmlReadIO(noko_io_read_protected, noko_io_close, (void *)rb_io, c_url, c_encoding, options);
53
+ VALUE rb_io_read_exception = noko_io_read_take_exception();
52
54
 
53
55
  xmlSetStructuredErrorFunc(NULL, NULL);
54
56
 
57
+ if (!NIL_P(rb_io_read_exception)) {
58
+ xmlFreeDoc(c_doc);
59
+ noko_io_read_raise_exception(rb_io_read_exception);
60
+ }
61
+
55
62
  /*
56
63
  * If EncodingFound has occurred in EncodingReader, make sure to do
57
64
  * a cleanup and propagate the error.
@@ -112,3 +112,15 @@ xmlSwitchEncodingName(xmlParserCtxtPtr ctxt, const char *encoding)
112
112
  return (xmlSwitchToEncoding(ctxt, handler));
113
113
  }
114
114
  #endif
115
+
116
+ #ifndef HAVE_XMLADDIDSAFE
117
+ int
118
+ xmlAddIDSafe(xmlAttrPtr attr, const xmlChar *value)
119
+ {
120
+ xmlIDPtr id = xmlAddID(NULL, attr->doc, value, attr);
121
+ if (id) {
122
+ return 1;
123
+ }
124
+ return 0;
125
+ }
126
+ #endif
@@ -50,7 +50,7 @@ void noko_init_html4_sax_parser(void);
50
50
  void noko_init_gumbo(void);
51
51
  void noko_init_test_global_handlers(void);
52
52
 
53
- static ID id_read, id_write, id_external_encoding;
53
+ static ID id_read, id_write, id_external_encoding, id_io_read_exception;
54
54
 
55
55
 
56
56
  static VALUE
@@ -92,6 +92,87 @@ noko_io_read(void *io, char *c_buffer, int c_buffer_len)
92
92
  }
93
93
 
94
94
 
95
+ typedef struct {
96
+ void *io;
97
+ char *c_buffer;
98
+ int c_buffer_len;
99
+ int n_bytes_read;
100
+ } noko_io_read_args_t;
101
+
102
+ static VALUE
103
+ noko_io_read_call(VALUE data)
104
+ {
105
+ noko_io_read_args_t *args = (noko_io_read_args_t *)data;
106
+ args->n_bytes_read = noko_io_read(args->io, args->c_buffer, args->c_buffer_len);
107
+ return Qnil;
108
+ }
109
+
110
+
111
+ /*
112
+ * Like noko_io_read, a StandardError raised by IO#read is swallowed, and libxml2 sees a read
113
+ * error. Any other exception (e.g., Interrupt or Timeout::ExitException) must not unwind through
114
+ * libxml2's stack frames, because that skips libxml2's cleanup and leaks memory, and skips the
115
+ * reset of the error handlers. So it is saved in a fiber-local variable, and $! is cleared until
116
+ * it is re-raised.
117
+ *
118
+ * A caller of libxml2 that passes this callback must call noko_io_read_reset_exception before
119
+ * calling libxml2, and noko_io_read_take_exception after libxml2 returns, and must re-raise a
120
+ * non-nil result with noko_io_read_raise_exception.
121
+ */
122
+ int
123
+ noko_io_read_protected(void *io, char *c_buffer, int c_buffer_len)
124
+ {
125
+ noko_io_read_args_t args = { io, c_buffer, c_buffer_len, -1 };
126
+ int state = 0;
127
+
128
+ rb_protect(noko_io_read_call, (VALUE)&args, &state);
129
+
130
+ if (state) {
131
+ VALUE rb_errinfo_value = rb_errinfo();
132
+ if (RB_TYPE_P(rb_errinfo_value, T_OBJECT) && rb_obj_is_kind_of(rb_errinfo_value, rb_eException)) {
133
+ rb_set_errinfo(Qnil);
134
+ } else {
135
+ rb_errinfo_value = Qnil;
136
+ }
137
+ rb_thread_local_aset(rb_thread_current(), id_io_read_exception,
138
+ rb_assoc_new(INT2FIX(state), rb_errinfo_value));
139
+ return -1;
140
+ }
141
+
142
+ return args.n_bytes_read;
143
+ }
144
+
145
+
146
+ void
147
+ noko_io_read_reset_exception(void)
148
+ {
149
+ rb_thread_local_aset(rb_thread_current(), id_io_read_exception, Qnil);
150
+ }
151
+
152
+
153
+ VALUE
154
+ noko_io_read_take_exception(void)
155
+ {
156
+ VALUE rb_thread = rb_thread_current();
157
+ VALUE rb_exception = rb_thread_local_aref(rb_thread, id_io_read_exception);
158
+
159
+ rb_thread_local_aset(rb_thread, id_io_read_exception, Qnil);
160
+ return rb_exception;
161
+ }
162
+
163
+
164
+ void
165
+ noko_io_read_raise_exception(VALUE rb_exception)
166
+ {
167
+ VALUE rb_errinfo_value = rb_ary_entry(rb_exception, 1);
168
+
169
+ if (!NIL_P(rb_errinfo_value)) {
170
+ rb_set_errinfo(rb_errinfo_value);
171
+ }
172
+ rb_jump_tag(FIX2INT(rb_ary_entry(rb_exception, 0)));
173
+ }
174
+
175
+
95
176
  static VALUE
96
177
  noko_io_write_check(VALUE rb_args)
97
178
  {
@@ -122,7 +203,7 @@ noko_io_write(void *io, char *c_buffer, int c_buffer_len)
122
203
  io_encoding = RB_NIL_P(rb_enc) ? rb_ascii8bit_encoding() : rb_to_encoding(rb_enc);
123
204
 
124
205
  rb_args[0] = rb_io;
125
- rb_args[1] = rb_enc_str_new(c_buffer, (long)c_buffer_len, io_encoding);
206
+ rb_args[1] = rb_enc_str_new(c_buffer, (rb_len_t)c_buffer_len, io_encoding);
126
207
 
127
208
  rb_n_bytes_written = rb_rescue(noko_io_write_check, (VALUE)rb_args, noko_io_write_failed, 0);
128
209
  if (rb_n_bytes_written == Qundef) { return -1; }
@@ -203,9 +284,6 @@ Init_nokogiri(void)
203
284
  rb_const_set(mNokogiri, rb_intern("LIBXSLT_COMPILED_VERSION"), NOKOGIRI_STR_NEW2(LIBXSLT_DOTTED_VERSION));
204
285
  rb_const_set(mNokogiri, rb_intern("LIBXSLT_LOADED_VERSION"), NOKOGIRI_STR_NEW2(xsltEngineVersion));
205
286
 
206
- rb_const_set(mNokogiri, rb_intern("LIBXML_ZLIB_ENABLED"),
207
- xmlHasFeature(XML_WITH_ZLIB) == 1 ? Qtrue : Qfalse);
208
-
209
287
  #ifdef NOKOGIRI_PACKAGED_LIBRARIES
210
288
  rb_const_set(mNokogiri, rb_intern("PACKAGED_LIBRARIES"), Qtrue);
211
289
  # ifdef NOKOGIRI_PRECOMPILED_LIBRARIES
@@ -228,6 +306,12 @@ Init_nokogiri(void)
228
306
  rb_const_set(mNokogiri, rb_intern("LIBXML_ICONV_ENABLED"), Qfalse);
229
307
  #endif
230
308
 
309
+ rb_const_set(mNokogiri, rb_intern("LIBXML_ZLIB_ENABLED"),
310
+ xmlHasFeature(XML_WITH_ZLIB) == 1 ? Qtrue : Qfalse);
311
+
312
+ rb_const_set(mNokogiri, rb_intern("LIBXML_HTTP_ENABLED"),
313
+ xmlHasFeature(XML_WITH_HTTP) == 1 ? Qtrue : Qfalse);
314
+
231
315
  #ifdef NOKOGIRI_OTHER_LIBRARY_VERSIONS
232
316
  rb_const_set(mNokogiri, rb_intern("OTHER_LIBRARY_VERSIONS"), NOKOGIRI_STR_NEW2(NOKOGIRI_OTHER_LIBRARY_VERSIONS));
233
317
  #endif
@@ -289,6 +373,7 @@ Init_nokogiri(void)
289
373
  noko_init_test_global_handlers();
290
374
 
291
375
  id_read = rb_intern("read");
376
+ id_io_read_exception = rb_intern("__nokogiri_io_read_exception");
292
377
  id_write = rb_intern("write");
293
378
  id_external_encoding = rb_intern("external_encoding");
294
379
  }
@@ -10,37 +10,42 @@ VALUE cNokogiriXmlAttr;
10
10
  * (e.g., a HTML boolean attribute).
11
11
  */
12
12
  static VALUE
13
- set_value(VALUE self, VALUE content)
13
+ noko_xml_attr_set_value(VALUE self, VALUE content)
14
14
  {
15
15
  xmlAttrPtr attr;
16
- xmlChar *value;
17
- xmlNode *cur;
18
16
 
19
17
  Noko_Node_Get_Struct(self, xmlAttr, attr);
20
18
 
21
- if (attr->children) {
22
- xmlFreeNodeList(attr->children);
19
+ {
20
+ /* Unlink and pin any wrapped children */
21
+ xmlNode *cur = attr->children;
22
+ xmlNode *next;
23
+
24
+ while (cur) {
25
+ next = cur->next;
26
+ if (cur->_private) {
27
+ xmlUnlinkNode(cur);
28
+ noko_xml_document_pin_node(cur);
29
+ }
30
+ cur = next;
31
+ }
23
32
  }
24
- attr->children = attr->last = NULL;
25
33
 
26
34
  if (content == Qnil) {
27
- return content;
28
- }
29
-
30
- value = xmlEncodeEntitiesReentrant(attr->doc, (unsigned char *)StringValueCStr(content));
31
- if (xmlStrlen(value) == 0) {
32
- attr->children = xmlNewDocText(attr->doc, value);
35
+ xmlNodeSetContent((xmlNodePtr)attr, NULL); /* Clear any remaining unwrapped children. */
33
36
  } else {
34
- attr->children = xmlStringGetNodeList(attr->doc, value);
35
- }
36
- xmlFree(value);
37
+ xmlChar *value = xmlEncodeEntitiesReentrant(attr->doc, (unsigned char *)StringValueCStr(content));
38
+
39
+ if (xmlStrlen(value) == 0) {
40
+ xmlNodeSetContent((xmlNodePtr)attr, NULL); /* Clear any remaining unwrapped children. */
37
41
 
38
- for (cur = attr->children; cur; cur = cur->next) {
39
- cur->parent = (xmlNode *)attr;
40
- cur->doc = attr->doc;
41
- if (cur->next == NULL) {
42
- attr->last = cur;
42
+ /* Preserve empty-string attributes as `foo=""` and not boolean `foo` */
43
+ attr->children = attr->last = xmlNewDocText(attr->doc, value);
44
+ attr->children->parent = (xmlNode *)attr;
45
+ } else {
46
+ xmlNodeSetContent((xmlNodePtr)attr, value);
43
47
  }
48
+ xmlFree(value);
44
49
  }
45
50
 
46
51
  return content;
@@ -53,7 +58,7 @@ set_value(VALUE self, VALUE content)
53
58
  * Create a new Attr element on the +document+ with +name+
54
59
  */
55
60
  static VALUE
56
- new (int argc, VALUE *argv, VALUE klass)
61
+ noko_xml_attr__new(int argc, VALUE *argv, VALUE klass)
57
62
  {
58
63
  xmlDocPtr xml_doc;
59
64
  VALUE document;
@@ -97,7 +102,7 @@ noko_init_xml_attr(void)
97
102
  */
98
103
  cNokogiriXmlAttr = rb_define_class_under(mNokogiriXml, "Attr", cNokogiriXmlNode);
99
104
 
100
- rb_define_singleton_method(cNokogiriXmlAttr, "new", new, -1);
105
+ rb_define_singleton_method(cNokogiriXmlAttr, "new", noko_xml_attr__new, -1);
101
106
 
102
- rb_define_method(cNokogiriXmlAttr, "value=", set_value, 1);
107
+ rb_define_method(cNokogiriXmlAttr, "value=", noko_xml_attr_set_value, 1);
103
108
  }
@@ -255,12 +255,6 @@ rb_xml_document_root_set(VALUE self, VALUE rb_new_root)
255
255
 
256
256
  c_document = noko_xml_document_unwrap(self);
257
257
 
258
- c_current_root = xmlDocGetRootElement(c_document);
259
- if (c_current_root) {
260
- xmlUnlinkNode(c_current_root);
261
- noko_xml_document_pin_node(c_current_root);
262
- }
263
-
264
258
  if (!NIL_P(rb_new_root)) {
265
259
  if (!rb_obj_is_kind_of(rb_new_root, cNokogiriXmlNode)) {
266
260
  rb_raise(rb_eArgError,
@@ -270,13 +264,23 @@ rb_xml_document_root_set(VALUE self, VALUE rb_new_root)
270
264
 
271
265
  Noko_Node_Get_Struct(rb_new_root, xmlNode, c_new_root);
272
266
 
273
- /* If the new root's document is not the same as the current document,
274
- * then we need to dup the node in to this document. */
275
- if (c_new_root->doc != c_document) {
276
- c_new_root = xmlDocCopyNode(c_new_root, c_document, 1);
277
- if (!c_new_root) {
278
- rb_raise(rb_eRuntimeError, "Could not reparent node (xmlDocCopyNode)");
279
- }
267
+ if (c_new_root->type != XML_ELEMENT_NODE) {
268
+ rb_raise(rb_eTypeError, "root must be a Nokogiri::XML::Element");
269
+ }
270
+ }
271
+
272
+ c_current_root = xmlDocGetRootElement(c_document);
273
+ if (c_current_root) {
274
+ xmlUnlinkNode(c_current_root);
275
+ noko_xml_document_pin_node(c_current_root);
276
+ }
277
+
278
+ /* If the new root's document is not the same as the current document,
279
+ * then we need to dup the node in to this document. */
280
+ if (c_new_root && c_new_root->doc != c_document) {
281
+ c_new_root = xmlDocCopyNode(c_new_root, c_document, 1);
282
+ if (!c_new_root) {
283
+ rb_raise(rb_eRuntimeError, "Could not reparent node (xmlDocCopyNode)");
280
284
  }
281
285
  }
282
286
 
@@ -317,12 +321,13 @@ static VALUE
317
321
  set_encoding(VALUE self, VALUE encoding)
318
322
  {
319
323
  xmlDocPtr doc = noko_xml_document_unwrap(self);
324
+ xmlChar *new_encoding = xmlStrdup((xmlChar *)StringValueCStr(encoding));
320
325
 
321
326
  if (doc->encoding) {
322
327
  xmlFree(DISCARD_CONST_QUAL_XMLCHAR(doc->encoding));
323
328
  }
324
329
 
325
- doc->encoding = xmlStrdup((xmlChar *)StringValueCStr(encoding));
330
+ doc->encoding = new_encoding;
326
331
 
327
332
  return encoding;
328
333
  }
@@ -379,17 +384,24 @@ noko_xml_document_s_read_io(VALUE rb_class,
379
384
 
380
385
  const char *c_url = NIL_P(rb_url) ? NULL : StringValueCStr(rb_url);
381
386
  const char *c_enc = NIL_P(rb_encoding) ? NULL : StringValueCStr(rb_encoding);
387
+ noko_io_read_reset_exception();
382
388
  xmlDocPtr c_document = xmlReadIO(
383
- (xmlInputReadCallback)noko_io_read,
389
+ (xmlInputReadCallback)noko_io_read_protected,
384
390
  (xmlInputCloseCallback)noko_io_close,
385
391
  (void *)rb_io,
386
392
  c_url,
387
393
  c_enc,
388
394
  (int)NUM2INT(rb_options)
389
395
  );
396
+ VALUE rb_io_read_exception = noko_io_read_take_exception();
390
397
 
391
398
  noko__structured_error_func_restore(&handler_state);
392
399
 
400
+ if (!NIL_P(rb_io_read_exception)) {
401
+ xmlFreeDoc(c_document);
402
+ noko_io_read_raise_exception(rb_io_read_exception);
403
+ }
404
+
393
405
  if (c_document == NULL) {
394
406
  xmlFreeDoc(c_document);
395
407
 
@@ -459,7 +471,7 @@ new (int argc, VALUE *argv, VALUE klass)
459
471
  VALUE version, rest, rb_doc ;
460
472
 
461
473
  rb_scan_args(argc, argv, "0*", &rest);
462
- version = rb_ary_entry(rest, (long)0);
474
+ version = rb_ary_entry(rest, (rb_len_t)0);
463
475
  if (NIL_P(version)) { version = rb_str_new2("1.0"); }
464
476
 
465
477
  doc = xmlNewDoc((xmlChar *)StringValueCStr(version));
@@ -604,7 +616,8 @@ rb_xml_document_canonicalize(int argc, VALUE *argv, VALUE self)
604
616
  VALUE rb_namespaces;
605
617
  VALUE rb_comments_p;
606
618
  int c_mode = 0;
607
- xmlChar **c_namespaces;
619
+ xmlChar **c_namespaces = NULL;
620
+ VALUE rb_namespaces_buffer = 0;
608
621
 
609
622
  xmlDocPtr c_doc;
610
623
  xmlOutputBufferPtr c_obuf;
@@ -624,6 +637,35 @@ rb_xml_document_canonicalize(int argc, VALUE *argv, VALUE self)
624
637
  if (c_mode == XML_C14N_1_0 || c_mode == XML_C14N_1_1) {
625
638
  rb_raise(rb_eRuntimeError, "This canonicalizer does not support this operation");
626
639
  }
640
+
641
+ // Copy every prefix into one temporary buffer before libxml2 sees it. Converting first means a
642
+ // raise from #to_str or a null byte leaks nothing. Copying means GC compaction, which moves the
643
+ // bytes of embedded strings, cannot move a prefix while libxml2 reads it. No allocation may
644
+ // happen between reading a prefix's pointer and copying its bytes.
645
+ rb_len_t ns_len = RARRAY_LEN(rb_namespaces);
646
+ VALUE rb_prefixes = rb_ary_new_capa(ns_len);
647
+ for (rb_len_t j = 0; j < ns_len; j++) {
648
+ VALUE entry = rb_ary_entry(rb_namespaces, j);
649
+ StringValueCStr(entry);
650
+ rb_ary_push(rb_prefixes, entry);
651
+ }
652
+
653
+ size_t size = ((size_t)ns_len + 1) * sizeof(xmlChar *);
654
+ for (rb_len_t j = 0; j < ns_len; j++) {
655
+ size += (size_t)RSTRING_LEN(RARRAY_AREF(rb_prefixes, j)) + 1;
656
+ }
657
+ c_namespaces = ALLOCV(rb_namespaces_buffer, size);
658
+
659
+ xmlChar *c_prefix = (xmlChar *)(c_namespaces + ns_len + 1);
660
+ for (rb_len_t j = 0; j < ns_len; j++) {
661
+ VALUE prefix = RARRAY_AREF(rb_prefixes, j);
662
+ size_t prefix_len = (size_t)RSTRING_LEN(prefix);
663
+ memcpy(c_prefix, RSTRING_PTR(prefix), prefix_len);
664
+ c_prefix[prefix_len] = '\0';
665
+ c_namespaces[j] = c_prefix;
666
+ c_prefix += prefix_len + 1;
667
+ }
668
+ c_namespaces[ns_len] = NULL;
627
669
  }
628
670
 
629
671
  c_doc = noko_xml_document_unwrap(self);
@@ -641,24 +683,13 @@ rb_xml_document_canonicalize(int argc, VALUE *argv, VALUE self)
641
683
  rb_callback = (void *)rb_block_proc();
642
684
  }
643
685
 
644
- if (NIL_P(rb_namespaces)) {
645
- c_namespaces = NULL;
646
- } else {
647
- long ns_len = RARRAY_LEN(rb_namespaces);
648
- c_namespaces = ruby_xcalloc((size_t)ns_len + 1, sizeof(xmlChar *));
649
- for (int j = 0 ; j < ns_len ; j++) {
650
- VALUE entry = rb_ary_entry(rb_namespaces, j);
651
- c_namespaces[j] = (xmlChar *)StringValueCStr(entry);
652
- }
653
- }
654
-
655
686
  int ret = xmlC14NExecute(c_doc, c_callback_wrapper, rb_callback,
656
687
  c_mode,
657
688
  c_namespaces,
658
689
  (int)RTEST(rb_comments_p),
659
690
  c_obuf);
660
691
 
661
- ruby_xfree(c_namespaces);
692
+ ALLOCV_END(rb_namespaces_buffer);
662
693
  xmlOutputBufferClose(c_obuf);
663
694
 
664
695
  if (ret < 0) {
@@ -708,6 +739,9 @@ noko_xml_document_unwrap(VALUE rb_document)
708
739
  {
709
740
  xmlDocPtr c_document;
710
741
  TypedData_Get_Struct(rb_document, xmlDoc, &xml_doc_type, c_document);
742
+ if (c_document == NULL) {
743
+ rb_raise(rb_eRuntimeError, "Uninitialized %" PRIsVALUE " struct (null data pointer)", rb_obj_class(rb_document));
744
+ }
711
745
  return c_document;
712
746
  }
713
747
 
@@ -725,7 +759,7 @@ noko_xml_document_has_wrapped_blank_nodes_p(xmlDocPtr c_document)
725
759
  return 0;
726
760
  }
727
761
 
728
- for (long jnode = 0; jnode < RARRAY_LEN(cache); jnode++) {
762
+ for (rb_len_t jnode = 0; jnode < RARRAY_LEN(cache); jnode++) {
729
763
  xmlNodePtr node;
730
764
  VALUE element = rb_ary_entry(cache, jnode);
731
765