-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core: normalize malloc, realloc #7564
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A future improvement is to replace Malloc calls like this one with a Calloc-like version that does overflow checks (but doesn't zero the memory.) |
||
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>(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -229,6 +229,32 @@ bool StringEqualNoCaseN(const char* a, const char* b, size_t length) { | |
return true; | ||
} | ||
|
||
// These should be used in our code as opposed to the native | ||
// versions as they abstract out some platform and or | ||
// compiler version specific functionality | ||
// 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. | ||
void* Realloc(void* pointer, size_t size) { | ||
if (size == 0) { | ||
free(pointer); | ||
return nullptr; | ||
} | ||
return realloc(pointer, size); | ||
} | ||
|
||
// as per spec realloc behaves like malloc if passed nullptr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you capitalize and punctuate the comment? |
||
void* Malloc(size_t size) { | ||
return Realloc(nullptr, size); | ||
} | ||
|
||
void* Calloc(size_t n, size_t size) { | ||
if ((n == 0) || (size == 0)) return nullptr; | ||
CHECK_GE(n * size, n); // Overflow guard. | ||
return calloc(n, size); | ||
} | ||
|
||
} // namespace node | ||
|
||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linter accepts this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It complained when it was on one line and passed with it this way !