diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 1a83a1cf7adf09..de4397f228e0c4 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -1797,14 +1797,16 @@ static void Query(const FunctionCallbackInfo& args) { Local req_wrap_obj = args[0].As(); Local string = args[1].As(); - Wrap* wrap = new Wrap(channel, req_wrap_obj); + auto wrap = std::make_unique(channel, req_wrap_obj); node::Utf8Value name(env->isolate(), string); channel->ModifyActivityQueryCount(1); int err = wrap->Send(*name); if (err) { channel->ModifyActivityQueryCount(-1); - delete wrap; + } else { + // Release ownership of the pointer allowing the ownership to be transferred + USE(wrap.release()); } args.GetReturnValue().Set(err); @@ -1812,7 +1814,8 @@ static void Query(const FunctionCallbackInfo& args) { void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) { - GetAddrInfoReqWrap* req_wrap = static_cast(req->data); + std::unique_ptr req_wrap { + static_cast(req->data)}; Environment* env = req_wrap->env(); HandleScope handle_scope(env->isolate()); @@ -1869,13 +1872,11 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) { uv_freeaddrinfo(res); TRACE_EVENT_NESTABLE_ASYNC_END2( - TRACING_CATEGORY_NODE2(dns, native), "lookup", req_wrap, + TRACING_CATEGORY_NODE2(dns, native), "lookup", req_wrap.get(), "count", n, "verbatim", verbatim); // Make the callback into JavaScript req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv); - - delete req_wrap; } @@ -1883,7 +1884,8 @@ void AfterGetNameInfo(uv_getnameinfo_t* req, int status, const char* hostname, const char* service) { - GetNameInfoReqWrap* req_wrap = static_cast(req->data); + std::unique_ptr req_wrap { + static_cast(req->data)}; Environment* env = req_wrap->env(); HandleScope handle_scope(env->isolate()); @@ -1904,14 +1906,12 @@ void AfterGetNameInfo(uv_getnameinfo_t* req, } TRACE_EVENT_NESTABLE_ASYNC_END2( - TRACING_CATEGORY_NODE2(dns, native), "lookupService", req_wrap, + TRACING_CATEGORY_NODE2(dns, native), "lookupService", req_wrap.get(), "hostname", TRACE_STR_COPY(hostname), "service", TRACE_STR_COPY(service)); // Make the callback into JavaScript req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv); - - delete req_wrap; } using ParseIPResult = decltype(static_cast(0)->addr); @@ -1971,7 +1971,9 @@ void GetAddrInfo(const FunctionCallbackInfo& args) { CHECK(0 && "bad address family"); } - auto req_wrap = new GetAddrInfoReqWrap(env, req_wrap_obj, args[4]->IsTrue()); + auto req_wrap = std::make_unique(env, + req_wrap_obj, + args[4]->IsTrue()); struct addrinfo hints; memset(&hints, 0, sizeof(struct addrinfo)); @@ -1980,7 +1982,7 @@ void GetAddrInfo(const FunctionCallbackInfo& args) { hints.ai_flags = flags; TRACE_EVENT_NESTABLE_ASYNC_BEGIN2( - TRACING_CATEGORY_NODE2(dns, native), "lookup", req_wrap, + TRACING_CATEGORY_NODE2(dns, native), "lookup", req_wrap.get(), "hostname", TRACE_STR_COPY(*hostname), "family", family == AF_INET ? "ipv4" : family == AF_INET6 ? "ipv6" : "unspec"); @@ -1990,8 +1992,9 @@ void GetAddrInfo(const FunctionCallbackInfo& args) { *hostname, nullptr, &hints); - if (err) - delete req_wrap; + if (err == 0) + // Release ownership of the pointer allowing the ownership to be transferred + USE(req_wrap.release()); args.GetReturnValue().Set(err); } @@ -2011,18 +2014,19 @@ void GetNameInfo(const FunctionCallbackInfo& args) { CHECK(uv_ip4_addr(*ip, port, reinterpret_cast(&addr)) == 0 || uv_ip6_addr(*ip, port, reinterpret_cast(&addr)) == 0); - GetNameInfoReqWrap* req_wrap = new GetNameInfoReqWrap(env, req_wrap_obj); + auto req_wrap = std::make_unique(env, req_wrap_obj); TRACE_EVENT_NESTABLE_ASYNC_BEGIN2( - TRACING_CATEGORY_NODE2(dns, native), "lookupService", req_wrap, + TRACING_CATEGORY_NODE2(dns, native), "lookupService", req_wrap.get(), "ip", TRACE_STR_COPY(*ip), "port", port); int err = req_wrap->Dispatch(uv_getnameinfo, AfterGetNameInfo, reinterpret_cast(&addr), NI_NAMEREQD); - if (err) - delete req_wrap; + if (err == 0) + // Release ownership of the pointer allowing the ownership to be transferred + USE(req_wrap.release()); args.GetReturnValue().Set(err); }