GeezerVison is showing github.com — src: normalize malloc, realloc · nodejs/node@51e09d0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/cares_wrap.cc +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -175,7 +175,8 @@ static void ares_poll_close_cb(uv_handle_t* watcher) { | 175 | 175 |
| 176 | 176 | /* Allocates and returns a new node_ares_task */ | 177 | 177 | static node_ares_task* ares_task_create(Environment* env, ares_socket_t sock) { | 178 | | - node_ares_task* task = static_cast<node_ares_task*>(malloc(sizeof(*task))); | | 178 | + node_ares_task* task = | | 179 | + static_cast<node_ares_task*>(node::Malloc(sizeof(*task))); | 179 | 180 |
| 180 | 181 | if (task == nullptr) { | 181 | 182 | /* Out of memory. */ | |
Collapse file Copy file name to clipboardExpand all lines: src/node.cc +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -978,9 +978,9 @@ void* ArrayBufferAllocator::Allocate(size_t size) { | 978 | 978 | if (env_ == nullptr || | 979 | 979 | !env_->array_buffer_allocator_info()->no_zero_fill() || | 980 | 980 | zero_fill_all_buffers) | 981 | | - return calloc(size, 1); | | 981 | + return node::Calloc(size, 1); | 982 | 982 | env_->array_buffer_allocator_info()->reset_fill_flag(); | 983 | | - return malloc(size); | | 983 | + return node::Malloc(size); | 984 | 984 | } | 985 | 985 |
| 986 | 986 | static bool DomainHasErrorHandler(const Environment* env, | |
Collapse file Copy file name to clipboardExpand all lines: src/node_buffer.cc +4-8Lines changed: 4 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 49 | 49 | size_t length = end - start; | 50 | 50 |
| 51 | 51 | #define BUFFER_MALLOC(length) \ | 52 | | - zero_fill_all_buffers ? calloc(length, 1) : malloc(length) | | 52 | + zero_fill_all_buffers ? node::Calloc(length, 1) : node::Malloc(length) | 53 | 53 |
| 54 | 54 | namespace node { | 55 | 55 |
| @@ -247,10 +247,6 @@ MaybeLocal<Object> New(Isolate* isolate, | 247 | 247 | size_t actual = 0; | 248 | 248 | char* data = nullptr; | 249 | 249 |
| 250 | | - // malloc(0) and realloc(ptr, 0) have implementation-defined behavior in | 251 | | - // that the standard allows them to either return a unique pointer or a | 252 | | - // nullptr for zero-sized allocation requests. Normalize by always using | 253 | | - // a nullptr. | 254 | 250 | if (length > 0) { | 255 | 251 | data = static_cast<char*>(BUFFER_MALLOC(length)); | 256 | 252 |
| @@ -264,7 +260,7 @@ MaybeLocal<Object> New(Isolate* isolate, | 264 | 260 | free(data); | 265 | 261 | data = nullptr; | 266 | 262 | } else if (actual < length) { | 267 | | - data = static_cast<char*>(realloc(data, actual)); | | 263 | + data = static_cast<char*>(node::Realloc(data, actual)); | 268 | 264 | CHECK_NE(data, nullptr); | 269 | 265 | } | 270 | 266 | } | @@ -343,7 +339,7 @@ MaybeLocal<Object> Copy(Environment* env, const char* data, size_t length) { | 343 | 339 | void* new_data; | 344 | 340 | if (length > 0) { | 345 | 341 | CHECK_NE(data, nullptr); | 346 | | - new_data = malloc(length); | | 342 | + new_data = node::Malloc(length); | 347 | 343 | if (new_data == nullptr) | 348 | 344 | return Local<Object>(); | 349 | 345 | memcpy(new_data, data, length); | @@ -931,7 +927,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) { | 931 | 927 | needle_length, | 932 | 928 | offset); | 933 | 929 | } else if (enc == BINARY) { | 934 | | - uint8_t* needle_data = static_cast<uint8_t*>(malloc(needle_length)); | | 930 | + uint8_t* needle_data = static_cast<uint8_t*>(node::Malloc(needle_length)); | 935 | 931 | if (needle_data == nullptr) { | 936 | 932 | return args.GetReturnValue().Set(-1); | 937 | 933 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/node_crypto.cc +10-10Lines changed: 10 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2090,7 +2090,7 @@ int SSLWrap<Base>::TLSExtStatusCallback(SSL* s, void* arg) { | 2090 | 2090 | size_t len = Buffer::Length(obj); | 2091 | 2091 |
| 2092 | 2092 | // OpenSSL takes control of the pointer after accepting it | 2093 | | - char* data = reinterpret_cast<char*>(malloc(len)); | | 2093 | + char* data = reinterpret_cast<char*>(node::Malloc(len)); | 2094 | 2094 | CHECK_NE(data, nullptr); | 2095 | 2095 | memcpy(data, resp, len); | 2096 | 2096 |
| @@ -3139,7 +3139,7 @@ bool CipherBase::GetAuthTag(char** out, unsigned int* out_len) const { | 3139 | 3139 | if (initialised_ || kind_ != kCipher || !auth_tag_) | 3140 | 3140 | return false; | 3141 | 3141 | *out_len = auth_tag_len_; | 3142 | | - *out = static_cast<char*>(malloc(auth_tag_len_)); | | 3142 | + *out = static_cast<char*>(node::Malloc(auth_tag_len_)); | 3143 | 3143 | CHECK_NE(*out, nullptr); | 3144 | 3144 | memcpy(*out, auth_tag_, auth_tag_len_); | 3145 | 3145 | return true; | @@ -4694,7 +4694,7 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) { | 4694 | 4694 | // NOTE: field_size is in bits | 4695 | 4695 | int field_size = EC_GROUP_get_degree(ecdh->group_); | 4696 | 4696 | size_t out_len = (field_size + 7) / 8; | 4697 | | - char* out = static_cast<char*>(malloc(out_len)); | | 4697 | + char* out = static_cast<char*>(node::Malloc(out_len)); | 4698 | 4698 | CHECK_NE(out, nullptr); | 4699 | 4699 |
| 4700 | 4700 | int r = ECDH_compute_key(out, out_len, pub, ecdh->key_, nullptr); | @@ -4733,7 +4733,7 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) { | 4733 | 4733 | if (size == 0) | 4734 | 4734 | return env->ThrowError("Failed to get public key length"); | 4735 | 4735 |
| 4736 | | - unsigned char* out = static_cast<unsigned char*>(malloc(size)); | | 4736 | + unsigned char* out = static_cast<unsigned char*>(node::Malloc(size)); | 4737 | 4737 | CHECK_NE(out, nullptr); | 4738 | 4738 |
| 4739 | 4739 | int r = EC_POINT_point2oct(ecdh->group_, pub, form, out, size, nullptr); | @@ -4762,7 +4762,7 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) { | 4762 | 4762 | return env->ThrowError("Failed to get ECDH private key"); | 4763 | 4763 |
| 4764 | 4764 | int size = BN_num_bytes(b); | 4765 | | - unsigned char* out = static_cast<unsigned char*>(malloc(size)); | | 4765 | + unsigned char* out = static_cast<unsigned char*>(node::Malloc(size)); | 4766 | 4766 | CHECK_NE(out, nullptr); | 4767 | 4767 |
| 4768 | 4768 | if (size != BN_bn2bin(b, out)) { | @@ -4839,7 +4839,7 @@ class PBKDF2Request : public AsyncWrap { | 4839 | 4839 | saltlen_(saltlen), | 4840 | 4840 | salt_(salt), | 4841 | 4841 | keylen_(keylen), | 4842 | | - key_(static_cast<char*>(malloc(keylen))), | | 4842 | + key_(static_cast<char*>(node::Malloc(keylen))), | 4843 | 4843 | iter_(iter) { | 4844 | 4844 | if (key() == nullptr) | 4845 | 4845 | FatalError("node::PBKDF2Request()", "Out of Memory"); | @@ -5002,7 +5002,7 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) { | 5002 | 5002 |
| 5003 | 5003 | THROW_AND_RETURN_IF_NOT_BUFFER(args[1]); | 5004 | 5004 |
| 5005 | | - pass = static_cast<char*>(malloc(passlen)); | | 5005 | + pass = static_cast<char*>(node::Malloc(passlen)); | 5006 | 5006 | if (pass == nullptr) { | 5007 | 5007 | FatalError("node::PBKDF2()", "Out of Memory"); | 5008 | 5008 | } | @@ -5014,7 +5014,7 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) { | 5014 | 5014 | goto err; | 5015 | 5015 | } | 5016 | 5016 |
| 5017 | | - salt = static_cast<char*>(malloc(saltlen)); | | 5017 | + salt = static_cast<char*>(node::Malloc(saltlen)); | 5018 | 5018 | if (salt == nullptr) { | 5019 | 5019 | FatalError("node::PBKDF2()", "Out of Memory"); | 5020 | 5020 | } | @@ -5107,7 +5107,7 @@ class RandomBytesRequest : public AsyncWrap { | 5107 | 5107 | : AsyncWrap(env, object, AsyncWrap::PROVIDER_CRYPTO), | 5108 | 5108 | error_(0), | 5109 | 5109 | size_(size), | 5110 | | - data_(static_cast<char*>(malloc(size))) { | | 5110 | + data_(static_cast<char*>(node::Malloc(size))) { | 5111 | 5111 | if (data() == nullptr) | 5112 | 5112 | FatalError("node::RandomBytesRequest()", "Out of Memory"); | 5113 | 5113 | Wrap(object, this); | @@ -5336,7 +5336,7 @@ void GetCurves(const FunctionCallbackInfo<Value>& args) { | 5336 | 5336 |
| 5337 | 5337 | if (num_curves) { | 5338 | 5338 | alloc_size = sizeof(*curves) * num_curves; | 5339 | | - curves = static_cast<EC_builtin_curve*>(malloc(alloc_size)); | | 5339 | + curves = static_cast<EC_builtin_curve*>(node::Malloc(alloc_size)); | 5340 | 5340 |
| 5341 | 5341 | CHECK_NE(curves, nullptr); | 5342 | 5342 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_internals.h +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -216,7 +216,8 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | 216 | 216 | inline void set_env(Environment* env) { env_ = env; } | 217 | 217 |
| 218 | 218 | virtual void* Allocate(size_t size); // Defined in src/node.cc | 219 | | - virtual void* AllocateUninitialized(size_t size) { return malloc(size); } | | 219 | + virtual void* AllocateUninitialized(size_t size) | | 220 | + { return node::Malloc(size); } | 220 | 221 | virtual void Free(void* data, size_t) { free(data); } | 221 | 222 |
| 222 | 223 | private: | |
Collapse file Copy file name to clipboardExpand all lines: src/stream_wrap.cc +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -154,7 +154,7 @@ void StreamWrap::OnAlloc(uv_handle_t* handle, | 154 | 154 |
| 155 | 155 |
| 156 | 156 | void StreamWrap::OnAllocImpl(size_t size, uv_buf_t* buf, void* ctx) { | 157 | | - buf->base = static_cast<char*>(malloc(size)); | | 157 | + buf->base = static_cast<char*>(node::Malloc(size)); | 158 | 158 | buf->len = size; | 159 | 159 |
| 160 | 160 | if (buf->base == nullptr && size > 0) { | @@ -210,7 +210,7 @@ void StreamWrap::OnReadImpl(ssize_t nread, | 210 | 210 | return; | 211 | 211 | } | 212 | 212 |
| 213 | | - char* base = static_cast<char*>(realloc(buf->base, nread)); | | 213 | + char* base = static_cast<char*>(node::Realloc(buf->base, nread)); | 214 | 214 | CHECK_LE(static_cast<size_t>(nread), buf->len); | 215 | 215 |
| 216 | 216 | if (pending == UV_TCP) { | |
Collapse file Copy file name to clipboardExpand all lines: src/string_bytes.cc +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,7 +54,7 @@ class ExternString: public ResourceType { | 54 | 54 | return scope.Escape(String::Empty(isolate)); | 55 | 55 |
| 56 | 56 | TypeName* new_data = | 57 | | - static_cast<TypeName*>(malloc(length * sizeof(*new_data))); | | 57 | + static_cast<TypeName*>(node::Malloc(length * sizeof(*new_data))); | 58 | 58 | if (new_data == nullptr) { | 59 | 59 | return Local<String>(); | 60 | 60 | } | @@ -784,7 +784,7 @@ Local<Value> StringBytes::Encode(Isolate* isolate, | 784 | 784 |
| 785 | 785 | case ASCII: | 786 | 786 | if (contains_non_ascii(buf, buflen)) { | 787 | | - char* out = static_cast<char*>(malloc(buflen)); | | 787 | + char* out = static_cast<char*>(node::Malloc(buflen)); | 788 | 788 | if (out == nullptr) { | 789 | 789 | return Local<String>(); | 790 | 790 | } | @@ -819,7 +819,7 @@ Local<Value> StringBytes::Encode(Isolate* isolate, | 819 | 819 |
| 820 | 820 | case BASE64: { | 821 | 821 | size_t dlen = base64_encoded_size(buflen); | 822 | | - char* dst = static_cast<char*>(malloc(dlen)); | | 822 | + char* dst = static_cast<char*>(node::Malloc(dlen)); | 823 | 823 | if (dst == nullptr) { | 824 | 824 | return Local<String>(); | 825 | 825 | } | @@ -838,7 +838,7 @@ Local<Value> StringBytes::Encode(Isolate* isolate, | 838 | 838 |
| 839 | 839 | case HEX: { | 840 | 840 | size_t dlen = buflen * 2; | 841 | | - char* dst = static_cast<char*>(malloc(dlen)); | | 841 | + char* dst = static_cast<char*>(node::Malloc(dlen)); | 842 | 842 | if (dst == nullptr) { | 843 | 843 | return Local<String>(); | 844 | 844 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/tls_wrap.cc +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -662,7 +662,7 @@ void TLSWrap::OnReadImpl(ssize_t nread, | 662 | 662 |
| 663 | 663 |
| 664 | 664 | void TLSWrap::OnAllocSelf(size_t suggested_size, uv_buf_t* buf, void* ctx) { | 665 | | - buf->base = static_cast<char*>(malloc(suggested_size)); | | 665 | + buf->base = static_cast<char*>(node::Malloc(suggested_size)); | 666 | 666 | CHECK_NE(buf->base, nullptr); | 667 | 667 | buf->len = suggested_size; | 668 | 668 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/udp_wrap.cc +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -358,7 +358,7 @@ void UDPWrap::OnSend(uv_udp_send_t* req, int status) { | 358 | 358 | void UDPWrap::OnAlloc(uv_handle_t* handle, | 359 | 359 | size_t suggested_size, | 360 | 360 | uv_buf_t* buf) { | 361 | | - buf->base = static_cast<char*>(malloc(suggested_size)); | | 361 | + buf->base = static_cast<char*>(node::Malloc(suggested_size)); | 362 | 362 | buf->len = suggested_size; | 363 | 363 |
| 364 | 364 | if (buf->base == nullptr && suggested_size > 0) { | @@ -400,7 +400,7 @@ void UDPWrap::OnRecv(uv_udp_t* handle, | 400 | 400 | return; | 401 | 401 | } | 402 | 402 |
| 403 | | - char* base = static_cast<char*>(realloc(buf->base, nread)); | | 403 | + char* base = static_cast<char*>(node::Realloc(buf->base, nread)); | 404 | 404 | argv[2] = Buffer::New(env, base, nread).ToLocalChecked(); | 405 | 405 | argv[3] = AddressToJS(env, addr); | 406 | 406 | wrap->MakeCallback(env->onmessage_string(), arraysize(argv), argv); | |
Collapse file Copy file name to clipboardExpand all lines: src/util-inl.h +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -217,6 +217,32 @@ bool StringEqualNoCase(const char* a, const char* b) { | 217 | 217 | return false; | 218 | 218 | } | 219 | 219 |
| | 220 | +// These should be used in our code as opposed to the native | | 221 | +// versions as they abstract out some platform and or | | 222 | +// compiler version specific functionality. | | 223 | +// malloc(0) and realloc(ptr, 0) have implementation-defined behavior in | | 224 | +// that the standard allows them to either return a unique pointer or a | | 225 | +// nullptr for zero-sized allocation requests. Normalize by always using | | 226 | +// a nullptr. | | 227 | +void* Realloc(void* pointer, size_t size) { | | 228 | + if (size == 0) { | | 229 | + free(pointer); | | 230 | + return nullptr; | | 231 | + } | | 232 | + return realloc(pointer, size); | | 233 | +} | | 234 | + | | 235 | +// As per spec realloc behaves like malloc if passed nullptr. | | 236 | +void* Malloc(size_t size) { | | 237 | + return Realloc(nullptr, size); | | 238 | +} | | 239 | + | | 240 | +void* Calloc(size_t n, size_t size) { | | 241 | + if ((n == 0) || (size == 0)) return nullptr; | | 242 | + CHECK_GE(n * size, n); // Overflow guard. | | 243 | + return calloc(n, size); | | 244 | +} | | 245 | + | 220 | 246 | } // namespace node | 221 | 247 |
| 222 | 248 | #endif // SRC_UTIL_INL_H_ |
|