diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 71fe303fd694ea..cfab04b11ead36 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -5534,9 +5534,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 req( + ContainerOf(&PBKDF2Request::work_req_, work_req)); req->After(); - delete req; } @@ -5551,7 +5551,6 @@ void PBKDF2(const FunctionCallbackInfo& args) { double raw_keylen = -1; int keylen = -1; int iter = -1; - PBKDF2Request* req = nullptr; Local obj; passlen = Buffer::Length(args[0]); @@ -5587,15 +5586,9 @@ void PBKDF2(const FunctionCallbackInfo& 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 req( + new PBKDF2Request(env, obj, digest, passlen, pass, saltlen, salt, iter, + keylen)); if (args[5]->IsFunction()) { obj->Set(env->ondone_string(), args[5]); @@ -5608,7 +5601,7 @@ void PBKDF2(const FunctionCallbackInfo& args) { } uv_queue_work(env->event_loop(), - req->work_req(), + req.release()->work_req(), PBKDF2Request::Work, PBKDF2Request::After); } else { @@ -5616,7 +5609,6 @@ void PBKDF2(const FunctionCallbackInfo& args) { req->Work(); Local argv[2]; req->After(&argv); - delete req; if (argv[0]->IsObject()) env->isolate()->ThrowException(argv[0]); @@ -5754,25 +5746,23 @@ void RandomBytesCheck(RandomBytesRequest* req, Local (*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 req( + ContainerOf(&RandomBytesRequest::work_req_, work_req)); Environment* env = req->env(); HandleScope handle_scope(env->isolate()); Context::Scope context_scope(env->context()); Local 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 req, Local (*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]); @@ -5788,12 +5778,12 @@ void RandomBytes(const FunctionCallbackInfo& args) { Local obj = env->randombytes_constructor_template()-> NewInstance(env->context()).ToLocalChecked(); char* data = node::Malloc(size); - RandomBytesRequest* req = + std::unique_ptr req( new RandomBytesRequest(env, obj, size, data, - RandomBytesRequest::FREE_DATA); + RandomBytesRequest::FREE_DATA)); if (args[1]->IsFunction()) { obj->Set(env->ondone_string(), args[1]); @@ -5806,13 +5796,13 @@ void RandomBytes(const FunctionCallbackInfo& args) { } uv_queue_work(env->event_loop(), - req->work_req(), + req.release()->work_req(), RandomBytesWork, RandomBytesAfter); args.GetReturnValue().Set(obj); } else { Local argv[2]; - RandomBytesProcessSync(env, req, &argv); + RandomBytesProcessSync(env, std::move(req), &argv); if (argv[0]->IsNull()) args.GetReturnValue().Set(argv[1]); } @@ -5835,12 +5825,12 @@ void RandomBytesBuffer(const FunctionCallbackInfo& args) { char* data = Buffer::Data(args[0]); data += offset; - RandomBytesRequest* req = + std::unique_ptr 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(); @@ -5852,13 +5842,13 @@ void RandomBytesBuffer(const FunctionCallbackInfo& args) { } uv_queue_work(env->event_loop(), - req->work_req(), + req.release()->work_req(), RandomBytesWork, RandomBytesAfter); args.GetReturnValue().Set(obj); } else { Local argv[2]; - RandomBytesProcessSync(env, req, &argv); + RandomBytesProcessSync(env, std::move(req), &argv); if (argv[0]->IsNull()) args.GetReturnValue().Set(argv[1]); }