crypto: don't mix new[] and free()
authorBen Noordhuis <info@bnoordhuis.nl>
Fri, 16 Aug 2013 14:36:21 +0000 (16:36 +0200)
committerBen Noordhuis <info@bnoordhuis.nl>
Fri, 16 Aug 2013 14:47:08 +0000 (16:47 +0200)
RandomBytes() allocated memory with new[] which was then handed off to
Buffer::Use() which eventually releases it again with free().

Mixing the two is technically a violation of the spec and besides, it's
generally frowned upon.

src/node_crypto.cc

index 0239e74..665010b 100644 (file)
@@ -81,6 +81,7 @@ using v8::Object;
 using v8::Persistent;
 using v8::String;
 using v8::ThrowException;
+using v8::V8;
 using v8::Value;
 
 
@@ -3463,8 +3464,14 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
 
   RandomBytesRequest* req = new RandomBytesRequest();
   req->error_ = 0;
-  req->data_ = new char[size];
   req->size_ = size;
+  req->data_ = static_cast<char*>(malloc(size));
+
+  if (req->data_ == NULL) {
+    delete req;
+    V8::LowMemoryNotification();
+    return ThrowError("Out of memory");
+  }
 
   if (args[1]->IsFunction()) {
     Local<Object> obj = Object::New();