Skip to content

Commit a05c49c

Browse files
fhinkelMylesBorins
authored andcommitted
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 113dd2b commit a05c49c

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

src/node_crypto.cc

+19-29
Original file line numberDiff line numberDiff line change
@@ -5565,9 +5565,9 @@ void PBKDF2Request::After() {
55655565

55665566
void PBKDF2Request::After(uv_work_t* work_req, int status) {
55675567
CHECK_EQ(status, 0);
5568-
PBKDF2Request* req = ContainerOf(&PBKDF2Request::work_req_, work_req);
5568+
std::unique_ptr<PBKDF2Request> req(
5569+
ContainerOf(&PBKDF2Request::work_req_, work_req));
55695570
req->After();
5570-
delete req;
55715571
}
55725572

55735573

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

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

56195618
obj = env->pbkdf2_constructor_template()->
56205619
NewInstance(env->context()).ToLocalChecked();
5621-
req = new PBKDF2Request(env,
5622-
obj,
5623-
digest,
5624-
passlen,
5625-
pass,
5626-
saltlen,
5627-
salt,
5628-
iter,
5629-
keylen);
5620+
std::unique_ptr<PBKDF2Request> req(
5621+
new PBKDF2Request(env, obj, digest, passlen, pass, saltlen, salt, iter,
5622+
keylen));
56305623

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

56415634
uv_queue_work(env->event_loop(),
5642-
req->work_req(),
5635+
req.release()->work_req(),
56435636
PBKDF2Request::Work,
56445637
PBKDF2Request::After);
56455638
} else {
56465639
env->PrintSyncTrace();
56475640
req->Work();
56485641
Local<Value> argv[2];
56495642
req->After(&argv);
5650-
delete req;
56515643

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

57865778
void RandomBytesAfter(uv_work_t* work_req, int status) {
57875779
CHECK_EQ(status, 0);
5788-
RandomBytesRequest* req =
5789-
ContainerOf(&RandomBytesRequest::work_req_, work_req);
5780+
std::unique_ptr<RandomBytesRequest> req(
5781+
ContainerOf(&RandomBytesRequest::work_req_, work_req));
57905782
Environment* env = req->env();
57915783
HandleScope handle_scope(env->isolate());
57925784
Context::Scope context_scope(env->context());
57935785
Local<Value> argv[2];
5794-
RandomBytesCheck(req, &argv);
5786+
RandomBytesCheck(req.get(), &argv);
57955787
req->MakeCallback(env->ondone_string(), arraysize(argv), argv);
5796-
delete req;
57975788
}
57985789

57995790

58005791
void RandomBytesProcessSync(Environment* env,
5801-
RandomBytesRequest* req,
5792+
std::unique_ptr<RandomBytesRequest> req,
58025793
Local<Value> (*argv)[2]) {
58035794
env->PrintSyncTrace();
58045795
RandomBytesWork(req->work_req());
5805-
RandomBytesCheck(req, argv);
5806-
delete req;
5796+
RandomBytesCheck(req.get(), argv);
58075797

58085798
if (!(*argv)[0]->IsNull())
58095799
env->isolate()->ThrowException((*argv)[0]);
@@ -5819,12 +5809,12 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
58195809
Local<Object> obj = env->randombytes_constructor_template()->
58205810
NewInstance(env->context()).ToLocalChecked();
58215811
char* data = node::Malloc(size);
5822-
RandomBytesRequest* req =
5812+
std::unique_ptr<RandomBytesRequest> req(
58235813
new RandomBytesRequest(env,
58245814
obj,
58255815
size,
58265816
data,
5827-
RandomBytesRequest::FREE_DATA);
5817+
RandomBytesRequest::FREE_DATA));
58285818

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

58395829
uv_queue_work(env->event_loop(),
5840-
req->work_req(),
5830+
req.release()->work_req(),
58415831
RandomBytesWork,
58425832
RandomBytesAfter);
58435833
args.GetReturnValue().Set(obj);
58445834
} else {
58455835
Local<Value> argv[2];
5846-
RandomBytesProcessSync(env, req, &argv);
5836+
RandomBytesProcessSync(env, std::move(req), &argv);
58475837
if (argv[0]->IsNull())
58485838
args.GetReturnValue().Set(argv[1]);
58495839
}
@@ -5866,12 +5856,12 @@ void RandomBytesBuffer(const FunctionCallbackInfo<Value>& args) {
58665856
char* data = Buffer::Data(args[0]);
58675857
data += offset;
58685858

5869-
RandomBytesRequest* req =
5859+
std::unique_ptr<RandomBytesRequest> req(
58705860
new RandomBytesRequest(env,
58715861
obj,
58725862
size,
58735863
data,
5874-
RandomBytesRequest::DONT_FREE_DATA);
5864+
RandomBytesRequest::DONT_FREE_DATA));
58755865
if (args[3]->IsFunction()) {
58765866
obj->Set(env->context(), env->ondone_string(), args[3]).FromJust();
58775867

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

58855875
uv_queue_work(env->event_loop(),
5886-
req->work_req(),
5876+
req.release()->work_req(),
58875877
RandomBytesWork,
58885878
RandomBytesAfter);
58895879
args.GetReturnValue().Set(obj);
58905880
} else {
58915881
Local<Value> argv[2];
5892-
RandomBytesProcessSync(env, req, &argv);
5882+
RandomBytesProcessSync(env, std::move(req), &argv);
58935883
if (argv[0]->IsNull())
58945884
args.GetReturnValue().Set(argv[1]);
58955885
}

0 commit comments

Comments
 (0)