Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
smalloc: don't allow to dispose typed arrays
Browse files Browse the repository at this point in the history
PR-URL: #8743
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
vkurchatkin authored and trevnorris committed Nov 26, 2014
1 parent e0a0e91 commit bf3e0f4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/smalloc.js
Expand Up @@ -86,6 +86,8 @@ function dispose(obj) {
throw new TypeError('obj must be an Object');
if (util.isBuffer(obj))
throw new TypeError('obj cannot be a Buffer');
if (smalloc.isTypedArray(obj))
throw new TypeError('obj cannot be a typed array');
if (!smalloc.hasExternalData(obj))
throw new Error('obj has no external array data');

Expand Down
4 changes: 4 additions & 0 deletions src/smalloc.cc
Expand Up @@ -446,6 +446,9 @@ bool HasExternalData(Environment* env, Local<Object> obj) {
return obj->HasIndexedPropertiesInExternalArrayData();
}

void IsTypedArray(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(args[0]->IsTypedArray());
}

void AllocTruncate(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args.GetIsolate());
Expand Down Expand Up @@ -547,6 +550,7 @@ void Initialize(Handle<Object> exports,
NODE_SET_METHOD(exports, "truncate", AllocTruncate);

NODE_SET_METHOD(exports, "hasExternalData", HasExternalData);
NODE_SET_METHOD(exports, "isTypedArray", IsTypedArray);

exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kMaxLength"),
Uint32::NewFromUnsigned(env->isolate(), kMaxLength));
Expand Down
4 changes: 4 additions & 0 deletions test/simple/test-smalloc.js
Expand Up @@ -323,6 +323,10 @@ assert.throws(function() {
smalloc.dispose(new Buffer());
});

assert.throws(function() {
smalloc.dispose(new Uint8Array(new ArrayBuffer(1)));
});

assert.throws(function() {
smalloc.dispose({});
});

0 comments on commit bf3e0f4

Please sign in to comment.