Skip to content

Commit 1b093cb

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. PR-URL: #17000 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 74e7a4a commit 1b093cb

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
@@ -5534,9 +5534,9 @@ void PBKDF2Request::After() {
55345534

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

55425542

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

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

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

56005593
if (args[5]->IsFunction()) {
56015594
obj->Set(env->ondone_string(), args[5]);
@@ -5608,15 +5601,14 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
56085601
}
56095602

56105603
uv_queue_work(env->event_loop(),
5611-
req->work_req(),
5604+
req.release()->work_req(),
56125605
PBKDF2Request::Work,
56135606
PBKDF2Request::After);
56145607
} else {
56155608
env->PrintSyncTrace();
56165609
req->Work();
56175610
Local<Value> argv[2];
56185611
req->After(&argv);
5619-
delete req;
56205612

56215613
if (argv[0]->IsObject())
56225614
env->isolate()->ThrowException(argv[0]);
@@ -5754,25 +5746,23 @@ void RandomBytesCheck(RandomBytesRequest* req, Local<Value> (*argv)[2]) {
57545746

57555747
void RandomBytesAfter(uv_work_t* work_req, int status) {
57565748
CHECK_EQ(status, 0);
5757-
RandomBytesRequest* req =
5758-
ContainerOf(&RandomBytesRequest::work_req_, work_req);
5749+
std::unique_ptr<RandomBytesRequest> req(
5750+
ContainerOf(&RandomBytesRequest::work_req_, work_req));
57595751
Environment* env = req->env();
57605752
HandleScope handle_scope(env->isolate());
57615753
Context::Scope context_scope(env->context());
57625754
Local<Value> argv[2];
5763-
RandomBytesCheck(req, &argv);
5755+
RandomBytesCheck(req.get(), &argv);
57645756
req->MakeCallback(env->ondone_string(), arraysize(argv), argv);
5765-
delete req;
57665757
}
57675758

57685759

57695760
void RandomBytesProcessSync(Environment* env,
5770-
RandomBytesRequest* req,
5761+
std::unique_ptr<RandomBytesRequest> req,
57715762
Local<Value> (*argv)[2]) {
57725763
env->PrintSyncTrace();
57735764
RandomBytesWork(req->work_req());
5774-
RandomBytesCheck(req, argv);
5775-
delete req;
5765+
RandomBytesCheck(req.get(), argv);
57765766

57775767
if (!(*argv)[0]->IsNull())
57785768
env->isolate()->ThrowException((*argv)[0]);
@@ -5788,12 +5778,12 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
57885778
Local<Object> obj = env->randombytes_constructor_template()->
57895779
NewInstance(env->context()).ToLocalChecked();
57905780
char* data = node::Malloc(size);
5791-
RandomBytesRequest* req =
5781+
std::unique_ptr<RandomBytesRequest> req(
57925782
new RandomBytesRequest(env,
57935783
obj,
57945784
size,
57955785
data,
5796-
RandomBytesRequest::FREE_DATA);
5786+
RandomBytesRequest::FREE_DATA));
57975787

57985788
if (args[1]->IsFunction()) {
57995789
obj->Set(env->ondone_string(), args[1]);
@@ -5806,13 +5796,13 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
58065796
}
58075797

58085798
uv_queue_work(env->event_loop(),
5809-
req->work_req(),
5799+
req.release()->work_req(),
58105800
RandomBytesWork,
58115801
RandomBytesAfter);
58125802
args.GetReturnValue().Set(obj);
58135803
} else {
58145804
Local<Value> argv[2];
5815-
RandomBytesProcessSync(env, req, &argv);
5805+
RandomBytesProcessSync(env, std::move(req), &argv);
58165806
if (argv[0]->IsNull())
58175807
args.GetReturnValue().Set(argv[1]);
58185808
}
@@ -5835,12 +5825,12 @@ void RandomBytesBuffer(const FunctionCallbackInfo<Value>& args) {
58355825
char* data = Buffer::Data(args[0]);
58365826
data += offset;
58375827

5838-
RandomBytesRequest* req =
5828+
std::unique_ptr<RandomBytesRequest> req(
58395829
new RandomBytesRequest(env,
58405830
obj,
58415831
size,
58425832
data,
5843-
RandomBytesRequest::DONT_FREE_DATA);
5833+
RandomBytesRequest::DONT_FREE_DATA));
58445834
if (args[3]->IsFunction()) {
58455835
obj->Set(env->context(), env->ondone_string(), args[3]).FromJust();
58465836

@@ -5852,13 +5842,13 @@ void RandomBytesBuffer(const FunctionCallbackInfo<Value>& args) {
58525842
}
58535843

58545844
uv_queue_work(env->event_loop(),
5855-
req->work_req(),
5845+
req.release()->work_req(),
58565846
RandomBytesWork,
58575847
RandomBytesAfter);
58585848
args.GetReturnValue().Set(obj);
58595849
} else {
58605850
Local<Value> argv[2];
5861-
RandomBytesProcessSync(env, req, &argv);
5851+
RandomBytesProcessSync(env, std::move(req), &argv);
58625852
if (argv[0]->IsNull())
58635853
args.GetReturnValue().Set(argv[1]);
58645854
}

0 commit comments

Comments
 (0)