Skip to content

Commit

Permalink
smalloc: don't mix malloc() and new char[]
Browse files Browse the repository at this point in the history
It's technically undefined behavior to mix malloc with delete[] and
new char[] with free().  smalloc was using new char[] in one place and
malloc() in another but in both cases the memory was freed with free().

PR-URL: #1205
Reviewed-By: Trevor Norris <[email protected]>
  • Loading branch information
bnoordhuis committed Mar 19, 2015
1 parent 4655521 commit 2034137
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/smalloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,10 @@ void Alloc(Environment* env,

char* data = static_cast<char*>(malloc(length));
if (data == nullptr) {
FatalError("node::smalloc::Alloc(v8::Handle<v8::Object>, size_t,"
" v8::ExternalArrayType)", "Out Of Memory");
FatalError("node::smalloc::Alloc(node::Environment*, "
" v8::Handle<v8::Object>, size_t, v8::ExternalArrayType)",
"Out Of Memory");
UNREACHABLE();
}

Alloc(env, obj, data, length, type);
Expand Down Expand Up @@ -394,7 +396,14 @@ void Alloc(Environment* env,

length *= type_size;

char* data = new char[length];
char* data = static_cast<char*>(malloc(length));
if (data == nullptr) {
FatalError("node::smalloc::Alloc(node::Environment*, "
" v8::Handle<v8::Object>, size_t, node::FreeCallback,"
" void*, v8::ExternalArrayType)", "Out Of Memory");
UNREACHABLE();
}

Alloc(env, obj, data, length, fn, hint, type);
}

Expand Down

0 comments on commit 2034137

Please sign in to comment.