diff options
author | Michael Dawson <michael_dawson@ca.ibm.com> | 2016-09-01 18:14:02 -0400 |
---|---|---|
committer | Michael Dawson <michael_dawson@ca.ibm.com> | 2016-09-06 09:39:06 -0400 |
commit | a00ccb0fb9eb716925058b0a20fcec9251de3309 (patch) | |
tree | 91c1aa556e0519d73c4b04238a5cf02f67342886 /src/node_buffer.cc | |
parent | a290ddfdc9cd9453886d433060c5132095d916c4 (diff) | |
download | android-node-v8-a00ccb0fb9eb716925058b0a20fcec9251de3309.tar.gz android-node-v8-a00ccb0fb9eb716925058b0a20fcec9251de3309.tar.bz2 android-node-v8-a00ccb0fb9eb716925058b0a20fcec9251de3309.zip |
src: normalize malloc, realloc
malloc(0) and realloc(ptr, 0) have implementation-defined behavior in
that the standard allows them to either return a unique pointer or a
nullptr for zero-sized allocation requests. Normalize by always using
a nullptr.
- Introduce node::malloc, node::realloc and node::calloc that should
be used throught our source.
- Update all existing node source files to use the new functions
instead of the native allocation functions.
Fixes: https://github.com/nodejs/node/issues/7549
PR-URL: https://github.com/nodejs/node/pull/7564
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'src/node_buffer.cc')
-rw-r--r-- | src/node_buffer.cc | 12 |
1 files changed, 4 insertions, 8 deletions
diff --git a/src/node_buffer.cc b/src/node_buffer.cc index ad06dd0233..4baa8d9ac6 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -50,7 +50,7 @@ size_t length = end - start; #define BUFFER_MALLOC(length) \ - zero_fill_all_buffers ? calloc(length, 1) : malloc(length) + zero_fill_all_buffers ? node::Calloc(length, 1) : node::Malloc(length) #if defined(__GNUC__) || defined(__clang__) #define BSWAP_INTRINSIC_2(x) __builtin_bswap16(x) @@ -265,10 +265,6 @@ MaybeLocal<Object> New(Isolate* isolate, size_t actual = 0; char* data = nullptr; - // malloc(0) and realloc(ptr, 0) have implementation-defined behavior in - // that the standard allows them to either return a unique pointer or a - // nullptr for zero-sized allocation requests. Normalize by always using - // a nullptr. if (length > 0) { data = static_cast<char*>(BUFFER_MALLOC(length)); @@ -282,7 +278,7 @@ MaybeLocal<Object> New(Isolate* isolate, free(data); data = nullptr; } else if (actual < length) { - data = static_cast<char*>(realloc(data, actual)); + data = static_cast<char*>(node::Realloc(data, actual)); CHECK_NE(data, nullptr); } } @@ -361,7 +357,7 @@ MaybeLocal<Object> Copy(Environment* env, const char* data, size_t length) { void* new_data; if (length > 0) { CHECK_NE(data, nullptr); - new_data = malloc(length); + new_data = node::Malloc(length); if (new_data == nullptr) return Local<Object>(); memcpy(new_data, data, length); @@ -1083,7 +1079,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) { offset, is_forward); } else if (enc == LATIN1) { - uint8_t* needle_data = static_cast<uint8_t*>(malloc(needle_length)); + uint8_t* needle_data = static_cast<uint8_t*>(node::Malloc(needle_length)); if (needle_data == nullptr) { return args.GetReturnValue().Set(-1); } |