Skip to content

Commit

Permalink
Check for NULL unwrapped pointers from Node / v8
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbj committed Nov 17, 2015
1 parent 37d668f commit 177fc9f
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 36 deletions.
64 changes: 37 additions & 27 deletions src/njs/src/njsConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ void Connection::connectionPropertyException(Connection* njsConn,
Nan::HandleScope scope;

string msg;

if(!njsConn->isValid_)
msg = NJSMessages::getErrorMsg(errInvalidConnection);
else
Expand All @@ -197,6 +198,7 @@ void Connection::connectionPropertyException(Connection* njsConn,
NAN_GETTER(Connection::GetStmtCacheSize)
{
Connection* njsConn = Nan::ObjectWrap::Unwrap<Connection>(info.Holder());
NJS_CHECK_OBJECT_VALID2( njsConn, info ) ;
if(!njsConn->isValid_)
{
string error = NJSMessages::getErrorMsg ( errInvalidConnection );
Expand Down Expand Up @@ -224,7 +226,11 @@ NAN_GETTER(Connection::GetStmtCacheSize)
*/
NAN_SETTER(Connection::SetStmtCacheSize)
{
connectionPropertyException(Nan::ObjectWrap::Unwrap<Connection>(info.Holder()), errReadOnly, "stmtCacheSize");
Connection *connection = Nan::ObjectWrap::Unwrap<Connection>(info.Holder());

NJS_CHECK_OBJECT_VALID ( connection );

connectionPropertyException(connection, errReadOnly, "stmtCacheSize");
}

/*****************************************************************************/
Expand All @@ -246,6 +252,8 @@ NAN_GETTER(Connection::GetClientId)
NAN_SETTER(Connection::SetClientId)
{
Connection* njsConn = Nan::ObjectWrap::Unwrap<Connection>(info.Holder());
NJS_CHECK_OBJECT_VALID(njsConn) ;

if(!njsConn->isValid_)
{
string msg = NJSMessages::getErrorMsg(errInvalidConnection);
Expand Down Expand Up @@ -279,6 +287,7 @@ NAN_GETTER(Connection::GetModule)
NAN_SETTER(Connection::SetModule)
{
Connection *njsConn = Nan::ObjectWrap::Unwrap<Connection>(info.Holder());
NJS_CHECK_OBJECT_VALID (njsConn ) ;
if(!njsConn->isValid_)
{
string msg = NJSMessages::getErrorMsg(errInvalidConnection);
Expand Down Expand Up @@ -312,6 +321,7 @@ NAN_GETTER(Connection::GetAction)
NAN_SETTER(Connection::SetAction)
{
Connection *njsConn = Nan::ObjectWrap::Unwrap<Connection>(info.Holder());
NJS_CHECK_OBJECT_VALID ( njsConn ) ;
if(!njsConn->isValid_)
{
string msg = NJSMessages::getErrorMsg(errInvalidConnection);
Expand All @@ -334,6 +344,7 @@ NAN_GETTER (Connection::GetOracleServerVersion)
{
Connection *njsConn = ObjectWrap::Unwrap<Connection>(info.Holder());

NJS_CHECK_OBJECT_VALID2 ( njsConn, info ) ;
if ( !njsConn->isValid_ )
{
string error = NJSMessages::getErrorMsg ( errInvalidConnection );
Expand Down Expand Up @@ -379,8 +390,9 @@ NAN_GETTER (Connection::GetOracleServerVersion)
*/
NAN_SETTER(Connection::SetOracleServerVersion)
{
connectionPropertyException(ObjectWrap::Unwrap<Connection>(info.Holder()),
errReadOnly, "oracleServerVersion" );
Connection *connection = Nan::ObjectWrap::Unwrap<Connection>(info.Holder());
NJS_CHECK_OBJECT_VALID(connection);
connectionPropertyException(connection, errReadOnly, "oracleServerVersion" );
}


Expand Down Expand Up @@ -408,6 +420,8 @@ NAN_METHOD(Connection::Execute)
NJS_CHECK_NUMBER_OF_ARGS ( executeBaton->error, info, 2, 4, exitExecute );
connection = Nan::ObjectWrap::Unwrap<Connection>(info.This());

NJS_CHECK_OBJECT_VALID3( connection, executeBaton->error, exitExecute );

if(!connection->isValid_)
{
executeBaton->error = NJSMessages::getErrorMsg ( errInvalidConnection );
Expand Down Expand Up @@ -2530,6 +2544,7 @@ NAN_METHOD(Connection::Release)
NJS_CHECK_NUMBER_OF_ARGS ( releaseBaton->error, info, 1, 1, exitRelease );
connection = Nan::ObjectWrap::Unwrap<Connection>(info.This());

NJS_CHECK_OBJECT_VALID3(connection, releaseBaton->error, exitRelease);
if(!connection->isValid_)
{
releaseBaton->error = NJSMessages::getErrorMsg ( errInvalidConnection );
Expand Down Expand Up @@ -2627,6 +2642,7 @@ NAN_METHOD(Connection::Commit)
NJS_CHECK_NUMBER_OF_ARGS ( commitBaton->error, info, 1, 1, exitCommit );
connection = Nan::ObjectWrap::Unwrap<Connection>(info.This());

NJS_CHECK_OBJECT_VALID3 ( connection, commitBaton->error, exitCommit );
if(!connection->isValid_)
{
commitBaton->error = NJSMessages::getErrorMsg ( errInvalidConnection );
Expand Down Expand Up @@ -2721,6 +2737,7 @@ NAN_METHOD(Connection::Rollback)
rollbackBaton->cb.Reset( callback );
NJS_CHECK_NUMBER_OF_ARGS ( rollbackBaton->error, info, 1, 1, exitRollback );
connection = Nan::ObjectWrap::Unwrap<Connection>(info.This());
NJS_CHECK_OBJECT_VALID3 ( connection, rollbackBaton->error, exitRollback );

if(!connection->isValid_)
{
Expand Down Expand Up @@ -2815,6 +2832,8 @@ NAN_METHOD(Connection::Break)
NJS_CHECK_NUMBER_OF_ARGS ( breakBaton->error, info, 1, 1, exitBreak );
connection = Nan::ObjectWrap::Unwrap<Connection>(info.This());

NJS_CHECK_OBJECT_VALID3 ( connection, breakBaton->error, exitBreak );

if(!connection->isValid_)
{
breakBaton->error = NJSMessages::getErrorMsg ( errInvalidConnection );
Expand Down Expand Up @@ -3348,15 +3367,14 @@ v8::Local<v8::Value> Connection::NewLob(eBaton* executeBaton,
{
Nan::EscapableHandleScope scope;
Connection *connection = executeBaton->njsconn;
// Handle<Object> jsOracledb = connection->oracledb_->jsOracledb;
Local<Object> jsOracledb = Nan::New<Object>(connection->oracledb_->jsOracledb);
Local<Value> argv[1];

Local<Object> iLob = Nan::New<FunctionTemplate>(ILob::iLobTemplate_s)->GetFunction()->NewInstance();

// the ownership of all handles in the ProtoILob are transferred to ILob
// here. Any error in initialization of ILob will cleanup the OCI
// handles in the ILob cleanup routine.
// the ownership of all handles in the ProtoILob are transferred to ILob
// here. Any error in initialization of ILob will cleanup the OCI
// handles in the ILob cleanup routine.

(Nan::ObjectWrap::Unwrap<ILob>(iLob))->setILob(executeBaton, protoILob);

Expand All @@ -3369,12 +3387,6 @@ v8::Local<v8::Value> Connection::NewLob(eBaton* executeBaton,
Local<Function>::Cast(jsOracledb->Get(Nan::New<v8::String>("newLob").ToLocalChecked()))->Call(
jsOracledb, 1, argv);

// Local<Value> result =
// Nan::MakeCallback(
// jsOracledb,
// Nan::New<v8::String>("newLob").ToLocalChecked(),
// 1, argv);

return scope.Escape(result);
}

Expand All @@ -3387,7 +3399,6 @@ v8::Local<v8::Value> Connection::NewLob(eBaton* executeBaton,
PARAMETERS
none
RETURNS
A Lob object
Expand All @@ -3403,20 +3414,19 @@ v8::Local<v8::Value> Connection::NewLob(eBaton* executeBaton,

NAN_METHOD(Connection::GetLob)
{
Connection *connection = Nan::ObjectWrap::Unwrap<Connection>(info.This());
//Handle<Object> jsOracledb = connection->oracledb_->jsOracledb;
Local<Object> jsOracledb = Nan::New<Object>(connection->oracledb_->jsOracledb);
Local<Value> argv[1];
Connection *connection = Nan::ObjectWrap::Unwrap<Connection>(info.This());

Local<Value> result =
Local<Function>::Cast(jsOracledb->Get(Nan::New<v8::String>("newLob").ToLocalChecked()))->Call(
jsOracledb, 1, argv);
NJS_CHECK_OBJECT_VALID2 ( connection, info );

// Local<Value> result =
// Nan::MakeCallback(
// jsOracledb,
// Nan::New<v8::String>("newLob").ToLocalChecked(),
// 1, argv);
Local<Object> jsOracledb = Nan::New<Object>(
connection->oracledb_->jsOracledb);
Local<Value> argv[1];

Local<Value> result = Local<Function>::Cast(
jsOracledb->Get(
Nan::New<v8::String>(
"newLob").ToLocalChecked()))->Call(
jsOracledb, 1, argv);

info.GetReturnValue().Set(result);
}
Expand Down
27 changes: 20 additions & 7 deletions src/njs/src/njsIntLob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ NAN_METHOD(ILob::Release)
ILob *iLob = Nan::ObjectWrap::Unwrap<ILob>(info.This());
string msg;

NJS_CHECK_OBJECT_VALID2(iLob, info);
if( !iLob->njsconn_->isValid() )
{
msg = NJSMessages::getErrorMsg ( errInvalidConnection );
Expand Down Expand Up @@ -429,6 +430,7 @@ NAN_GETTER(ILob::GetChunkSize)
ILob *iLob = Nan::ObjectWrap::Unwrap<ILob>(info.Holder());
string msg;

NJS_CHECK_OBJECT_VALID2(iLob, info);
if( !iLob->njsconn_->isValid() )
{
msg = NJSMessages::getErrorMsg ( errInvalidConnection );
Expand Down Expand Up @@ -470,8 +472,9 @@ NAN_GETTER(ILob::GetChunkSize)

NAN_SETTER(ILob::SetChunkSize)
{
lobPropertyException(Nan::ObjectWrap::Unwrap<ILob>(info.Holder()), errReadOnly,
"chunkSize");
ILob *iLob = Nan::ObjectWrap::Unwrap<ILob>(info.Holder());
NJS_CHECK_OBJECT_VALID (iLob);
lobPropertyException(iLob, errReadOnly, "chunkSize");
}


Expand All @@ -497,6 +500,7 @@ NAN_GETTER(ILob::GetLength)
ILob *iLob = Nan::ObjectWrap::Unwrap<ILob>(info.Holder());
string msg;

NJS_CHECK_OBJECT_VALID2(iLob, info);
if( !iLob->njsconn_->isValid() )
{
msg = NJSMessages::getErrorMsg ( errInvalidConnection );
Expand Down Expand Up @@ -538,8 +542,9 @@ NAN_GETTER(ILob::GetLength)

NAN_SETTER(ILob::SetLength)
{
lobPropertyException(Nan::ObjectWrap::Unwrap<ILob>(info.Holder()), errReadOnly,
"length");
ILob *iLob = Nan::ObjectWrap::Unwrap<ILob>(info.Holder());
NJS_CHECK_OBJECT_VALID(iLob);
lobPropertyException(iLob, errReadOnly, "length");
}


Expand All @@ -564,6 +569,7 @@ NAN_GETTER(ILob::GetPieceSize)
ILob *iLob = Nan::ObjectWrap::Unwrap<ILob>(info.Holder());
string msg;

NJS_CHECK_OBJECT_VALID2(iLob, info);
if( !iLob->njsconn_->isValid() )
{
msg = NJSMessages::getErrorMsg ( errInvalidConnection );
Expand Down Expand Up @@ -607,6 +613,7 @@ NAN_SETTER(ILob::SetPieceSize)
ILob *iLob = Nan::ObjectWrap::Unwrap<ILob>(info.Holder());
string msg;

NJS_CHECK_OBJECT_VALID(iLob);
NJS_SET_PROP_UINT(iLob->bufSize_, value, "pieceSize");

if (iLob->state_ == ACTIVE)
Expand Down Expand Up @@ -659,6 +666,7 @@ NAN_GETTER(ILob::GetOffset)
ILob *iLob = Nan::ObjectWrap::Unwrap<ILob>(info.Holder());
string msg;

NJS_CHECK_OBJECT_VALID2(iLob, info);
if( !iLob->njsconn_->isValid() )
{
msg = NJSMessages::getErrorMsg ( errInvalidConnection );
Expand Down Expand Up @@ -703,6 +711,7 @@ NAN_SETTER(ILob::SetOffset)
double offset = 0.0;
string msg;

NJS_CHECK_OBJECT_VALID(iLob);
NJS_SET_PROP_UINT(offset, value, "offset");

if (offset < 1)
Expand Down Expand Up @@ -750,6 +759,7 @@ NAN_GETTER(ILob::GetType)
{
ILob *iLob = Nan::ObjectWrap::Unwrap<ILob>(info.Holder());

NJS_CHECK_OBJECT_VALID2(iLob, info);
try
{
Local<Number> value = Nan::New<v8::Number>((unsigned long)iLob->type_);
Expand Down Expand Up @@ -784,8 +794,9 @@ NAN_GETTER(ILob::GetType)

NAN_SETTER(ILob::SetType)
{
lobPropertyException(Nan::ObjectWrap::Unwrap<ILob>(info.Holder()), errReadOnly,
"type");
ILob *iLob = Nan::ObjectWrap::Unwrap<ILob>(info.Holder());
NJS_CHECK_OBJECT_VALID(iLob);
lobPropertyException(iLob, errReadOnly, "type");
}


Expand Down Expand Up @@ -819,6 +830,8 @@ NAN_METHOD(ILob::Read)
NJS_CHECK_NUMBER_OF_ARGS (lobBaton->error, info, 1, 1, exitRead);
iLob = Nan::ObjectWrap::Unwrap<ILob>(info.This());

NJS_CHECK_OBJECT_VALID3(iLob, lobBaton->error, exitRead);

if(!iLob->isValid_)
{
lobBaton->error = NJSMessages::getErrorMsg(errInvalidLob);
Expand Down Expand Up @@ -1005,7 +1018,7 @@ NAN_METHOD(ILob::Write)

NJS_CHECK_NUMBER_OF_ARGS (lobBaton->error, info, 2, 2, exitWrite);
iLob = Nan::ObjectWrap::Unwrap<ILob>(info.This());

NJS_CHECK_OBJECT_VALID3 ( iLob, lobBaton->error, exitWrite);
if(!iLob->isValid_)
{
lobBaton->error = NJSMessages::getErrorMsg(errInvalidLob);
Expand Down
1 change: 1 addition & 0 deletions src/njs/src/njsMessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static const char *errMsg[] =
"NJS-026: maxRows must be greater than zero",
"NJS-027: unexpected SQL parsing error",
"NJS-028: raw database type is not supported with DML Returning statements",
"NJS-029: Invalid object from javascript",
};

string NJSMessages::getErrorMsg ( NJSErrorType err, ... )
Expand Down
1 change: 1 addition & 0 deletions src/njs/src/njsMessages.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ typedef enum
errInvalidmaxRows,
errSQLSyntaxError,
errBufferReturningInvalid,
errInvalidJSObject,

// New ones should be added here

Expand Down
Loading

0 comments on commit 177fc9f

Please sign in to comment.