Skip to content

Commit

Permalink
src: always clear wrap before persistent Reset()
Browse files Browse the repository at this point in the history
In case the handle is stored and accessed after the associated C++ class
was destructed, set the internal pointer to nullptr so any
getters/setters can return accordingly without aborting the application.

PR-URL: #6184
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
trevnorris authored and MylesBorins committed Oct 26, 2016
1 parent b3149ce commit 166a9b8
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "req-wrap-inl.h"
#include "tree.h"
#include "util.h"
#include "util-inl.h"
#include "uv.h"

#include <errno.h>
Expand Down Expand Up @@ -258,6 +259,7 @@ class QueryWrap : public AsyncWrap {

~QueryWrap() override {
CHECK_EQ(false, persistent().IsEmpty());
ClearWrap(object());
persistent().Reset();
}

Expand Down
3 changes: 1 addition & 2 deletions src/handle_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,12 @@ void HandleWrap::OnClose(uv_handle_t* handle) {

HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());
Local<Object> object = wrap->object();

if (wrap->flags_ & kCloseCallback) {
wrap->MakeCallback(env->onclose_string(), 0, nullptr);
}

object->SetAlignedPointerInInternalField(0, nullptr);
ClearWrap(wrap->object());
wrap->persistent().Reset();
delete wrap;
}
Expand Down
2 changes: 2 additions & 0 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4761,6 +4761,7 @@ class PBKDF2Request : public AsyncWrap {

~PBKDF2Request() override {
release();
ClearWrap(object());
persistent().Reset();
}

Expand Down Expand Up @@ -5026,6 +5027,7 @@ class RandomBytesRequest : public AsyncWrap {
}

~RandomBytesRequest() override {
ClearWrap(object());
persistent().Reset();
}

Expand Down
1 change: 1 addition & 0 deletions src/req-wrap-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ template <typename T>
ReqWrap<T>::~ReqWrap() {
CHECK_EQ(req_.data, this); // Assert that someone has called Dispatched().
CHECK_EQ(false, persistent().IsEmpty());
ClearWrap(object());
persistent().Reset();
}

Expand Down

0 comments on commit 166a9b8

Please sign in to comment.