From 2034137385e8b77cf09a379efb0700f67b57e658 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 19 Mar 2015 13:35:47 +0100 Subject: [PATCH] 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: https://github.com/iojs/io.js/pull/1205 Reviewed-By: Trevor Norris --- src/smalloc.cc | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/smalloc.cc b/src/smalloc.cc index 97ae293b4bcd80..57ea97f2aca20e 100644 --- a/src/smalloc.cc +++ b/src/smalloc.cc @@ -314,8 +314,10 @@ void Alloc(Environment* env, char* data = static_cast(malloc(length)); if (data == nullptr) { - FatalError("node::smalloc::Alloc(v8::Handle, size_t," - " v8::ExternalArrayType)", "Out Of Memory"); + FatalError("node::smalloc::Alloc(node::Environment*, " + " v8::Handle, size_t, v8::ExternalArrayType)", + "Out Of Memory"); + UNREACHABLE(); } Alloc(env, obj, data, length, type); @@ -394,7 +396,14 @@ void Alloc(Environment* env, length *= type_size; - char* data = new char[length]; + char* data = static_cast(malloc(length)); + if (data == nullptr) { + FatalError("node::smalloc::Alloc(node::Environment*, " + " v8::Handle, size_t, node::FreeCallback," + " void*, v8::ExternalArrayType)", "Out Of Memory"); + UNREACHABLE(); + } + Alloc(env, obj, data, length, fn, hint, type); }