Skip to content

Commit 2034137

Browse files
committed
smalloc: don't mix malloc() and new char[]
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]>
1 parent 4655521 commit 2034137

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/smalloc.cc

+12-3
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,10 @@ void Alloc(Environment* env,
314314

315315
char* data = static_cast<char*>(malloc(length));
316316
if (data == nullptr) {
317-
FatalError("node::smalloc::Alloc(v8::Handle<v8::Object>, size_t,"
318-
" v8::ExternalArrayType)", "Out Of Memory");
317+
FatalError("node::smalloc::Alloc(node::Environment*, "
318+
" v8::Handle<v8::Object>, size_t, v8::ExternalArrayType)",
319+
"Out Of Memory");
320+
UNREACHABLE();
319321
}
320322

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

395397
length *= type_size;
396398

397-
char* data = new char[length];
399+
char* data = static_cast<char*>(malloc(length));
400+
if (data == nullptr) {
401+
FatalError("node::smalloc::Alloc(node::Environment*, "
402+
" v8::Handle<v8::Object>, size_t, node::FreeCallback,"
403+
" void*, v8::ExternalArrayType)", "Out Of Memory");
404+
UNREACHABLE();
405+
}
406+
398407
Alloc(env, obj, data, length, fn, hint, type);
399408
}
400409

0 commit comments

Comments
 (0)