Skip to content

Commit c981c48

Browse files
authored
Merge pull request #2480 from Garfield96/check-alloc
C: Check whether memory allocation was successful
2 parents 9f080d0 + 8727764 commit c981c48

File tree

5 files changed

+13
-13
lines changed

5 files changed

+13
-13
lines changed

ext/nokogiri/nokogiri.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ NOKOPUBFUN VALUE Nokogiri_wrap_xml_document(VALUE klass,
195195
#define NOKOGIRI_SAX_SELF(_ctxt) ((nokogiriSAXTuplePtr)(_ctxt))->self
196196
#define NOKOGIRI_SAX_CTXT(_ctxt) ((nokogiriSAXTuplePtr)(_ctxt))->ctxt
197197
#define NOKOGIRI_SAX_TUPLE_NEW(_ctxt, _self) nokogiri_sax_tuple_new(_ctxt, _self)
198-
#define NOKOGIRI_SAX_TUPLE_DESTROY(_tuple) free(_tuple)
198+
#define NOKOGIRI_SAX_TUPLE_DESTROY(_tuple) ruby_xfree(_tuple)
199199

200200
#define DISCARD_CONST_QUAL(t, v) ((t)(uintptr_t)(v))
201201
#define DISCARD_CONST_QUAL_XMLCHAR(v) DISCARD_CONST_QUAL(xmlChar *, v)
@@ -214,7 +214,7 @@ static inline
214214
nokogiriSAXTuplePtr
215215
nokogiri_sax_tuple_new(xmlParserCtxtPtr ctxt, VALUE self)
216216
{
217-
nokogiriSAXTuplePtr tuple = malloc(sizeof(nokogiriSAXTuple));
217+
nokogiriSAXTuplePtr tuple = ruby_xmalloc(sizeof(nokogiriSAXTuple));
218218
tuple->self = self;
219219
tuple->ctxt = ctxt;
220220
return tuple;

ext/nokogiri/xml_document.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ dealloc(xmlDocPtr doc)
7272
st_foreach(node_hash, dealloc_node_i, (st_data_t)doc);
7373
st_free_table(node_hash);
7474

75-
free(doc->_private);
75+
ruby_xfree(doc->_private);
7676

7777
/* When both Nokogiri and libxml-ruby are loaded, make sure that all nodes
7878
* have their _private pointers cleared. This is to avoid libxml-ruby's
@@ -569,7 +569,7 @@ rb_xml_document_canonicalize(int argc, VALUE *argv, VALUE self)
569569
c_namespaces = NULL;
570570
} else {
571571
long ns_len = RARRAY_LEN(rb_namespaces);
572-
c_namespaces = calloc((size_t)ns_len + 1, sizeof(xmlChar *));
572+
c_namespaces = ruby_xcalloc((size_t)ns_len + 1, sizeof(xmlChar *));
573573
for (int j = 0 ; j < ns_len ; j++) {
574574
VALUE entry = rb_ary_entry(rb_namespaces, j);
575575
c_namespaces[j] = (xmlChar *)StringValueCStr(entry);
@@ -582,7 +582,7 @@ rb_xml_document_canonicalize(int argc, VALUE *argv, VALUE self)
582582
(int)RTEST(rb_comments_p),
583583
c_obuf);
584584

585-
free(c_namespaces);
585+
ruby_xfree(c_namespaces);
586586
xmlOutputBufferClose(c_obuf);
587587

588588
return rb_funcall(rb_io, rb_intern("string"), 0);
@@ -600,7 +600,7 @@ noko_xml_document_wrap_with_init_args(VALUE klass, xmlDocPtr c_document, int arg
600600

601601
rb_document = Data_Wrap_Struct(klass, mark, dealloc, c_document);
602602

603-
tuple = (nokogiriTuplePtr)malloc(sizeof(nokogiriTuple));
603+
tuple = (nokogiriTuplePtr)ruby_xmalloc(sizeof(nokogiriTuple));
604604
tuple->doc = rb_document;
605605
tuple->unlinkedNodes = st_init_numtable_with_size(128);
606606
tuple->node_cache = rb_ary_new();

ext/nokogiri/xml_sax_parser.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,14 @@ static void
261261
deallocate(xmlSAXHandlerPtr handler)
262262
{
263263
NOKOGIRI_DEBUG_START(handler);
264-
free(handler);
264+
ruby_xfree(handler);
265265
NOKOGIRI_DEBUG_END(handler);
266266
}
267267

268268
static VALUE
269269
allocate(VALUE klass)
270270
{
271-
xmlSAXHandlerPtr handler = calloc((size_t)1, sizeof(xmlSAXHandler));
271+
xmlSAXHandlerPtr handler = ruby_xcalloc((size_t)1, sizeof(xmlSAXHandler));
272272

273273
handler->startDocument = start_document;
274274
handler->endDocument = end_document;

ext/nokogiri/xml_xpath_context.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ Nokogiri_marshal_xpath_funcall_and_return_values(xmlXPathParserContextPtr ctx, i
195195
assert(ctx->context->doc);
196196
assert(DOC_RUBY_OBJECT_TEST(ctx->context->doc));
197197

198-
argv = (VALUE *)calloc((size_t)nargs, sizeof(VALUE));
198+
argv = (VALUE *)ruby_xcalloc((size_t)nargs, sizeof(VALUE));
199199
for (int j = 0 ; j < nargs ; ++j) {
200200
rb_gc_register_address(&argv[j]);
201201
}
@@ -216,7 +216,7 @@ Nokogiri_marshal_xpath_funcall_and_return_values(xmlXPathParserContextPtr ctx, i
216216
for (int j = 0 ; j < nargs ; ++j) {
217217
rb_gc_unregister_address(&argv[j]);
218218
}
219-
free(argv);
219+
ruby_xfree(argv);
220220

221221
switch (TYPE(result)) {
222222
case T_FLOAT:

ext/nokogiri/xslt_stylesheet.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ dealloc(nokogiriXsltStylesheetTuple *wrapper)
1717
xsltFreeStylesheet(doc); /* commented out for now. */
1818
NOKOGIRI_DEBUG_END(doc);
1919

20-
free(wrapper);
20+
ruby_xfree(wrapper);
2121
}
2222

2323
static void
@@ -248,7 +248,7 @@ transform(int argc, VALUE *argv, VALUE self)
248248
Data_Get_Struct(self, nokogiriXsltStylesheetTuple, wrapper);
249249

250250
param_len = RARRAY_LEN(paramobj);
251-
params = calloc((size_t)param_len + 1, sizeof(char *));
251+
params = ruby_xcalloc((size_t)param_len + 1, sizeof(char *));
252252
for (j = 0 ; j < param_len ; j++) {
253253
VALUE entry = rb_ary_entry(paramobj, j);
254254
const char *ptr = StringValueCStr(entry);
@@ -261,7 +261,7 @@ transform(int argc, VALUE *argv, VALUE self)
261261
xmlSetGenericErrorFunc((void *)errstr, xslt_generic_error_handler);
262262

263263
result = xsltApplyStylesheet(wrapper->ss, xml, params);
264-
free(params);
264+
ruby_xfree(params);
265265

266266
xsltSetGenericErrorFunc(NULL, NULL);
267267
xmlSetGenericErrorFunc(NULL, NULL);

0 commit comments

Comments
 (0)