From 0d7fc0ecd0cf2feb2c18d67335a29fd3fd21aaba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20S=C3=A1nchez?= Date: Wed, 7 Sep 2016 08:44:53 -0600 Subject: [PATCH] Fix #45 and #46 - Fix memory leak and segfault - Fixed #45 by removing call to `worker-SaveToPersistent` - Fixed #46 by checking null pointer and assigning empty string if pointer was null Thanks to @cem for the help --- src/node_libxslt.cc | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/node_libxslt.cc b/src/node_libxslt.cc index c67ce53..70904d1 100644 --- a/src/node_libxslt.cc +++ b/src/node_libxslt.cc @@ -1,5 +1,4 @@ #define BUILDING_NODE_EXTENSION -#include #include #include #include @@ -125,8 +124,9 @@ NAN_METHOD(ApplySync) { unsigned char* resStr; int len; xsltSaveResultToString(&resStr,&len,result,stylesheet->stylesheet_obj); + const char* resOrEmpty = resStr ? reinterpret_cast(resStr) : ""; xmlFreeDoc(result); - info.GetReturnValue().Set(Nan::New((char*)resStr).ToLocalChecked()); + info.GetReturnValue().Set(Nan::New(resOrEmpty).ToLocalChecked()); } else { // Fill a result libxmljs document. // for some obscure reason I didn't manage to create a new libxmljs document in applySync, @@ -186,14 +186,13 @@ class ApplyWorker : public Nan::AsyncWorker { } else { unsigned char* resStr; int len; - int cnt=xsltSaveResultToString(&resStr,&len,result,stylesheet->stylesheet_obj); + int cnt = xsltSaveResultToString(&resStr,&len,result,stylesheet->stylesheet_obj); + const char* resOrEmpty = resStr ? reinterpret_cast(resStr) : ""; xmlFreeDoc(result); - Local argv[] = { Nan::Null(), Nan::New((char*)resStr).ToLocalChecked()}; + Local argv[] = { Nan::Null(), Nan::New(resOrEmpty).ToLocalChecked()}; freeArray(params, paramsLength); callback->Call(2, argv); } - - }; private: @@ -211,6 +210,7 @@ NAN_METHOD(ApplyAsync) { Nan::HandleScope scope; Stylesheet* stylesheet = Nan::ObjectWrap::Unwrap(info[0]->ToObject()); + libxmljs::XmlDocument* docSource = Nan::ObjectWrap::Unwrap(info[1]->ToObject()); Handle paramsArray = Handle::Cast(info[2]); bool outputString = info[3]->BooleanValue(); @@ -224,7 +224,6 @@ NAN_METHOD(ApplyAsync) { char** params = PrepareParams(paramsArray); ApplyWorker* worker = new ApplyWorker(stylesheet, docSource, params, paramsArray->Length(), outputString, docResult, callback); - for (uint32_t i = 0; i < 5; ++i) worker->SaveToPersistent(i, info[i]); Nan::AsyncQueueWorker(worker); return; }