Wrap calls in TryCatch; Check return values after UNWRAP.

This is to avoid a segfault when you don't use the API correctly.
This commit is contained in:
Ryan 2009-05-15 01:12:46 +02:00
Родитель 73fb24f48d
Коммит febbf75302
3 изменённых файлов: 36 добавлений и 6 удалений

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

@ -61,8 +61,16 @@ void*
ObjectWrap::Unwrap (Handle<Object> handle)
{
HandleScope scope;
Handle<External> field =
Handle<External>::Cast(handle->GetInternalField(0));
if(handle.IsEmpty() || handle->InternalFieldCount() == 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();
}