Skip to content

Commit

Permalink
Stop stealing documents
Browse files Browse the repository at this point in the history
Stealing documents can have adverse effects since there might be JavaScript
node objects referencing the xml document (using the _private member of the
xmlElement structure).  If we steal such a document, things can break in
many places since ownership becomes inconsistent.
  • Loading branch information
gagern committed Jan 2, 2016
1 parent 8d12867 commit 373dcc8
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/node_libxslt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,16 @@

using namespace v8;

// Assume ownership of the input document at the libxml level.
// The libxmljs object will be modified so it now represents an empty document.
static xmlDoc* stealDocument(Local<Value> input) {
static xmlDoc* copyDocument(Local<Value> input) {
libxmljs::XmlDocument* docWrapper =
Nan::ObjectWrap::Unwrap<libxmljs::XmlDocument>(input->ToObject());
xmlDoc* stylesheetDoc = docWrapper->xml_obj;
xmlDoc* dummyDoc = xmlNewDoc((const xmlChar*)"1.0");
stylesheetDoc->_private = NULL;
dummyDoc->_private = docWrapper;
docWrapper->xml_obj = dummyDoc;
return stylesheetDoc;
return xmlCopyDoc(stylesheetDoc, true);
}

NAN_METHOD(StylesheetSync) {
Nan::HandleScope scope;
xmlDoc* doc = stealDocument(info[0]);
xmlDoc* doc = copyDocument(info[0]);
xsltStylesheetPtr stylesheet = xsltParseStylesheetDoc(doc);
// TODO fetch actual error.
if (!stylesheet) {
Expand Down Expand Up @@ -83,7 +77,7 @@ class StylesheetWorker : public Nan::AsyncWorker {

NAN_METHOD(StylesheetAsync) {
Nan::HandleScope scope;
xmlDoc* doc = stealDocument(info[0]);
xmlDoc* doc = copyDocument(info[0]);
Nan::Callback *callback = new Nan::Callback(info[1].As<Function>());
StylesheetWorker* worker = new StylesheetWorker(doc, callback);
Nan::AsyncQueueWorker(worker);
Expand Down

0 comments on commit 373dcc8

Please sign in to comment.