Skip to content

Commit 9acde46

Browse files
committed
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.
1 parent 23dfa71 commit 9acde46

23 files changed

+210
-143
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ class ContextifyScript : public BaseObject {
634634
}
635635

636636
ContextifyScript* wrapped_script =
637-
Unwrap<ContextifyScript>(args.This());
637+
Unwrap<ContextifyScript>(args.Holder());
638638
Local<Script> script = PersistentToLocal(env->isolate(),
639639
wrapped_script->script_);
640640

0 commit comments

Comments
 (0)