From fffb9a386a6abc86664307a4d21a7f70518f0331 Mon Sep 17 00:00:00 2001 From: Paul Kiddie Date: Sat, 17 Sep 2016 10:45:00 +0100 Subject: 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: https://github.com/nodejs/node/pull/8626 Reviewed-By: Anna Henningsen --- src/stream_base.cc | 12 ++---------- src/udp_wrap.cc | 14 ++------------ 2 files changed, 4 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/stream_base.cc b/src/stream_base.cc index 585b84885c..a12a1efc73 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -102,8 +102,7 @@ int StreamBase::Writev(const FunctionCallbackInfo& args) { size_t count = chunks->Length() >> 1; - uv_buf_t bufs_[16]; - uv_buf_t* bufs = bufs_; + MaybeStackBuffer bufs(count); // Determine storage size first size_t storage_size = 0; @@ -132,9 +131,6 @@ int StreamBase::Writev(const FunctionCallbackInfo& args) { if (storage_size > INT_MAX) return UV_ENOBUFS; - if (arraysize(bufs_) < count) - bufs = new uv_buf_t[count]; - WriteWrap* req_wrap = WriteWrap::New(env, req_wrap_obj, this, @@ -174,11 +170,7 @@ int StreamBase::Writev(const FunctionCallbackInfo& args) { bytes += str_size; } - int err = DoWrite(req_wrap, bufs, count, nullptr); - - // Deallocate space - if (bufs != bufs_) - delete[] bufs; + int err = DoWrite(req_wrap, *bufs, count, nullptr); req_wrap->object()->Set(env->async(), True(env->isolate())); req_wrap->object()->Set(env->bytes_string(), diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index f191b3a755..7fba279fca 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -275,13 +275,7 @@ void UDPWrap::DoSend(const FunctionCallbackInfo& args, int family) { SendWrap* req_wrap = new SendWrap(env, req_wrap_obj, have_callback); size_t msg_size = 0; - // allocate uv_buf_t of the correct size - // if bigger than 16 elements - uv_buf_t bufs_[16]; - uv_buf_t* bufs = bufs_; - - if (arraysize(bufs_) < count) - bufs = new uv_buf_t[count]; + MaybeStackBuffer bufs(count); // construct uv_buf_t array for (size_t i = 0; i < count; i++) { @@ -313,16 +307,12 @@ void UDPWrap::DoSend(const FunctionCallbackInfo& args, int family) { if (err == 0) { err = uv_udp_send(&req_wrap->req_, &wrap->handle_, - bufs, + *bufs, count, reinterpret_cast(&addr), OnSend); } - // Deallocate space - if (bufs != bufs_) - delete[] bufs; - req_wrap->Dispatched(); if (err) delete req_wrap; -- cgit v1.2.1