summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2016-01-25 15:00:06 -0800
committerJames M Snell <jasnell@gmail.com>2016-03-16 08:34:02 -0700
commit85ab4a5f1281c4e1dd06450ac7bd3250326267fa (patch)
tree7a51edfc5d6efc231cf821d575c9ca4a26d0545f /src
parent90a5fc20be22b4278a01bc58acba0cb732da0140 (diff)
downloadandroid-node-v8-85ab4a5f1281c4e1dd06450ac7bd3250326267fa.tar.gz
android-node-v8-85ab4a5f1281c4e1dd06450ac7bd3250326267fa.tar.bz2
android-node-v8-85ab4a5f1281c4e1dd06450ac7bd3250326267fa.zip
buffer: add .from(), .alloc() and .allocUnsafe()
Several changes: * Soft-Deprecate Buffer() constructors * Add `Buffer.from()`, `Buffer.alloc()`, and `Buffer.allocUnsafe()` * Add `--zero-fill-buffers` command line option * Add byteOffset and length to `new Buffer(arrayBuffer)` constructor * buffer.fill('') previously had no effect, now zero-fills * Update the docs PR-URL: https://github.com/nodejs/node/pull/4682 Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Diffstat (limited to 'src')
-rw-r--r--src/node.cc8
-rw-r--r--src/node_buffer.cc27
-rw-r--r--src/node_buffer.h3
3 files changed, 33 insertions, 5 deletions
diff --git a/src/node.cc b/src/node.cc
index 976237efe2..679643ebf8 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -955,7 +955,9 @@ Local<Value> WinapiErrnoException(Isolate* isolate,
void* ArrayBufferAllocator::Allocate(size_t size) {
- if (env_ == nullptr || !env_->array_buffer_allocator_info()->no_zero_fill())
+ if (env_ == nullptr ||
+ !env_->array_buffer_allocator_info()->no_zero_fill() ||
+ zero_fill_all_buffers)
return calloc(size, 1);
env_->array_buffer_allocator_info()->reset_fill_flag();
return malloc(size);
@@ -3312,6 +3314,8 @@ static void PrintHelp() {
"snapshots\n"
" --prof-process process v8 profiler output generated\n"
" using --prof\n"
+ " --zero-fill-buffers automatically zero-fill all newly allocated\n"
+ " Buffer and SlowBuffer instances\n"
" --v8-options print v8 command line options\n"
#if HAVE_OPENSSL
" --tls-cipher-list=val use an alternative default TLS cipher list\n"
@@ -3455,6 +3459,8 @@ static void ParseArgs(int* argc,
} else if (strcmp(arg, "--prof-process") == 0) {
prof_process = true;
short_circuit = true;
+ } else if (strcmp(arg, "--zero-fill-buffers") == 0) {
+ zero_fill_all_buffers = true;
} else if (strcmp(arg, "--v8-options") == 0) {
new_v8_argv[new_v8_argc] = "--help";
new_v8_argc += 1;
diff --git a/src/node_buffer.cc b/src/node_buffer.cc
index aecc6874d8..6720f2c937 100644
--- a/src/node_buffer.cc
+++ b/src/node_buffer.cc
@@ -48,7 +48,14 @@
CHECK_NOT_OOB(end <= end_max); \
size_t length = end - start;
+#define BUFFER_MALLOC(length) \
+ zero_fill_all_buffers ? calloc(length, 1) : malloc(length)
+
namespace node {
+
+// if true, all Buffer and SlowBuffer instances will automatically zero-fill
+bool zero_fill_all_buffers = false;
+
namespace Buffer {
using v8::ArrayBuffer;
@@ -74,7 +81,6 @@ using v8::Uint8Array;
using v8::Value;
using v8::WeakCallbackInfo;
-
class CallbackInfo {
public:
static inline void Free(char* data, void* hint);
@@ -210,7 +216,7 @@ MaybeLocal<Object> New(Isolate* isolate,
// nullptr for zero-sized allocation requests. Normalize by always using
// a nullptr.
if (length > 0) {
- data = static_cast<char*>(malloc(length));
+ data = static_cast<char*>(BUFFER_MALLOC(length));
if (data == nullptr)
return Local<Object>();
@@ -256,7 +262,7 @@ MaybeLocal<Object> New(Environment* env, size_t length) {
void* data;
if (length > 0) {
- data = malloc(length);
+ data = BUFFER_MALLOC(length);
if (data == nullptr)
return Local<Object>();
} else {
@@ -419,7 +425,20 @@ void CreateFromArrayBuffer(const FunctionCallbackInfo<Value>& args) {
if (!args[0]->IsArrayBuffer())
return env->ThrowTypeError("argument is not an ArrayBuffer");
Local<ArrayBuffer> ab = args[0].As<ArrayBuffer>();
- Local<Uint8Array> ui = Uint8Array::New(ab, 0, ab->ByteLength());
+
+ size_t ab_length = ab->ByteLength();
+ size_t offset;
+ size_t max_length;
+
+ CHECK_NOT_OOB(ParseArrayIndex(args[1], 0, &offset));
+ CHECK_NOT_OOB(ParseArrayIndex(args[2], ab_length - offset, &max_length));
+
+ if (offset >= ab_length)
+ return env->ThrowRangeError("'offset' is out of bounds");
+ if (max_length > ab_length - offset)
+ return env->ThrowRangeError("'length' is out of bounds");
+
+ Local<Uint8Array> ui = Uint8Array::New(ab, offset, max_length);
Maybe<bool> mb =
ui->SetPrototype(env->context(), env->buffer_prototype_object());
if (!mb.FromMaybe(false))
diff --git a/src/node_buffer.h b/src/node_buffer.h
index 2bcf245f39..2ecfab33a3 100644
--- a/src/node_buffer.h
+++ b/src/node_buffer.h
@@ -5,6 +5,9 @@
#include "v8.h"
namespace node {
+
+extern bool zero_fill_all_buffers;
+
namespace Buffer {
static const unsigned int kMaxLength =