mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
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> Conflicts: src/node.cc
This commit is contained in:
1 parent
d0a92be798
commit
d1d1433b02
11 files changed
+67
-32
No files matched your search
+4
-4
@@ -54,7 +54,7 @@ class ExternString: public ResourceType {
|
||||
return scope.Escape(String::Empty(isolate));
|
||||
|
||||
TypeName* new_data =
|
||||
static_cast<TypeName*>(malloc(length * sizeof(*new_data)));
|
||||
static_cast<TypeName*>(node::Malloc(length * sizeof(*new_data)));
|
||||
if (new_data == nullptr) {
|
||||
return Local<String>();
|
||||
}
|
||||
@@ -624,7 +624,7 @@ Local<Value> StringBytes::Encode(Isolate* isolate,
|
||||
|
||||
case ASCII:
|
||||
if (contains_non_ascii(buf, buflen)) {
|
||||
char* out = static_cast<char*>(malloc(buflen));
|
||||
char* out = static_cast<char*>(node::Malloc(buflen));
|
||||
if (out == nullptr) {
|
||||
return Local<String>();
|
||||
}
|
||||
@@ -659,7 +659,7 @@ Local<Value> StringBytes::Encode(Isolate* isolate,
|
||||
|
||||
case BASE64: {
|
||||
size_t dlen = base64_encoded_size(buflen);
|
||||
char* dst = static_cast<char*>(malloc(dlen));
|
||||
char* dst = static_cast<char*>(node::Malloc(dlen));
|
||||
if (dst == nullptr) {
|
||||
return Local<String>();
|
||||
}
|
||||
@@ -678,7 +678,7 @@ Local<Value> StringBytes::Encode(Isolate* isolate,
|
||||
|
||||
case HEX: {
|
||||
size_t dlen = buflen * 2;
|
||||
char* dst = static_cast<char*>(malloc(dlen));
|
||||
char* dst = static_cast<char*>(node::Malloc(dlen));
|
||||
if (dst == nullptr) {
|
||||
return Local<String>();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user