Browse Source

Wrap calls in TryCatch; Check return values after UNWRAP.

This is to avoid a segfault when you don't use the API correctly.
v0.7.4-release
Ryan 16 years ago
parent
commit
febbf75302
  1. 5
      src/http.cc
  2. 25
      src/net.cc
  3. 12
      src/node.cc

5
src/http.cc

@ -231,10 +231,9 @@ HTTPConnection::on_body (http_parser *parser, const char *buf, size_t len)
} }
argv[0] = array; argv[0] = array;
} }
on_body->Call(message_handler, 1, argv);
TryCatch try_catch; TryCatch try_catch;
Local<Value> ret = on_body->Call(message_handler, 0, NULL); Local<Value> ret = on_body->Call(message_handler, 1, argv);
if (ret.IsEmpty()) { if (ret.IsEmpty()) {
fatal_exception(try_catch); fatal_exception(try_catch);
return -2; return -2;
@ -339,6 +338,8 @@ HTTPServer::OnConnection (struct sockaddr *addr, socklen_t len)
HTTPConnection::server_constructor_template->GetFunction()->NewInstance(0, NULL); HTTPConnection::server_constructor_template->GetFunction()->NewInstance(0, NULL);
HTTPConnection *connection = NODE_UNWRAP(HTTPConnection, connection_handle); HTTPConnection *connection = NODE_UNWRAP(HTTPConnection, connection_handle);
if (!connection) return NULL;
connection->SetAcceptor(handle_); connection->SetAcceptor(handle_);
return connection; return connection;

25
src/net.cc

@ -72,6 +72,8 @@ Handle<Value>
Connection::EncodingGetter (Local<String> _, const AccessorInfo& info) Connection::EncodingGetter (Local<String> _, const AccessorInfo& info)
{ {
Connection *connection = NODE_UNWRAP(Connection, info.This()); Connection *connection = NODE_UNWRAP(Connection, info.This());
if (!connection) return Handle<Value>();
HandleScope scope; HandleScope scope;
if (connection->encoding_ == UTF8) if (connection->encoding_ == UTF8)
@ -84,6 +86,8 @@ void
Connection::EncodingSetter (Local<String> _, Local<Value> value, const AccessorInfo& info) Connection::EncodingSetter (Local<String> _, Local<Value> value, const AccessorInfo& info)
{ {
Connection *connection = NODE_UNWRAP(Connection, info.This()); Connection *connection = NODE_UNWRAP(Connection, info.This());
if (!connection) return;
if (!value->IsString()) { if (!value->IsString()) {
connection->encoding_ = RAW; connection->encoding_ = RAW;
return; return;
@ -148,8 +152,10 @@ Connection::v8New (const Arguments& args)
Handle<Value> Handle<Value>
Connection::v8Connect (const Arguments& args) Connection::v8Connect (const Arguments& args)
{ {
HandleScope scope;
Connection *connection = NODE_UNWRAP(Connection, args.Holder()); Connection *connection = NODE_UNWRAP(Connection, args.Holder());
if (!connection) return Handle<Value>();
HandleScope scope;
if (args.Length() == 0 || args[0]->IsInt32() == false) if (args.Length() == 0 || args[0]->IsInt32() == false)
return ThrowException(String::New("Must specify a port.")); return ThrowException(String::New("Must specify a port."));
@ -231,6 +237,8 @@ Connection::v8Close (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Connection *connection = NODE_UNWRAP(Connection, args.Holder()); Connection *connection = NODE_UNWRAP(Connection, args.Holder());
if (!connection) return Handle<Value>();
connection->Close(); connection->Close();
return Undefined(); return Undefined();
} }
@ -240,6 +248,8 @@ Connection::v8FullClose (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Connection *connection = NODE_UNWRAP(Connection, args.Holder()); Connection *connection = NODE_UNWRAP(Connection, args.Holder());
if (!connection) return Handle<Value>();
connection->FullClose(); connection->FullClose();
return Undefined(); return Undefined();
} }
@ -249,6 +259,8 @@ Connection::v8ForceClose (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Connection *connection = NODE_UNWRAP(Connection, args.Holder()); Connection *connection = NODE_UNWRAP(Connection, args.Holder());
if (!connection) return Handle<Value>();
connection->ForceClose(); connection->ForceClose();
//connection->Detach(); //connection->Detach();
return Undefined(); return Undefined();
@ -260,6 +272,7 @@ Connection::v8Send (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Connection *connection = NODE_UNWRAP(Connection, args.Holder()); Connection *connection = NODE_UNWRAP(Connection, args.Holder());
if (!connection) return Handle<Value>();
if (args[0]->IsString()) { if (args[0]->IsString()) {
// utf8 encoding // utf8 encoding
@ -321,7 +334,7 @@ Connection::OnReceive (const void *buf, size_t len)
callback->Call(handle_, argc, argv); callback->Call(handle_, argc, argv);
if (try_catch.HasCaught()) if (try_catch.HasCaught())
fatal_exception(try_catch); // XXX is this the right action to take? fatal_exception(try_catch);
} }
#define DEFINE_SIMPLE_CALLBACK(name, symbol) \ #define DEFINE_SIMPLE_CALLBACK(name, symbol) \
@ -331,7 +344,10 @@ void name () \
Local<Value> callback_v = handle_->Get(symbol); \ Local<Value> callback_v = handle_->Get(symbol); \
if (!callback_v->IsFunction()) return; \ if (!callback_v->IsFunction()) return; \
Handle<Function> callback = Handle<Function>::Cast(callback_v); \ Handle<Function> callback = Handle<Function>::Cast(callback_v); \
TryCatch try_catch; \
callback->Call(handle_, 0, NULL); \ callback->Call(handle_, 0, NULL); \
if (try_catch.HasCaught()) \
fatal_exception(try_catch); \
} }
DEFINE_SIMPLE_CALLBACK(Connection::OnConnect, ON_CONNECT_SYMBOL) DEFINE_SIMPLE_CALLBACK(Connection::OnConnect, ON_CONNECT_SYMBOL)
@ -397,6 +413,8 @@ Acceptor::OnConnection (struct sockaddr *addr, socklen_t len)
Connection::constructor_template->GetFunction()->NewInstance(0, NULL); Connection::constructor_template->GetFunction()->NewInstance(0, NULL);
Connection *connection = NODE_UNWRAP(Connection, connection_handle); Connection *connection = NODE_UNWRAP(Connection, connection_handle);
if (!connection) return NULL;
connection->SetAcceptor(handle_); connection->SetAcceptor(handle_);
Handle<Value> argv[1] = { connection_handle }; Handle<Value> argv[1] = { connection_handle };
@ -436,6 +454,7 @@ Handle<Value>
Acceptor::v8Listen (const Arguments& args) Acceptor::v8Listen (const Arguments& args)
{ {
Acceptor *acceptor = NODE_UNWRAP(Acceptor, args.Holder()); Acceptor *acceptor = NODE_UNWRAP(Acceptor, args.Holder());
if (!acceptor) return Handle<Value>();
if (args.Length() < 1) if (args.Length() < 1)
return ThrowException(String::New("Must give at least a port as argument.")); return ThrowException(String::New("Must give at least a port as argument."));
@ -468,6 +487,8 @@ Handle<Value>
Acceptor::v8Close (const Arguments& args) Acceptor::v8Close (const Arguments& args)
{ {
Acceptor *acceptor = NODE_UNWRAP(Acceptor, args.Holder()); Acceptor *acceptor = NODE_UNWRAP(Acceptor, args.Holder());
if (!acceptor) return Handle<Value>();
acceptor->Close(); acceptor->Close();
return Undefined(); return Undefined();
} }

12
src/node.cc

@ -61,8 +61,16 @@ void*
ObjectWrap::Unwrap (Handle<Object> handle) ObjectWrap::Unwrap (Handle<Object> handle)
{ {
HandleScope scope; HandleScope scope;
Handle<External> field = if(handle.IsEmpty() || handle->InternalFieldCount() == 0) {
Handle<External>::Cast(handle->GetInternalField(0)); ThrowException(String::New("Tried to unwrap object without internal field."));
return NULL;
}
Local<Value> value = handle->GetInternalField(0);
if (value.IsEmpty()) {
ThrowException(String::New("Tried to unwrap object with empty internal field."));
return NULL;
}
Handle<External> field = Handle<External>::Cast(value);
return field->Value(); return field->Value();
} }

Loading…
Cancel
Save