Skip to content

Commit 08a5b44

Browse files
cscotttrevnorris
authored andcommitted
node: add signature to SET_PROTOTYPE_METHOD
This prevents segfaults when a native method is reassigned to a different object (which corrupts args.This()). When unwrapping, clients should use args.Holder() instead of args.This(). Closes nodejs#6690. Signed-off-by: Trevor Norris <[email protected]>
1 parent d4fcb23 commit 08a5b44

23 files changed

+211
-145
lines changed

doc/api/addons.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ prototype:
401401
Isolate* isolate = Isolate::GetCurrent();
402402
HandleScope scope(isolate);
403403

404-
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.This());
404+
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
405405
obj->value_ += 1;
406406

407407
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
@@ -539,7 +539,7 @@ The implementation is similar to the above in `myobject.cc`:
539539
Isolate* isolate = Isolate::GetCurrent();
540540
HandleScope scope(isolate);
541541

542-
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.This());
542+
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
543543
obj->value_ += 1;
544544

545545
args.GetReturnValue().Set(Number::New(isolate, obj->value_));

src/fs_event_wrap.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void FSEventWrap::Start(const FunctionCallbackInfo<Value>& args) {
106106
Environment* env = Environment::GetCurrent(args.GetIsolate());
107107
HandleScope scope(env->isolate());
108108

109-
FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This());
109+
FSEventWrap* wrap = Unwrap<FSEventWrap>(args.Holder());
110110

111111
if (args.Length() < 1 || !args[0]->IsString()) {
112112
return env->ThrowTypeError("Bad arguments");
@@ -189,7 +189,7 @@ void FSEventWrap::Close(const FunctionCallbackInfo<Value>& args) {
189189
Environment* env = Environment::GetCurrent(args.GetIsolate());
190190
HandleScope scope(env->isolate());
191191

192-
FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This());
192+
FSEventWrap* wrap = Unwrap<FSEventWrap>(args.Holder());
193193

194194
if (wrap == NULL || wrap->initialized_ == false)
195195
return;

src/handle_wrap.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
4747
Environment* env = Environment::GetCurrent(args.GetIsolate());
4848
HandleScope scope(env->isolate());
4949

50-
HandleWrap* wrap = Unwrap<HandleWrap>(args.This());
50+
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder());
5151

5252
if (wrap != NULL && wrap->handle__ != NULL) {
5353
uv_ref(wrap->handle__);
@@ -60,7 +60,7 @@ void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
6060
Environment* env = Environment::GetCurrent(args.GetIsolate());
6161
HandleScope scope(env->isolate());
6262

63-
HandleWrap* wrap = Unwrap<HandleWrap>(args.This());
63+
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder());
6464

6565
if (wrap != NULL && wrap->handle__ != NULL) {
6666
uv_unref(wrap->handle__);
@@ -73,7 +73,7 @@ void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
7373
Environment* env = Environment::GetCurrent(args.GetIsolate());
7474
HandleScope scope(env->isolate());
7575

76-
HandleWrap* wrap = Unwrap<HandleWrap>(args.This());
76+
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder());
7777

7878
// guard against uninitialized handle or double close
7979
if (wrap == NULL || wrap->handle__ == NULL)

src/node.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ inline void NODE_SET_PROTOTYPE_METHOD(v8::Handle<v8::FunctionTemplate> recv,
212212
v8::FunctionCallback callback) {
213213
v8::Isolate* isolate = v8::Isolate::GetCurrent();
214214
v8::HandleScope handle_scope(isolate);
215-
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
216-
callback);
215+
v8::Handle<v8::Signature> s = v8::Signature::New(isolate, recv);
216+
v8::Local<v8::FunctionTemplate> t =
217+
v8::FunctionTemplate::New(isolate, callback, v8::Handle<v8::Value>(), s);
217218
recv->PrototypeTemplate()->Set(v8::String::NewFromUtf8(isolate, name),
218219
t->GetFunction());
219220
}

src/node_contextify.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,14 +634,13 @@ class ContextifyScript : public BaseObject {
634634
const bool display_errors,
635635
const FunctionCallbackInfo<Value>& args,
636636
TryCatch& try_catch) {
637-
if (!ContextifyScript::InstanceOf(env, args.This())) {
637+
if (!ContextifyScript::InstanceOf(env, args.Holder())) {
638638
env->ThrowTypeError(
639639
"Script methods can only be called on script instances.");
640640
return false;
641641
}
642642

643-
ContextifyScript* wrapped_script =
644-
Unwrap<ContextifyScript>(args.This());
643+
ContextifyScript* wrapped_script = Unwrap<ContextifyScript>(args.Holder());
645644
Local<UnboundScript> unbound_script =
646645
PersistentToLocal(env->isolate(), wrapped_script->script_);
647646
Local<Script> script = unbound_script->BindToCurrentContext();

0 commit comments

Comments
 (0)