Skip to content

Commit 2f0556a

Browse files
committed
Check whether memory allocation was successful
1 parent d858d17 commit 2f0556a

File tree

5 files changed

+11
-0
lines changed

5 files changed

+11
-0
lines changed

ext/nokogiri/nokogiri.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ NOKOPUBFUN VALUE Nokogiri_wrap_xml_document(VALUE klass,
200200
#define DISCARD_CONST_QUAL(t, v) ((t)(uintptr_t)(v))
201201
#define DISCARD_CONST_QUAL_XMLCHAR(v) DISCARD_CONST_QUAL(xmlChar *, v)
202202

203+
#define CHECK_ALLOC(p) if (p == NULL) rb_raise(rb_eNoMemError, "Cannot allocate memory")
204+
203205
void Nokogiri_structured_error_func_save(libxmlStructuredErrorHandlerState *handler_state);
204206
void Nokogiri_structured_error_func_save_and_set(libxmlStructuredErrorHandlerState *handler_state, void *user_data,
205207
xmlStructuredErrorFunc handler);
@@ -215,6 +217,7 @@ nokogiriSAXTuplePtr
215217
nokogiri_sax_tuple_new(xmlParserCtxtPtr ctxt, VALUE self)
216218
{
217219
nokogiriSAXTuplePtr tuple = malloc(sizeof(nokogiriSAXTuple));
220+
CHECK_ALLOC(tuple);
218221
tuple->self = self;
219222
tuple->ctxt = ctxt;
220223
return tuple;

ext/nokogiri/xml_document.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,10 @@ rb_xml_document_canonicalize(int argc, VALUE *argv, VALUE self)
570570
} else {
571571
long ns_len = RARRAY_LEN(rb_namespaces);
572572
c_namespaces = calloc((size_t)ns_len + 1, sizeof(xmlChar *));
573+
if (c_namespaces == NULL) {
574+
xmlOutputBufferClose(c_obuf);
575+
rb_raise(rb_eNoMemError, "Cannot allocate memory");
576+
}
573577
for (int j = 0 ; j < ns_len ; j++) {
574578
VALUE entry = rb_ary_entry(rb_namespaces, j);
575579
c_namespaces[j] = (xmlChar *)StringValueCStr(entry);
@@ -601,6 +605,7 @@ noko_xml_document_wrap_with_init_args(VALUE klass, xmlDocPtr c_document, int arg
601605
rb_document = Data_Wrap_Struct(klass, mark, dealloc, c_document);
602606

603607
tuple = (nokogiriTuplePtr)malloc(sizeof(nokogiriTuple));
608+
CHECK_ALLOC(tuple);
604609
tuple->doc = rb_document;
605610
tuple->unlinkedNodes = st_init_numtable_with_size(128);
606611
tuple->node_cache = rb_ary_new();

ext/nokogiri/xml_sax_parser.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ static VALUE
269269
allocate(VALUE klass)
270270
{
271271
xmlSAXHandlerPtr handler = calloc((size_t)1, sizeof(xmlSAXHandler));
272+
CHECK_ALLOC(handler);
272273

273274
handler->startDocument = start_document;
274275
handler->endDocument = end_document;

ext/nokogiri/xml_xpath_context.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ Nokogiri_marshal_xpath_funcall_and_return_values(xmlXPathParserContextPtr ctx, i
196196
assert(DOC_RUBY_OBJECT_TEST(ctx->context->doc));
197197

198198
argv = (VALUE *)calloc((size_t)nargs, sizeof(VALUE));
199+
CHECK_ALLOC(argv);
199200
for (int j = 0 ; j < nargs ; ++j) {
200201
rb_gc_register_address(&argv[j]);
201202
}

ext/nokogiri/xslt_stylesheet.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ transform(int argc, VALUE *argv, VALUE self)
249249

250250
param_len = RARRAY_LEN(paramobj);
251251
params = calloc((size_t)param_len + 1, sizeof(char *));
252+
CHECK_ALLOC(params);
252253
for (j = 0 ; j < param_len ; j++) {
253254
VALUE entry = rb_ary_entry(paramobj, j);
254255
const char *ptr = StringValueCStr(entry);

0 commit comments

Comments
 (0)