Skip to content

Commit aed9792

Browse files
addaleaxjasnell
authored andcommitted
src: provide allocation + nullptr check shortcuts
Provide shortcut `node::CheckedMalloc()` and friends that replace `node::Malloc()` + `CHECK_NE(·, nullptr);` combinations in a few places. PR-URL: #8482 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
1 parent d2470d4 commit aed9792

13 files changed

+67
-49
lines changed

src/cares_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static void ares_poll_close_cb(uv_handle_t* watcher) {
174174

175175
/* Allocates and returns a new node_ares_task */
176176
static node_ares_task* ares_task_create(Environment* env, ares_socket_t sock) {
177-
auto task = node::Malloc<node_ares_task>(1);
177+
auto task = node::UncheckedMalloc<node_ares_task>(1);
178178

179179
if (task == nullptr) {
180180
/* Out of memory. */

src/node.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -979,9 +979,9 @@ Local<Value> WinapiErrnoException(Isolate* isolate,
979979

980980
void* ArrayBufferAllocator::Allocate(size_t size) {
981981
if (zero_fill_field_ || zero_fill_all_buffers)
982-
return node::Calloc(size);
982+
return node::UncheckedCalloc(size);
983983
else
984-
return node::Malloc(size);
984+
return node::UncheckedMalloc(size);
985985
}
986986

987987
static bool DomainHasErrorHandler(const Environment* env,

src/node_buffer.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ bool zero_fill_all_buffers = false;
8989
namespace {
9090

9191
inline void* BufferMalloc(size_t length) {
92-
return zero_fill_all_buffers ? node::Calloc(length) :
93-
node::Malloc(length);
92+
return zero_fill_all_buffers ? node::UncheckedCalloc(length) :
93+
node::UncheckedMalloc(length);
9494
}
9595

9696
} // namespace
@@ -285,7 +285,6 @@ MaybeLocal<Object> New(Isolate* isolate,
285285
data = nullptr;
286286
} else if (actual < length) {
287287
data = node::Realloc(data, actual);
288-
CHECK_NE(data, nullptr);
289288
}
290289
}
291290

@@ -363,7 +362,7 @@ MaybeLocal<Object> Copy(Environment* env, const char* data, size_t length) {
363362
void* new_data;
364363
if (length > 0) {
365364
CHECK_NE(data, nullptr);
366-
new_data = node::Malloc(length);
365+
new_data = node::UncheckedMalloc(length);
367366
if (new_data == nullptr)
368367
return Local<Object>();
369368
memcpy(new_data, data, length);
@@ -1086,7 +1085,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
10861085
offset,
10871086
is_forward);
10881087
} else if (enc == LATIN1) {
1089-
uint8_t* needle_data = node::Malloc<uint8_t>(needle_length);
1088+
uint8_t* needle_data = node::UncheckedMalloc<uint8_t>(needle_length);
10901089
if (needle_data == nullptr) {
10911090
return args.GetReturnValue().Set(-1);
10921091
}

src/node_crypto.cc

-17
Original file line numberDiff line numberDiff line change
@@ -2280,7 +2280,6 @@ int SSLWrap<Base>::TLSExtStatusCallback(SSL* s, void* arg) {
22802280

22812281
// OpenSSL takes control of the pointer after accepting it
22822282
char* data = node::Malloc(len);
2283-
CHECK_NE(data, nullptr);
22842283
memcpy(data, resp, len);
22852284

22862285
if (!SSL_set_tlsext_status_ocsp_resp(s, data, len))
@@ -3331,7 +3330,6 @@ bool CipherBase::GetAuthTag(char** out, unsigned int* out_len) const {
33313330
return false;
33323331
*out_len = auth_tag_len_;
33333332
*out = node::Malloc(auth_tag_len_);
3334-
CHECK_NE(*out, nullptr);
33353333
memcpy(*out, auth_tag_, auth_tag_len_);
33363334
return true;
33373335
}
@@ -4907,7 +4905,6 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
49074905
int field_size = EC_GROUP_get_degree(ecdh->group_);
49084906
size_t out_len = (field_size + 7) / 8;
49094907
char* out = node::Malloc(out_len);
4910-
CHECK_NE(out, nullptr);
49114908

49124909
int r = ECDH_compute_key(out, out_len, pub, ecdh->key_, nullptr);
49134910
EC_POINT_free(pub);
@@ -4943,7 +4940,6 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
49434940
return env->ThrowError("Failed to get public key length");
49444941

49454942
unsigned char* out = node::Malloc<unsigned char>(size);
4946-
CHECK_NE(out, nullptr);
49474943

49484944
int r = EC_POINT_point2oct(ecdh->group_, pub, form, out, size, nullptr);
49494945
if (r != size) {
@@ -4969,7 +4965,6 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
49694965

49704966
int size = BN_num_bytes(b);
49714967
unsigned char* out = node::Malloc<unsigned char>(size);
4972-
CHECK_NE(out, nullptr);
49734968

49744969
if (size != BN_bn2bin(b, out)) {
49754970
free(out);
@@ -5101,8 +5096,6 @@ class PBKDF2Request : public AsyncWrap {
51015096
keylen_(keylen),
51025097
key_(node::Malloc(keylen)),
51035098
iter_(iter) {
5104-
if (key() == nullptr)
5105-
FatalError("node::PBKDF2Request()", "Out of Memory");
51065099
Wrap(object, this);
51075100
}
51085101

@@ -5263,9 +5256,6 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
52635256
THROW_AND_RETURN_IF_NOT_BUFFER(args[1], "Salt");
52645257

52655258
pass = node::Malloc(passlen);
5266-
if (pass == nullptr) {
5267-
FatalError("node::PBKDF2()", "Out of Memory");
5268-
}
52695259
memcpy(pass, Buffer::Data(args[0]), passlen);
52705260

52715261
saltlen = Buffer::Length(args[1]);
@@ -5275,9 +5265,6 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
52755265
}
52765266

52775267
salt = node::Malloc(saltlen);
5278-
if (salt == nullptr) {
5279-
FatalError("node::PBKDF2()", "Out of Memory");
5280-
}
52815268
memcpy(salt, Buffer::Data(args[1]), saltlen);
52825269

52835270
if (!args[2]->IsNumber()) {
@@ -5368,8 +5355,6 @@ class RandomBytesRequest : public AsyncWrap {
53685355
error_(0),
53695356
size_(size),
53705357
data_(node::Malloc(size)) {
5371-
if (data() == nullptr)
5372-
FatalError("node::RandomBytesRequest()", "Out of Memory");
53735358
Wrap(object, this);
53745359
}
53755360

@@ -5596,8 +5581,6 @@ void GetCurves(const FunctionCallbackInfo<Value>& args) {
55965581
if (num_curves) {
55975582
curves = node::Malloc<EC_builtin_curve>(num_curves);
55985583

5599-
CHECK_NE(curves, nullptr);
5600-
56015584
if (EC_get_builtin_curves(curves, num_curves)) {
56025585
for (size_t i = 0; i < num_curves; i++) {
56035586
arr->Set(i, OneByteString(env->isolate(), OBJ_nid2sn(curves[i].nid)));

src/node_internals.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
160160

161161
virtual void* Allocate(size_t size); // Defined in src/node.cc
162162
virtual void* AllocateUninitialized(size_t size)
163-
{ return node::Malloc(size); }
163+
{ return node::UncheckedMalloc(size); }
164164
virtual void Free(void* data, size_t) { free(data); }
165165

166166
private:

src/stream_wrap.cc

+1-7
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,6 @@ void StreamWrap::OnAlloc(uv_handle_t* handle,
150150
void StreamWrap::OnAllocImpl(size_t size, uv_buf_t* buf, void* ctx) {
151151
buf->base = node::Malloc(size);
152152
buf->len = size;
153-
154-
if (buf->base == nullptr && size > 0) {
155-
FatalError(
156-
"node::StreamWrap::DoAlloc(size_t, uv_buf_t*, void*)",
157-
"Out Of Memory");
158-
}
159153
}
160154

161155

@@ -204,8 +198,8 @@ void StreamWrap::OnReadImpl(ssize_t nread,
204198
return;
205199
}
206200

207-
char* base = node::Realloc(buf->base, nread);
208201
CHECK_LE(static_cast<size_t>(nread), buf->len);
202+
char* base = node::Realloc(buf->base, nread);
209203

210204
if (pending == UV_TCP) {
211205
pending_obj = AcceptHandle<TCPWrap, uv_tcp_t>(env, wrap);

src/string_bytes.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ExternString: public ResourceType {
5353
if (length == 0)
5454
return scope.Escape(String::Empty(isolate));
5555

56-
TypeName* new_data = node::Malloc<TypeName>(length);
56+
TypeName* new_data = node::UncheckedMalloc<TypeName>(length);
5757
if (new_data == nullptr) {
5858
return Local<String>();
5959
}
@@ -623,7 +623,7 @@ Local<Value> StringBytes::Encode(Isolate* isolate,
623623

624624
case ASCII:
625625
if (contains_non_ascii(buf, buflen)) {
626-
char* out = node::Malloc(buflen);
626+
char* out = node::UncheckedMalloc(buflen);
627627
if (out == nullptr) {
628628
return Local<String>();
629629
}
@@ -658,7 +658,7 @@ Local<Value> StringBytes::Encode(Isolate* isolate,
658658

659659
case BASE64: {
660660
size_t dlen = base64_encoded_size(buflen);
661-
char* dst = node::Malloc(dlen);
661+
char* dst = node::UncheckedMalloc(dlen);
662662
if (dst == nullptr) {
663663
return Local<String>();
664664
}
@@ -677,7 +677,7 @@ Local<Value> StringBytes::Encode(Isolate* isolate,
677677

678678
case HEX: {
679679
size_t dlen = buflen * 2;
680-
char* dst = node::Malloc(dlen);
680+
char* dst = node::UncheckedMalloc(dlen);
681681
if (dst == nullptr) {
682682
return Local<String>();
683683
}

src/tls_wrap.cc

-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,6 @@ void TLSWrap::OnReadImpl(ssize_t nread,
662662

663663
void TLSWrap::OnAllocSelf(size_t suggested_size, uv_buf_t* buf, void* ctx) {
664664
buf->base = node::Malloc(suggested_size);
665-
CHECK_NE(buf->base, nullptr);
666665
buf->len = suggested_size;
667666
}
668667

src/udp_wrap.cc

+1-6
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,6 @@ void UDPWrap::OnAlloc(uv_handle_t* handle,
376376
uv_buf_t* buf) {
377377
buf->base = node::Malloc(suggested_size);
378378
buf->len = suggested_size;
379-
380-
if (buf->base == nullptr && suggested_size > 0) {
381-
FatalError("node::UDPWrap::OnAlloc(uv_handle_t*, size_t, uv_buf_t*)",
382-
"Out Of Memory");
383-
}
384379
}
385380

386381

@@ -416,7 +411,7 @@ void UDPWrap::OnRecv(uv_udp_t* handle,
416411
return;
417412
}
418413

419-
char* base = node::Realloc(buf->base, nread);
414+
char* base = node::UncheckedRealloc(buf->base, nread);
420415
argv[2] = Buffer::New(env, base, nread).ToLocalChecked();
421416
argv[3] = AddressToJS(env, addr);
422417
wrap->MakeCallback(env->onmessage_string(), arraysize(argv), argv);

src/util-inl.h

+25-4
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ inline size_t MultiplyWithOverflowCheck(size_t a, size_t b) {
245245
// nullptr for zero-sized allocation requests. Normalize by always using
246246
// a nullptr.
247247
template <typename T>
248-
T* Realloc(T* pointer, size_t n) {
248+
T* UncheckedRealloc(T* pointer, size_t n) {
249249
size_t full_size = MultiplyWithOverflowCheck(sizeof(T), n);
250250

251251
if (full_size == 0) {
@@ -258,18 +258,39 @@ T* Realloc(T* pointer, size_t n) {
258258

259259
// As per spec realloc behaves like malloc if passed nullptr.
260260
template <typename T>
261-
T* Malloc(size_t n) {
261+
T* UncheckedMalloc(size_t n) {
262262
if (n == 0) n = 1;
263-
return Realloc<T>(nullptr, n);
263+
return UncheckedRealloc<T>(nullptr, n);
264264
}
265265

266266
template <typename T>
267-
T* Calloc(size_t n) {
267+
T* UncheckedCalloc(size_t n) {
268268
if (n == 0) n = 1;
269269
MultiplyWithOverflowCheck(sizeof(T), n);
270270
return static_cast<T*>(calloc(n, sizeof(T)));
271271
}
272272

273+
template <typename T>
274+
T* Realloc(T* pointer, size_t n) {
275+
T* ret = UncheckedRealloc(pointer, n);
276+
if (n > 0) CHECK_NE(ret, nullptr);
277+
return ret;
278+
}
279+
280+
template <typename T>
281+
T* Malloc(size_t n) {
282+
T* ret = UncheckedMalloc<T>(n);
283+
if (n > 0) CHECK_NE(ret, nullptr);
284+
return ret;
285+
}
286+
287+
template <typename T>
288+
T* Calloc(size_t n) {
289+
T* ret = UncheckedCalloc<T>(n);
290+
if (n > 0) CHECK_NE(ret, nullptr);
291+
return ret;
292+
}
293+
273294
} // namespace node
274295

275296
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

src/util.cc

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "util.h"
22
#include "string_bytes.h"
33
#include "node_buffer.h"
4+
#include "node_internals.h"
45
#include <stdio.h>
56

67
namespace node {

src/util.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ namespace node {
3232
// nullptr for zero-sized allocation requests. Normalize by always using
3333
// a nullptr.
3434
template <typename T>
35+
inline T* UncheckedRealloc(T* pointer, size_t n);
36+
template <typename T>
37+
inline T* UncheckedMalloc(size_t n);
38+
template <typename T>
39+
inline T* UncheckedCalloc(size_t n);
40+
41+
// Same things, but aborts immediately instead of returning nullptr when
42+
// no memory is available.
43+
template <typename T>
3544
inline T* Realloc(T* pointer, size_t n);
3645
template <typename T>
3746
inline T* Malloc(size_t n);
@@ -41,6 +50,8 @@ inline T* Calloc(size_t n);
4150
// Shortcuts for char*.
4251
inline char* Malloc(size_t n) { return Malloc<char>(n); }
4352
inline char* Calloc(size_t n) { return Calloc<char>(n); }
53+
inline char* UncheckedMalloc(size_t n) { return UncheckedMalloc<char>(n); }
54+
inline char* UncheckedCalloc(size_t n) { return UncheckedCalloc<char>(n); }
4455

4556
#ifdef __GNUC__
4657
#define NO_RETURN __attribute__((noreturn))
@@ -306,7 +317,6 @@ class MaybeStackBuffer {
306317
buf_ = buf_st_;
307318
} else {
308319
buf_ = Malloc<T>(storage);
309-
CHECK_NE(buf_, nullptr);
310320
}
311321

312322
// Remember how much was allocated to check against that in SetLength().

test/cctest/util.cc

+16
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,19 @@ TEST(UtilTest, Calloc) {
105105
EXPECT_NE(nullptr, Calloc(0));
106106
EXPECT_NE(nullptr, Calloc(1));
107107
}
108+
109+
TEST(UtilTest, UncheckedMalloc) {
110+
using node::UncheckedMalloc;
111+
EXPECT_NE(nullptr, UncheckedMalloc<char>(0));
112+
EXPECT_NE(nullptr, UncheckedMalloc<char>(1));
113+
EXPECT_NE(nullptr, UncheckedMalloc(0));
114+
EXPECT_NE(nullptr, UncheckedMalloc(1));
115+
}
116+
117+
TEST(UtilTest, UncheckedCalloc) {
118+
using node::UncheckedCalloc;
119+
EXPECT_NE(nullptr, UncheckedCalloc<char>(0));
120+
EXPECT_NE(nullptr, UncheckedCalloc<char>(1));
121+
EXPECT_NE(nullptr, UncheckedCalloc(0));
122+
EXPECT_NE(nullptr, UncheckedCalloc(1));
123+
}

0 commit comments

Comments
 (0)