Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: use unique_ptr for requests in crypto #17000

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 19 additions & 29 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5533,9 +5533,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 @@ -5550,7 +5550,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 @@ -5586,15 +5585,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 @@ -5607,15 +5600,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 @@ -5753,25 +5745,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 @@ -5787,12 +5777,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 @@ -5805,13 +5795,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 @@ -5834,12 +5824,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 @@ -5851,13 +5841,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