Skip to content

Commit bf3e0f4

Browse files
vkurchatkintrevnorris
authored andcommitted
smalloc: don't allow to dispose typed arrays
PR-URL: nodejs#8743 Reviewed-by: Trevor Norris <[email protected]>
1 parent e0a0e91 commit bf3e0f4

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

lib/smalloc.js

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ function dispose(obj) {
8686
throw new TypeError('obj must be an Object');
8787
if (util.isBuffer(obj))
8888
throw new TypeError('obj cannot be a Buffer');
89+
if (smalloc.isTypedArray(obj))
90+
throw new TypeError('obj cannot be a typed array');
8991
if (!smalloc.hasExternalData(obj))
9092
throw new Error('obj has no external array data');
9193

src/smalloc.cc

+4
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,9 @@ bool HasExternalData(Environment* env, Local<Object> obj) {
446446
return obj->HasIndexedPropertiesInExternalArrayData();
447447
}
448448

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

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

549552
NODE_SET_METHOD(exports, "hasExternalData", HasExternalData);
553+
NODE_SET_METHOD(exports, "isTypedArray", IsTypedArray);
550554

551555
exports->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kMaxLength"),
552556
Uint32::NewFromUnsigned(env->isolate(), kMaxLength));

test/simple/test-smalloc.js

+4
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ assert.throws(function() {
323323
smalloc.dispose(new Buffer());
324324
});
325325

326+
assert.throws(function() {
327+
smalloc.dispose(new Uint8Array(new ArrayBuffer(1)));
328+
});
329+
326330
assert.throws(function() {
327331
smalloc.dispose({});
328332
});

0 commit comments

Comments
 (0)