From a00ccb0fb9eb716925058b0a20fcec9251de3309 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Thu, 1 Sep 2016 18:14:02 -0400 Subject: 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 Reviewed-By: Anna Henningsen --- src/node_buffer.cc | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'src/node_buffer.cc') 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 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(BUFFER_MALLOC(length)); @@ -282,7 +278,7 @@ MaybeLocal New(Isolate* isolate, free(data); data = nullptr; } else if (actual < length) { - data = static_cast(realloc(data, actual)); + data = static_cast(node::Realloc(data, actual)); CHECK_NE(data, nullptr); } } @@ -361,7 +357,7 @@ MaybeLocal 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(); memcpy(new_data, data, length); @@ -1083,7 +1079,7 @@ void IndexOfString(const FunctionCallbackInfo& args) { offset, is_forward); } else if (enc == LATIN1) { - uint8_t* needle_data = static_cast(malloc(needle_length)); + uint8_t* needle_data = static_cast(node::Malloc(needle_length)); if (needle_data == nullptr) { return args.GetReturnValue().Set(-1); } -- cgit v1.2.3