Skip to content

Commit

Permalink
src: use unique_ptr for requests in crypto
Browse files Browse the repository at this point in the history
Instead of raw pointerns, use std::unique_ptr for PBKDF2Request and
RandomBytesRequest. This makes ownership more clear.

PR-URL: #17000
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
fhinkel authored and MylesBorins committed Dec 11, 2017
1 parent 113dd2b commit a05c49c
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5565,9 +5565,9 @@ void PBKDF2Request::After() {

void PBKDF2Request::After(uv_work_t* work_req, int status) {
CHECK_EQ(status, 0);
PBKDF2Request* req = ContainerOf(&PBKDF2Request::work_req_, work_req);
std::unique_ptr<PBKDF2Request> req(
ContainerOf(&PBKDF2Request::work_req_, work_req));
req->After();
delete req;
}


Expand All @@ -5582,7 +5582,6 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
double raw_keylen = -1;
int keylen = -1;
int iter = -1;
PBKDF2Request* req = nullptr;
Local<Object> obj;

passlen = Buffer::Length(args[0]);
Expand Down Expand Up @@ -5618,15 +5617,9 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {

obj = env->pbkdf2_constructor_template()->
NewInstance(env->context()).ToLocalChecked();
req = new PBKDF2Request(env,
obj,
digest,
passlen,
pass,
saltlen,
salt,
iter,
keylen);
std::unique_ptr<PBKDF2Request> req(
new PBKDF2Request(env, obj, digest, passlen, pass, saltlen, salt, iter,
keylen));

if (args[5]->IsFunction()) {
obj->Set(env->ondone_string(), args[5]);
Expand All @@ -5639,15 +5632,14 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
}

uv_queue_work(env->event_loop(),
req->work_req(),
req.release()->work_req(),
PBKDF2Request::Work,
PBKDF2Request::After);
} else {
env->PrintSyncTrace();
req->Work();
Local<Value> argv[2];
req->After(&argv);
delete req;

if (argv[0]->IsObject())
env->isolate()->ThrowException(argv[0]);
Expand Down Expand Up @@ -5785,25 +5777,23 @@ void RandomBytesCheck(RandomBytesRequest* req, Local<Value> (*argv)[2]) {

void RandomBytesAfter(uv_work_t* work_req, int status) {
CHECK_EQ(status, 0);
RandomBytesRequest* req =
ContainerOf(&RandomBytesRequest::work_req_, work_req);
std::unique_ptr<RandomBytesRequest> req(
ContainerOf(&RandomBytesRequest::work_req_, work_req));
Environment* env = req->env();
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());
Local<Value> argv[2];
RandomBytesCheck(req, &argv);
RandomBytesCheck(req.get(), &argv);
req->MakeCallback(env->ondone_string(), arraysize(argv), argv);
delete req;
}


void RandomBytesProcessSync(Environment* env,
RandomBytesRequest* req,
std::unique_ptr<RandomBytesRequest> req,
Local<Value> (*argv)[2]) {
env->PrintSyncTrace();
RandomBytesWork(req->work_req());
RandomBytesCheck(req, argv);
delete req;
RandomBytesCheck(req.get(), argv);

if (!(*argv)[0]->IsNull())
env->isolate()->ThrowException((*argv)[0]);
Expand All @@ -5819,12 +5809,12 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
Local<Object> obj = env->randombytes_constructor_template()->
NewInstance(env->context()).ToLocalChecked();
char* data = node::Malloc(size);
RandomBytesRequest* req =
std::unique_ptr<RandomBytesRequest> req(
new RandomBytesRequest(env,
obj,
size,
data,
RandomBytesRequest::FREE_DATA);
RandomBytesRequest::FREE_DATA));

if (args[1]->IsFunction()) {
obj->Set(env->ondone_string(), args[1]);
Expand All @@ -5837,13 +5827,13 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
}

uv_queue_work(env->event_loop(),
req->work_req(),
req.release()->work_req(),
RandomBytesWork,
RandomBytesAfter);
args.GetReturnValue().Set(obj);
} else {
Local<Value> argv[2];
RandomBytesProcessSync(env, req, &argv);
RandomBytesProcessSync(env, std::move(req), &argv);
if (argv[0]->IsNull())
args.GetReturnValue().Set(argv[1]);
}
Expand All @@ -5866,12 +5856,12 @@ void RandomBytesBuffer(const FunctionCallbackInfo<Value>& args) {
char* data = Buffer::Data(args[0]);
data += offset;

RandomBytesRequest* req =
std::unique_ptr<RandomBytesRequest> req(
new RandomBytesRequest(env,
obj,
size,
data,
RandomBytesRequest::DONT_FREE_DATA);
RandomBytesRequest::DONT_FREE_DATA));
if (args[3]->IsFunction()) {
obj->Set(env->context(), env->ondone_string(), args[3]).FromJust();

Expand All @@ -5883,13 +5873,13 @@ void RandomBytesBuffer(const FunctionCallbackInfo<Value>& args) {
}

uv_queue_work(env->event_loop(),
req->work_req(),
req.release()->work_req(),
RandomBytesWork,
RandomBytesAfter);
args.GetReturnValue().Set(obj);
} else {
Local<Value> argv[2];
RandomBytesProcessSync(env, req, &argv);
RandomBytesProcessSync(env, std::move(req), &argv);
if (argv[0]->IsNull())
args.GetReturnValue().Set(argv[1]);
}
Expand Down

0 comments on commit a05c49c

Please sign in to comment.