Skip to content

Commit ea862ac

Browse files
danbevrvagg
authored andcommitted
src: use smart pointers in cares_wrap.cc
PR-URL: #23813 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent 53fac5c commit ea862ac

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/cares_wrap.cc

+22-18
Original file line numberDiff line numberDiff line change
@@ -1796,22 +1796,25 @@ static void Query(const FunctionCallbackInfo<Value>& args) {
17961796

17971797
Local<Object> req_wrap_obj = args[0].As<Object>();
17981798
Local<String> string = args[1].As<String>();
1799-
Wrap* wrap = new Wrap(channel, req_wrap_obj);
1799+
auto wrap = std::make_unique<Wrap>(channel, req_wrap_obj);
18001800

18011801
node::Utf8Value name(env->isolate(), string);
18021802
channel->ModifyActivityQueryCount(1);
18031803
int err = wrap->Send(*name);
18041804
if (err) {
18051805
channel->ModifyActivityQueryCount(-1);
1806-
delete wrap;
1806+
} else {
1807+
// Release ownership of the pointer allowing the ownership to be transferred
1808+
USE(wrap.release());
18071809
}
18081810

18091811
args.GetReturnValue().Set(err);
18101812
}
18111813

18121814

18131815
void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) {
1814-
GetAddrInfoReqWrap* req_wrap = static_cast<GetAddrInfoReqWrap*>(req->data);
1816+
std::unique_ptr<GetAddrInfoReqWrap> req_wrap {
1817+
static_cast<GetAddrInfoReqWrap*>(req->data)};
18151818
Environment* env = req_wrap->env();
18161819

18171820
HandleScope handle_scope(env->isolate());
@@ -1868,21 +1871,20 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) {
18681871
uv_freeaddrinfo(res);
18691872

18701873
TRACE_EVENT_NESTABLE_ASYNC_END2(
1871-
TRACING_CATEGORY_NODE2(dns, native), "lookup", req_wrap,
1874+
TRACING_CATEGORY_NODE2(dns, native), "lookup", req_wrap.get(),
18721875
"count", n, "verbatim", verbatim);
18731876

18741877
// Make the callback into JavaScript
18751878
req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
1876-
1877-
delete req_wrap;
18781879
}
18791880

18801881

18811882
void AfterGetNameInfo(uv_getnameinfo_t* req,
18821883
int status,
18831884
const char* hostname,
18841885
const char* service) {
1885-
GetNameInfoReqWrap* req_wrap = static_cast<GetNameInfoReqWrap*>(req->data);
1886+
std::unique_ptr<GetNameInfoReqWrap> req_wrap {
1887+
static_cast<GetNameInfoReqWrap*>(req->data)};
18861888
Environment* env = req_wrap->env();
18871889

18881890
HandleScope handle_scope(env->isolate());
@@ -1903,14 +1905,12 @@ void AfterGetNameInfo(uv_getnameinfo_t* req,
19031905
}
19041906

19051907
TRACE_EVENT_NESTABLE_ASYNC_END2(
1906-
TRACING_CATEGORY_NODE2(dns, native), "lookupService", req_wrap,
1908+
TRACING_CATEGORY_NODE2(dns, native), "lookupService", req_wrap.get(),
19071909
"hostname", TRACE_STR_COPY(hostname),
19081910
"service", TRACE_STR_COPY(service));
19091911

19101912
// Make the callback into JavaScript
19111913
req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
1912-
1913-
delete req_wrap;
19141914
}
19151915

19161916
using ParseIPResult = decltype(static_cast<ares_addr_port_node*>(0)->addr);
@@ -1970,7 +1970,9 @@ void GetAddrInfo(const FunctionCallbackInfo<Value>& args) {
19701970
CHECK(0 && "bad address family");
19711971
}
19721972

1973-
auto req_wrap = new GetAddrInfoReqWrap(env, req_wrap_obj, args[4]->IsTrue());
1973+
auto req_wrap = std::make_unique<GetAddrInfoReqWrap>(env,
1974+
req_wrap_obj,
1975+
args[4]->IsTrue());
19741976

19751977
struct addrinfo hints;
19761978
memset(&hints, 0, sizeof(struct addrinfo));
@@ -1979,7 +1981,7 @@ void GetAddrInfo(const FunctionCallbackInfo<Value>& args) {
19791981
hints.ai_flags = flags;
19801982

19811983
TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(
1982-
TRACING_CATEGORY_NODE2(dns, native), "lookup", req_wrap,
1984+
TRACING_CATEGORY_NODE2(dns, native), "lookup", req_wrap.get(),
19831985
"hostname", TRACE_STR_COPY(*hostname),
19841986
"family",
19851987
family == AF_INET ? "ipv4" : family == AF_INET6 ? "ipv6" : "unspec");
@@ -1989,8 +1991,9 @@ void GetAddrInfo(const FunctionCallbackInfo<Value>& args) {
19891991
*hostname,
19901992
nullptr,
19911993
&hints);
1992-
if (err)
1993-
delete req_wrap;
1994+
if (err == 0)
1995+
// Release ownership of the pointer allowing the ownership to be transferred
1996+
USE(req_wrap.release());
19941997

19951998
args.GetReturnValue().Set(err);
19961999
}
@@ -2010,18 +2013,19 @@ void GetNameInfo(const FunctionCallbackInfo<Value>& args) {
20102013
CHECK(uv_ip4_addr(*ip, port, reinterpret_cast<sockaddr_in*>(&addr)) == 0 ||
20112014
uv_ip6_addr(*ip, port, reinterpret_cast<sockaddr_in6*>(&addr)) == 0);
20122015

2013-
GetNameInfoReqWrap* req_wrap = new GetNameInfoReqWrap(env, req_wrap_obj);
2016+
auto req_wrap = std::make_unique<GetNameInfoReqWrap>(env, req_wrap_obj);
20142017

20152018
TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(
2016-
TRACING_CATEGORY_NODE2(dns, native), "lookupService", req_wrap,
2019+
TRACING_CATEGORY_NODE2(dns, native), "lookupService", req_wrap.get(),
20172020
"ip", TRACE_STR_COPY(*ip), "port", port);
20182021

20192022
int err = req_wrap->Dispatch(uv_getnameinfo,
20202023
AfterGetNameInfo,
20212024
reinterpret_cast<struct sockaddr*>(&addr),
20222025
NI_NAMEREQD);
2023-
if (err)
2024-
delete req_wrap;
2026+
if (err == 0)
2027+
// Release ownership of the pointer allowing the ownership to be transferred
2028+
USE(req_wrap.release());
20252029

20262030
args.GetReturnValue().Set(err);
20272031
}

0 commit comments

Comments
 (0)