From f86ad1693f5fd21cb645bbe6672a30bcb83229e1 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 27 Aug 2010 15:23:39 -0700 Subject: [PATCH] Safe Constructor: Buffer --- src/node_buffer.cc | 10 ++++++++++ test/simple/test-buffer.js | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index a809ae96d55..599c83b6ded 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -156,6 +156,16 @@ Buffer* Buffer::New(size_t size) { Handle Buffer::New(const Arguments &args) { HandleScope scope; + if (!args.IsConstructCall()) { + Local argv[10]; + for (int i = 0; i < MIN(args.Length(), 10); i++) { + argv[i] = args[i]; + } + Local instance = + constructor_template->GetFunction()->NewInstance(args.Length(), argv); + return scope.Close(instance); + } + Buffer *buffer; if (args[0]->IsInt32()) { // var buffer = new Buffer(1024); diff --git a/test/simple/test-buffer.js b/test/simple/test-buffer.js index c6e6cc5ec64..7a3f1549d91 100644 --- a/test/simple/test-buffer.js +++ b/test/simple/test-buffer.js @@ -4,7 +4,7 @@ assert = require("assert"); var Buffer = require('buffer').Buffer; -var b = new Buffer(1024); +var b = Buffer(1024); // safe constructor console.log("b.length == " + b.length); assert.strictEqual(1024, b.length);