Browse files

Merge pull request #160 from jmandel/master

Fix segfault on broken xpath (+ test case)

Previously, the library failed to detect that an xpath query could have no results and would thus segfault. This checks if the xpath result set is empty and returns an empty array accordingly.
  • Loading branch information...
2 parents b69f528 + ee353f4 commit 894cad0082cf8a4aeb67ac84c82a9bd331f39057 @defunctzombie defunctzombie committed Aug 31, 2012
Showing with 14 additions and 0 deletions.
  1. +5 −0 src/xml_xpath_context.cc
  2. +9 −0 test/searching.js
View
5 src/xml_xpath_context.cc
@@ -32,6 +32,11 @@ XmlXpathContext::evaluate(const xmlChar* xpath) {
if (xpathobj) {
switch (xpathobj->type) {
case XPATH_NODESET: {
+ if (xmlXPathNodeSetIsEmpty(xpathobj->nodesetval)) {
+ res = v8::Array::New(0);
+ break;
+ }
+
v8::Handle<v8::Array> nodes = v8::Array::New(xpathobj->nodesetval->nodeNr);
for (int i = 0; i != xpathobj->nodesetval->nodeNr; ++i) {
nodes->Set(i, XmlNode::New(xpathobj->nodesetval->nodeTab[i]));
View
9 test/searching.js
@@ -14,6 +14,15 @@ module.exports.get = function(assert) {
assert.done();
};
+module.exports.get_missing = function(assert) {
+ var doc = libxml.Document();
+ var root = doc.node('root');
+
+ var missing = doc.get('missing/text()');
+ assert.equal(missing, undefined);
+ assert.done();
+};
+
module.exports.get_attr = function(assert) {
var doc = libxml.Document();
var root = doc.node('root');

0 comments on commit 894cad0

Please sign in to comment.