Skip to content

Commit 2f6cad8

Browse files
committed
Fix length check in ArrayBuffer.prototype.slice (#1211)
Fixes: #1210
1 parent c952c93 commit 2f6cad8

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

quickjs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54334,7 +54334,7 @@ static JSValue js_array_buffer_slice(JSContext *ctx,
5433454334
goto fail;
5433554335
}
5433654336
/* must test again because of side effects */
54337-
if (abuf->detached) {
54337+
if (abuf->detached || abuf->byte_length < start + new_len) {
5433854338
JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
5433954339
goto fail;
5434054340
}

tests/test_builtin.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ function test_typed_array()
586586
try {
587587
new TypedArray(); // extensible but not instantiable
588588
} catch (e) {
589+
assert(e instanceof TypeError);
589590
assert(/cannot be called/.test(e.message));
590591
caught = true;
591592
}
@@ -598,6 +599,25 @@ function test_typed_array()
598599
assert(a[0], 42);
599600
buffer.transfer();
600601
assert(a[0], undefined);
602+
603+
// https://github.com/quickjs-ng/quickjs/issues/1210
604+
var buffer = new ArrayBuffer(16, {maxByteLength: 16});
605+
var desc = Object.getOwnPropertyDescriptor(ArrayBuffer, Symbol.species);
606+
assert(typeof desc.get, "function");
607+
var get = function() {
608+
buffer.resize(1);
609+
return ArrayBuffer;
610+
};
611+
Object.defineProperty(ArrayBuffer, Symbol.species, {...desc, get});
612+
let ex;
613+
try {
614+
buffer.slice();
615+
} catch (ex_) {
616+
ex = ex_;
617+
}
618+
Object.defineProperty(ArrayBuffer, Symbol.species, desc); // restore
619+
assert(ex instanceof TypeError);
620+
assert("ArrayBuffer is detached", ex.message);
601621
}
602622

603623
function test_json()

0 commit comments

Comments
 (0)