From 0a088a090af51cf95b0c5b607af6964fdbf89a38 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Tue, 16 Nov 2021 17:10:35 +0100 Subject: [PATCH] [quickjs] std::nothrow --- src/ecmascript/quickjs/document.c | 62 ++++++++++++++++++++----- src/ecmascript/quickjs/element.c | 30 ++++++++++-- src/ecmascript/quickjs/implementation.c | 2 +- 3 files changed, 77 insertions(+), 17 deletions(-) diff --git a/src/ecmascript/quickjs/document.c b/src/ecmascript/quickjs/document.c index 9f4ef12c..c9b4080a 100644 --- a/src/ecmascript/quickjs/document.c +++ b/src/ecmascript/quickjs/document.c @@ -90,7 +90,11 @@ js_document_get_property_anchors(JSContext *ctx, JSValueConst this_val) xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node(); xmlpp::ustring xpath = "//a"; - xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet; + xmlpp::Node::NodeSet *elements = new(std::nothrow) xmlpp::Node::NodeSet; + + if (!elements) { + return JS_NULL; + } *elements = root->find(xpath); @@ -300,7 +304,11 @@ js_document_get_property_childNodes(JSContext *ctx, JSValueConst this_val) return JS_NULL; } - xmlpp::Node::NodeList *nodes = new xmlpp::Node::NodeList; + xmlpp::Node::NodeList *nodes = new(std::nothrow) xmlpp::Node::NodeList; + + if (!nodes) { + return JS_NULL; + } *nodes = root->get_children(); if (nodes->empty()) { @@ -452,7 +460,7 @@ js_document_get_property_forms(JSContext *ctx, JSValueConst this_val) } if (!document->forms_nodeset) { - document->forms_nodeset = new xmlpp::Node::NodeSet; + document->forms_nodeset = new(std::nothrow) xmlpp::Node::NodeSet; } if (!document->forms_nodeset) { @@ -525,7 +533,11 @@ js_document_get_property_images(JSContext *ctx, JSValueConst this_val) xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node(); xmlpp::ustring xpath = "//img"; - xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet; + xmlpp::Node::NodeSet *elements = new(std::nothrow) xmlpp::Node::NodeSet; + + if (!elements) { + return JS_NULL; + } *elements = root->find(xpath); @@ -578,7 +590,11 @@ js_document_get_property_links(JSContext *ctx, JSValueConst this_val) xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node(); xmlpp::ustring xpath = "//a[@href]|//area[@href]"; - xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet; + xmlpp::Node::NodeSet *elements = new(std::nothrow) xmlpp::Node::NodeSet; + + if (!elements) { + return JS_NULL; + } *elements = root->find(xpath); @@ -728,7 +744,11 @@ js_document_get_property_scripts(JSContext *ctx, JSValueConst this_val) xmlpp::Element* root = (xmlpp::Element *)docu->get_root_node(); xmlpp::ustring xpath = "//script"; - xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet; + xmlpp::Node::NodeSet *elements = new(std::nothrow) xmlpp::Node::NodeSet; + + if (!elements) { + return JS_NULL; + } *elements = root->find(xpath); @@ -1108,7 +1128,11 @@ js_document_createDocumentFragment(JSContext *ctx, JSValueConst this_val, int ar return JS_NULL; } - xmlpp::Node *node = new xmlpp::Node(xmlnode); + xmlpp::Node *node = new(std::nothrow) xmlpp::Node(xmlnode); + + if (!node) { + return JS_NULL; + } return getElement(ctx, node); } @@ -1283,7 +1307,11 @@ js_document_getElementsByClassName(JSContext *ctx, JSValueConst this_val, int ar xmlpp::ustring xpath = "//*[@class=\""; xpath += id; xpath += "\"]"; - xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet; + xmlpp::Node::NodeSet *elements = new(std::nothrow) xmlpp::Node::NodeSet; + + if (!elements) { + return JS_NULL; + } *elements = root->find(xpath); @@ -1329,7 +1357,11 @@ js_document_getElementsByName(JSContext *ctx, JSValueConst this_val, int argc, J xpath += "\"]|//*[@name=\""; xpath += id; xpath += "\"]"; - xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet; + xmlpp::Node::NodeSet *elements = new(std::nothrow) xmlpp::Node::NodeSet; + + if (!elements) { + return JS_NULL; + } *elements = root->find(xpath); @@ -1372,7 +1404,11 @@ js_document_getElementsByTagName(JSContext *ctx, JSValueConst this_val, int argc xmlpp::ustring xpath = "//"; xpath += id; - xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet; + xmlpp::Node::NodeSet *elements = new(std::nothrow) xmlpp::Node::NodeSet; + + if (!elements) { + return JS_NULL; + } *elements = root->find(xpath); @@ -1465,7 +1501,11 @@ js_document_querySelectorAll(JSContext *ctx, JSValueConst this_val, int argc, JS xmlpp::ustring css = str; JS_FreeCString(ctx, str); xmlpp::ustring xpath = css2xpath(css); - xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet; + xmlpp::Node::NodeSet *elements = new(std::nothrow) xmlpp::Node::NodeSet; + + if (!elements) { + return JS_NULL; + } try { *elements = root->find(xpath); diff --git a/src/ecmascript/quickjs/element.c b/src/ecmascript/quickjs/element.c index 6d0cd541..82a8457e 100644 --- a/src/ecmascript/quickjs/element.c +++ b/src/ecmascript/quickjs/element.c @@ -74,7 +74,11 @@ js_element_get_property_attributes(JSContext *ctx, JSValueConst this_val) return JS_NULL; } - xmlpp::Element::AttributeList *attrs = new xmlpp::Element::AttributeList; + xmlpp::Element::AttributeList *attrs = new(std::nothrow) xmlpp::Element::AttributeList; + + if (!attrs) { + return JS_NULL; + } *attrs = el->get_attributes(); @@ -98,7 +102,11 @@ js_element_get_property_children(JSContext *ctx, JSValueConst this_val) return JS_NULL; } - xmlpp::Node::NodeSet *list = new xmlpp::Node::NodeSet; + xmlpp::Node::NodeSet *list = new(std::nothrow) xmlpp::Node::NodeSet; + + if (!list) { + return JS_NULL; + } auto it = nodes.begin(); auto end = nodes.end(); @@ -148,7 +156,11 @@ js_element_get_property_childNodes(JSContext *ctx, JSValueConst this_val) return JS_NULL; } - xmlpp::Node::NodeList *nodes = new xmlpp::Node::NodeList; + xmlpp::Node::NodeList *nodes = new(std::nothrow) xmlpp::Node::NodeList; + + if (!nodes) { + return JS_NULL; + } *nodes = el->get_children(); if (nodes->empty()) { @@ -1083,7 +1095,11 @@ js_element_cloneNode(JSContext *ctx, JSValueConst this_val, int argc, JSValueCon if (!xmlnode) { return JS_NULL; } - xmlpp::Node *node = new xmlpp::Node(xmlnode); + xmlpp::Node *node = new(std::nothrow) xmlpp::Node(xmlnode); + + if (!node) { + return JS_NULL; + } try { xmlpp::Node *node2 = node->import_node(el, JS_ToBool(ctx, argv[0])); @@ -1430,7 +1446,11 @@ js_element_querySelectorAll(JSContext *ctx, JSValueConst this_val, int argc, JSV xmlpp::ustring css = str; xmlpp::ustring xpath = css2xpath(css); JS_FreeCString(ctx, str); - xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet; + xmlpp::Node::NodeSet *elements = new(std::nothrow) xmlpp::Node::NodeSet; + + if (!elements) { + return JS_NULL; + } try { *elements = el->find(xpath); diff --git a/src/ecmascript/quickjs/implementation.c b/src/ecmascript/quickjs/implementation.c index 46a5b16f..89b0758c 100644 --- a/src/ecmascript/quickjs/implementation.c +++ b/src/ecmascript/quickjs/implementation.c @@ -55,7 +55,7 @@ js_implementation_createHTMLDocument(JSContext *ctx, JSValueConst this_val, int xmlDoc* doc = htmlReadDoc((xmlChar*)str.source, NULL, "utf-8", HTML_PARSE_RECOVER | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING); // Encapsulate raw libxml document in a libxml++ wrapper - xmlpp::Document *docu = new xmlpp::Document(doc); + xmlpp::Document *docu = new(std::nothrow) xmlpp::Document(doc); done_string(&str); JS_FreeCString(ctx, title);