Skip to content

Commit e86545e

Browse files
committed
src: use unique_ptr for requests in crypto
Instead of raw pointerns, use std::unique_ptr for PBKDF2Request and RandomBytesRequest. This makes ownership more clear.
1 parent b204b09 commit e86545e

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

src/node_crypto.cc

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5533,9 +5533,9 @@ void PBKDF2Request::After() {
55335533

55345534
void PBKDF2Request::After(uv_work_t* work_req, int status) {
55355535
CHECK_EQ(status, 0);
5536-
PBKDF2Request* req = ContainerOf(&PBKDF2Request::work_req_, work_req);
5536+
std::unique_ptr<PBKDF2Request> req(
5537+
ContainerOf(&PBKDF2Request::work_req_, work_req));
55375538
req->After();
5538-
delete req;
55395539
}
55405540

55415541

@@ -5550,7 +5550,6 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
55505550
double raw_keylen = -1;
55515551
int keylen = -1;
55525552
int iter = -1;
5553-
PBKDF2Request* req = nullptr;
55545553
Local<Object> obj;
55555554

55565555
passlen = Buffer::Length(args[0]);
@@ -5586,15 +5585,9 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
55865585

55875586
obj = env->pbkdf2_constructor_template()->
55885587
NewInstance(env->context()).ToLocalChecked();
5589-
req = new PBKDF2Request(env,
5590-
obj,
5591-
digest,
5592-
passlen,
5593-
pass,
5594-
saltlen,
5595-
salt,
5596-
iter,
5597-
keylen);
5588+
std::unique_ptr<PBKDF2Request> req(
5589+
new PBKDF2Request(env, obj, digest, passlen, pass, saltlen, salt, iter,
5590+
keylen));
55985591

55995592
if (args[5]->IsFunction()) {
56005593
obj->Set(env->ondone_string(), args[5]);
@@ -5607,15 +5600,14 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
56075600
}
56085601

56095602
uv_queue_work(env->event_loop(),
5610-
req->work_req(),
5603+
req.release()->work_req(),
56115604
PBKDF2Request::Work,
56125605
PBKDF2Request::After);
56135606
} else {
56145607
env->PrintSyncTrace();
56155608
req->Work();
56165609
Local<Value> argv[2];
56175610
req->After(&argv);
5618-
delete req;
56195611

56205612
if (argv[0]->IsObject())
56215613
env->isolate()->ThrowException(argv[0]);
@@ -5753,25 +5745,23 @@ void RandomBytesCheck(RandomBytesRequest* req, Local<Value> (*argv)[2]) {
57535745

57545746
void RandomBytesAfter(uv_work_t* work_req, int status) {
57555747
CHECK_EQ(status, 0);
5756-
RandomBytesRequest* req =
5757-
ContainerOf(&RandomBytesRequest::work_req_, work_req);
5748+
std::unique_ptr<RandomBytesRequest> req(
5749+
ContainerOf(&RandomBytesRequest::work_req_, work_req));
57585750
Environment* env = req->env();
57595751
HandleScope handle_scope(env->isolate());
57605752
Context::Scope context_scope(env->context());
57615753
Local<Value> argv[2];
5762-
RandomBytesCheck(req, &argv);
5754+
RandomBytesCheck(req.get(), &argv);
57635755
req->MakeCallback(env->ondone_string(), arraysize(argv), argv);
5764-
delete req;
57655756
}
57665757

57675758

57685759
void RandomBytesProcessSync(Environment* env,
5769-
RandomBytesRequest* req,
5760+
std::unique_ptr<RandomBytesRequest> req,
57705761
Local<Value> (*argv)[2]) {
57715762
env->PrintSyncTrace();
57725763
RandomBytesWork(req->work_req());
5773-
RandomBytesCheck(req, argv);
5774-
delete req;
5764+
RandomBytesCheck(req.get(), argv);
57755765

57765766
if (!(*argv)[0]->IsNull())
57775767
env->isolate()->ThrowException((*argv)[0]);
@@ -5787,12 +5777,12 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
57875777
Local<Object> obj = env->randombytes_constructor_template()->
57885778
NewInstance(env->context()).ToLocalChecked();
57895779
char* data = node::Malloc(size);
5790-
RandomBytesRequest* req =
5780+
std::unique_ptr<RandomBytesRequest> req(
57915781
new RandomBytesRequest(env,
57925782
obj,
57935783
size,
57945784
data,
5795-
RandomBytesRequest::FREE_DATA);
5785+
RandomBytesRequest::FREE_DATA));
57965786

57975787
if (args[1]->IsFunction()) {
57985788
obj->Set(env->ondone_string(), args[1]);
@@ -5805,13 +5795,13 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
58055795
}
58065796

58075797
uv_queue_work(env->event_loop(),
5808-
req->work_req(),
5798+
req.release()->work_req(),
58095799
RandomBytesWork,
58105800
RandomBytesAfter);
58115801
args.GetReturnValue().Set(obj);
58125802
} else {
58135803
Local<Value> argv[2];
5814-
RandomBytesProcessSync(env, req, &argv);
5804+
RandomBytesProcessSync(env, std::move(req), &argv);
58155805
if (argv[0]->IsNull())
58165806
args.GetReturnValue().Set(argv[1]);
58175807
}
@@ -5834,12 +5824,12 @@ void RandomBytesBuffer(const FunctionCallbackInfo<Value>& args) {
58345824
char* data = Buffer::Data(args[0]);
58355825
data += offset;
58365826

5837-
RandomBytesRequest* req =
5827+
std::unique_ptr<RandomBytesRequest> req(
58385828
new RandomBytesRequest(env,
58395829
obj,
58405830
size,
58415831
data,
5842-
RandomBytesRequest::DONT_FREE_DATA);
5832+
RandomBytesRequest::DONT_FREE_DATA));
58435833
if (args[3]->IsFunction()) {
58445834
obj->Set(env->context(), env->ondone_string(), args[3]).FromJust();
58455835

@@ -5851,13 +5841,13 @@ void RandomBytesBuffer(const FunctionCallbackInfo<Value>& args) {
58515841
}
58525842

58535843
uv_queue_work(env->event_loop(),
5854-
req->work_req(),
5844+
req.release()->work_req(),
58555845
RandomBytesWork,
58565846
RandomBytesAfter);
58575847
args.GetReturnValue().Set(obj);
58585848
} else {
58595849
Local<Value> argv[2];
5860-
RandomBytesProcessSync(env, req, &argv);
5850+
RandomBytesProcessSync(env, std::move(req), &argv);
58615851
if (argv[0]->IsNull())
58625852
args.GetReturnValue().Set(argv[1]);
58635853
}

0 commit comments

Comments
 (0)