From 6a0eb9f6cfbe3c76a3508418997de7ab2becd4b5 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 10 Sep 2016 18:19:24 +0200 Subject: [PATCH] src: provide allocation + nullptr check shortcuts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Provide shortcut `node::CheckedMalloc()` and friends that replace `node::Malloc()` + `CHECK_NE(ยท, nullptr);` combinations in a few places. Backport-PR-URL: https://github.com/nodejs/node/pull/16587 PR-URL: https://github.com/nodejs/node/pull/8482 Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell Reviewed-By: Michael Dawson Reviewed-By: Ilkka Myller --- src/cares_wrap.cc | 2 +- src/node.cc | 4 ++-- src/node_buffer.cc | 9 ++++----- src/node_crypto.cc | 17 ----------------- src/node_internals.h | 2 +- src/stream_wrap.cc | 8 +------- src/string_bytes.cc | 8 ++++---- src/tls_wrap.cc | 1 - src/udp_wrap.cc | 7 +------ src/util-inl.h | 29 +++++++++++++++++++++++++---- src/util.cc | 1 + src/util.h | 12 +++++++++++- test/cctest/test_util.cc | 16 ++++++++++++++++ 13 files changed, 67 insertions(+), 49 deletions(-) diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 86afbd681e0ab8..638daef76226ca 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -175,7 +175,7 @@ static void ares_poll_close_cb(uv_handle_t* watcher) { /* Allocates and returns a new node_ares_task */ static node_ares_task* ares_task_create(Environment* env, ares_socket_t sock) { - auto task = node::Malloc(1); + auto task = node::UncheckedMalloc(1); if (task == nullptr) { /* Out of memory. */ diff --git a/src/node.cc b/src/node.cc index 731f33495e039c..6345abdb312dda 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1054,9 +1054,9 @@ void* ArrayBufferAllocator::Allocate(size_t size) { if (env_ == nullptr || !env_->array_buffer_allocator_info()->no_zero_fill() || zero_fill_all_buffers) - return node::Calloc(size); + return node::UncheckedCalloc(size); env_->array_buffer_allocator_info()->reset_fill_flag(); - return node::Malloc(size); + return node::UncheckedMalloc(size); } static bool DomainHasErrorHandler(const Environment* env, diff --git a/src/node_buffer.cc b/src/node_buffer.cc index f06b00318ddaa6..5e6de043ee6006 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -56,8 +56,8 @@ bool zero_fill_all_buffers = false; namespace { inline void* BufferMalloc(size_t length) { - return zero_fill_all_buffers ? node::Calloc(length) : - node::Malloc(length); + return zero_fill_all_buffers ? node::UncheckedCalloc(length) : + node::UncheckedMalloc(length); } } // namespace @@ -253,7 +253,6 @@ MaybeLocal New(Isolate* isolate, data = nullptr; } else if (actual < length) { data = node::Realloc(data, actual); - CHECK_NE(data, nullptr); } } @@ -331,7 +330,7 @@ MaybeLocal Copy(Environment* env, const char* data, size_t length) { void* new_data; if (length > 0) { CHECK_NE(data, nullptr); - new_data = node::Malloc(length); + new_data = node::UncheckedMalloc(length); if (new_data == nullptr) return Local(); memcpy(new_data, data, length); @@ -1069,7 +1068,7 @@ void IndexOfString(const FunctionCallbackInfo& args) { offset, is_forward); } else if (enc == LATIN1) { - uint8_t* needle_data = node::Malloc(needle_length); + uint8_t* needle_data = node::UncheckedMalloc(needle_length); if (needle_data == nullptr) { return args.GetReturnValue().Set(-1); } diff --git a/src/node_crypto.cc b/src/node_crypto.cc index e1ae4d893b2108..09002972a94b5f 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -2387,7 +2387,6 @@ int SSLWrap::TLSExtStatusCallback(SSL* s, void* arg) { // OpenSSL takes control of the pointer after accepting it char* data = node::Malloc(len); - CHECK_NE(data, nullptr); memcpy(data, resp, len); if (!SSL_set_tlsext_status_ocsp_resp(s, data, len)) @@ -3467,7 +3466,6 @@ bool CipherBase::GetAuthTag(char** out, unsigned int* out_len) const { return false; *out_len = auth_tag_len_; *out = node::Malloc(auth_tag_len_); - CHECK_NE(*out, nullptr); memcpy(*out, auth_tag_, auth_tag_len_); return true; } @@ -5139,7 +5137,6 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo& args) { int field_size = EC_GROUP_get_degree(ecdh->group_); size_t out_len = (field_size + 7) / 8; char* out = node::Malloc(out_len); - CHECK_NE(out, nullptr); int r = ECDH_compute_key(out, out_len, pub, ecdh->key_, nullptr); EC_POINT_free(pub); @@ -5175,7 +5172,6 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo& args) { return env->ThrowError("Failed to get public key length"); unsigned char* out = node::Malloc(size); - CHECK_NE(out, nullptr); int r = EC_POINT_point2oct(ecdh->group_, pub, form, out, size, nullptr); if (r != size) { @@ -5201,7 +5197,6 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo& args) { int size = BN_num_bytes(b); unsigned char* out = node::Malloc(size); - CHECK_NE(out, nullptr); if (size != BN_bn2bin(b, out)) { free(out); @@ -5335,8 +5330,6 @@ class PBKDF2Request : public AsyncWrap { keylen_(keylen), key_(node::Malloc(keylen)), iter_(iter) { - if (key() == nullptr) - FatalError("node::PBKDF2Request()", "Out of Memory"); Wrap(object, this); } @@ -5497,9 +5490,6 @@ void PBKDF2(const FunctionCallbackInfo& args) { THROW_AND_RETURN_IF_NOT_BUFFER(args[1], "Salt"); pass = node::Malloc(passlen); - if (pass == nullptr) { - FatalError("node::PBKDF2()", "Out of Memory"); - } memcpy(pass, Buffer::Data(args[0]), passlen); saltlen = Buffer::Length(args[1]); @@ -5509,9 +5499,6 @@ void PBKDF2(const FunctionCallbackInfo& args) { } salt = node::Malloc(saltlen); - if (salt == nullptr) { - FatalError("node::PBKDF2()", "Out of Memory"); - } memcpy(salt, Buffer::Data(args[1]), saltlen); if (!args[2]->IsNumber()) { @@ -5602,8 +5589,6 @@ class RandomBytesRequest : public AsyncWrap { error_(0), size_(size), data_(node::Malloc(size)) { - if (data() == nullptr) - FatalError("node::RandomBytesRequest()", "Out of Memory"); Wrap(object, this); } @@ -5830,8 +5815,6 @@ void GetCurves(const FunctionCallbackInfo& args) { if (num_curves) { curves = node::Malloc(num_curves); - CHECK_NE(curves, nullptr); - if (EC_get_builtin_curves(curves, num_curves)) { for (size_t i = 0; i < num_curves; i++) { arr->Set(i, OneByteString(env->isolate(), OBJ_nid2sn(curves[i].nid))); diff --git a/src/node_internals.h b/src/node_internals.h index adcb7f835a3451..130af5d1c0f97f 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -199,7 +199,7 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { virtual void* Allocate(size_t size); // Defined in src/node.cc virtual void* AllocateUninitialized(size_t size) - { return node::Malloc(size); } + { return node::UncheckedMalloc(size); } virtual void Free(void* data, size_t) { free(data); } private: diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index f5bc4ad8c4eca3..ba03221696a539 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -150,12 +150,6 @@ void StreamWrap::OnAlloc(uv_handle_t* handle, void StreamWrap::OnAllocImpl(size_t size, uv_buf_t* buf, void* ctx) { buf->base = node::Malloc(size); buf->len = size; - - if (buf->base == nullptr && size > 0) { - FatalError( - "node::StreamWrap::DoAlloc(size_t, uv_buf_t*, void*)", - "Out Of Memory"); - } } @@ -204,8 +198,8 @@ void StreamWrap::OnReadImpl(ssize_t nread, return; } - char* base = node::Realloc(buf->base, nread); CHECK_LE(static_cast(nread), buf->len); + char* base = node::Realloc(buf->base, nread); if (pending == UV_TCP) { pending_obj = AcceptHandle(env, wrap); diff --git a/src/string_bytes.cc b/src/string_bytes.cc index 065a8ece15a06c..882ca6e3e89bd3 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -53,7 +53,7 @@ class ExternString: public ResourceType { if (length == 0) return scope.Escape(String::Empty(isolate)); - TypeName* new_data = node::Malloc(length); + TypeName* new_data = node::UncheckedMalloc(length); if (new_data == nullptr) { return Local(); } @@ -609,7 +609,7 @@ Local StringBytes::Encode(Isolate* isolate, case ASCII: if (contains_non_ascii(buf, buflen)) { - char* out = node::Malloc(buflen); + char* out = node::UncheckedMalloc(buflen); if (out == nullptr) { return Local(); } @@ -644,7 +644,7 @@ Local StringBytes::Encode(Isolate* isolate, case BASE64: { size_t dlen = base64_encoded_size(buflen); - char* dst = node::Malloc(dlen); + char* dst = node::UncheckedMalloc(dlen); if (dst == nullptr) { return Local(); } @@ -663,7 +663,7 @@ Local StringBytes::Encode(Isolate* isolate, case HEX: { size_t dlen = buflen * 2; - char* dst = node::Malloc(dlen); + char* dst = node::UncheckedMalloc(dlen); if (dst == nullptr) { return Local(); } diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index 4b8a575d56f2d8..813f7ef869ecce 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -664,7 +664,6 @@ void TLSWrap::OnDestructImpl(void* ctx) { void TLSWrap::OnAllocSelf(size_t suggested_size, uv_buf_t* buf, void* ctx) { buf->base = node::Malloc(suggested_size); - CHECK_NE(buf->base, nullptr); buf->len = suggested_size; } diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 43378199fe0188..d14eefd64d600a 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -375,11 +375,6 @@ void UDPWrap::OnAlloc(uv_handle_t* handle, uv_buf_t* buf) { buf->base = node::Malloc(suggested_size); buf->len = suggested_size; - - if (buf->base == nullptr && suggested_size > 0) { - FatalError("node::UDPWrap::OnAlloc(uv_handle_t*, size_t, uv_buf_t*)", - "Out Of Memory"); - } } @@ -415,7 +410,7 @@ void UDPWrap::OnRecv(uv_udp_t* handle, return; } - char* base = node::Realloc(buf->base, nread); + char* base = node::UncheckedRealloc(buf->base, nread); argv[2] = Buffer::New(env, base, nread).ToLocalChecked(); argv[3] = AddressToJS(env, addr); wrap->MakeCallback(env->onmessage_string(), arraysize(argv), argv); diff --git a/src/util-inl.h b/src/util-inl.h index 7d4eda49152b16..886b8569d63d2b 100644 --- a/src/util-inl.h +++ b/src/util-inl.h @@ -336,7 +336,7 @@ inline size_t MultiplyWithOverflowCheck(size_t a, size_t b) { // nullptr for zero-sized allocation requests. Normalize by always using // a nullptr. template -T* Realloc(T* pointer, size_t n) { +T* UncheckedRealloc(T* pointer, size_t n) { size_t full_size = MultiplyWithOverflowCheck(sizeof(T), n); if (full_size == 0) { @@ -349,18 +349,39 @@ T* Realloc(T* pointer, size_t n) { // As per spec realloc behaves like malloc if passed nullptr. template -T* Malloc(size_t n) { +T* UncheckedMalloc(size_t n) { if (n == 0) n = 1; - return Realloc(nullptr, n); + return UncheckedRealloc(nullptr, n); } template -T* Calloc(size_t n) { +T* UncheckedCalloc(size_t n) { if (n == 0) n = 1; MultiplyWithOverflowCheck(sizeof(T), n); return static_cast(calloc(n, sizeof(T))); } +template +T* Realloc(T* pointer, size_t n) { + T* ret = UncheckedRealloc(pointer, n); + if (n > 0) CHECK_NE(ret, nullptr); + return ret; +} + +template +T* Malloc(size_t n) { + T* ret = UncheckedMalloc(n); + if (n > 0) CHECK_NE(ret, nullptr); + return ret; +} + +template +T* Calloc(size_t n) { + T* ret = UncheckedCalloc(n); + if (n > 0) CHECK_NE(ret, nullptr); + return ret; +} + } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS diff --git a/src/util.cc b/src/util.cc index 7ce99d5c76aa93..14aa68996f56cc 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1,6 +1,7 @@ #include "util.h" #include "string_bytes.h" #include "node_buffer.h" +#include "node_internals.h" #include namespace node { diff --git a/src/util.h b/src/util.h index 59a26fb8527735..8b2db6f5c321e8 100644 --- a/src/util.h +++ b/src/util.h @@ -32,6 +32,15 @@ namespace node { // nullptr for zero-sized allocation requests. Normalize by always using // a nullptr. template +inline T* UncheckedRealloc(T* pointer, size_t n); +template +inline T* UncheckedMalloc(size_t n); +template +inline T* UncheckedCalloc(size_t n); + +// Same things, but aborts immediately instead of returning nullptr when +// no memory is available. +template inline T* Realloc(T* pointer, size_t n); template inline T* Malloc(size_t n); @@ -41,6 +50,8 @@ inline T* Calloc(size_t n); // Shortcuts for char*. inline char* Malloc(size_t n) { return Malloc(n); } inline char* Calloc(size_t n) { return Calloc(n); } +inline char* UncheckedMalloc(size_t n) { return UncheckedMalloc(n); } +inline char* UncheckedCalloc(size_t n) { return UncheckedCalloc(n); } #ifdef __GNUC__ #define NO_RETURN __attribute__((noreturn)) @@ -302,7 +313,6 @@ class MaybeStackBuffer { buf_ = buf_st_; } else { buf_ = Malloc(storage); - CHECK_NE(buf_, nullptr); } // Remember how much was allocated to check against that in SetLength(). diff --git a/test/cctest/test_util.cc b/test/cctest/test_util.cc index 7bbf53af13d3c4..f1446ae0345153 100644 --- a/test/cctest/test_util.cc +++ b/test/cctest/test_util.cc @@ -105,3 +105,19 @@ TEST(UtilTest, Calloc) { EXPECT_NE(nullptr, Calloc(0)); EXPECT_NE(nullptr, Calloc(1)); } + +TEST(UtilTest, UncheckedMalloc) { + using node::UncheckedMalloc; + EXPECT_NE(nullptr, UncheckedMalloc(0)); + EXPECT_NE(nullptr, UncheckedMalloc(1)); + EXPECT_NE(nullptr, UncheckedMalloc(0)); + EXPECT_NE(nullptr, UncheckedMalloc(1)); +} + +TEST(UtilTest, UncheckedCalloc) { + using node::UncheckedCalloc; + EXPECT_NE(nullptr, UncheckedCalloc(0)); + EXPECT_NE(nullptr, UncheckedCalloc(1)); + EXPECT_NE(nullptr, UncheckedCalloc(0)); + EXPECT_NE(nullptr, UncheckedCalloc(1)); +}