Skip to content

Commit fffb9a3

Browse files
Paul Kiddieaddaleax
authored andcommitted
src: use MaybeStackBuffer on DoSend/Writev
instead of creating own buffer, use MaybeStackBuffer on DoSend to take advantage of its destructor to free up memory, so buffer never leaks memory - even if code in DoSend throws. Use MaybeStackBuffer in Writev to take advantage of destructor on MaybeStackBuffer to clear itself up, rather than Writev managing resources itself. PR-URL: #8626 Reviewed-By: Anna Henningsen <[email protected]>
1 parent c1e47ed commit fffb9a3

File tree

2 files changed

+4
-22
lines changed

2 files changed

+4
-22
lines changed

src/stream_base.cc

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
102102

103103
size_t count = chunks->Length() >> 1;
104104

105-
uv_buf_t bufs_[16];
106-
uv_buf_t* bufs = bufs_;
105+
MaybeStackBuffer<uv_buf_t, 16> bufs(count);
107106

108107
// Determine storage size first
109108
size_t storage_size = 0;
@@ -132,9 +131,6 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
132131
if (storage_size > INT_MAX)
133132
return UV_ENOBUFS;
134133

135-
if (arraysize(bufs_) < count)
136-
bufs = new uv_buf_t[count];
137-
138134
WriteWrap* req_wrap = WriteWrap::New(env,
139135
req_wrap_obj,
140136
this,
@@ -174,11 +170,7 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
174170
bytes += str_size;
175171
}
176172

177-
int err = DoWrite(req_wrap, bufs, count, nullptr);
178-
179-
// Deallocate space
180-
if (bufs != bufs_)
181-
delete[] bufs;
173+
int err = DoWrite(req_wrap, *bufs, count, nullptr);
182174

183175
req_wrap->object()->Set(env->async(), True(env->isolate()));
184176
req_wrap->object()->Set(env->bytes_string(),

src/udp_wrap.cc

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,7 @@ void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) {
275275
SendWrap* req_wrap = new SendWrap(env, req_wrap_obj, have_callback);
276276
size_t msg_size = 0;
277277

278-
// allocate uv_buf_t of the correct size
279-
// if bigger than 16 elements
280-
uv_buf_t bufs_[16];
281-
uv_buf_t* bufs = bufs_;
282-
283-
if (arraysize(bufs_) < count)
284-
bufs = new uv_buf_t[count];
278+
MaybeStackBuffer<uv_buf_t, 16> bufs(count);
285279

286280
// construct uv_buf_t array
287281
for (size_t i = 0; i < count; i++) {
@@ -313,16 +307,12 @@ void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) {
313307
if (err == 0) {
314308
err = uv_udp_send(&req_wrap->req_,
315309
&wrap->handle_,
316-
bufs,
310+
*bufs,
317311
count,
318312
reinterpret_cast<const sockaddr*>(&addr),
319313
OnSend);
320314
}
321315

322-
// Deallocate space
323-
if (bufs != bufs_)
324-
delete[] bufs;
325-
326316
req_wrap->Dispatched();
327317
if (err)
328318
delete req_wrap;

0 commit comments

Comments
 (0)