Skip to content

Commit cf201e9

Browse files
author
zhang.yuping
committed
fix crash in js_typed_array_slice caused by memory overlap
Use memmove instead of memcpy for safe. Fixes: #378
1 parent 4fb2e38 commit cf201e9

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

quickjs.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -50327,9 +50327,9 @@ static JSValue js_typed_array_slice(JSContext *ctx, JSValue this_val,
5032750327
if (p1 != NULL && p->class_id == p1->class_id &&
5032850328
typed_array_get_length(ctx, p1) >= count &&
5032950329
typed_array_get_length(ctx, p) >= start + count) {
50330-
memcpy(p1->u.array.u.uint8_ptr,
50331-
p->u.array.u.uint8_ptr + (start << shift),
50332-
count << shift);
50330+
memmove(p1->u.array.u.uint8_ptr,
50331+
p->u.array.u.uint8_ptr + (start << shift),
50332+
count << shift);
5033350333
} else {
5033450334
for (n = 0; n < count; n++) {
5033550335
val = JS_GetPropertyValue(ctx, this_val, js_int32(start + n));

tests/test_builtin.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ function test_eval()
516516

517517
function test_typed_array()
518518
{
519-
var buffer, a, i, str;
519+
var buffer, a, i, str, b;
520520

521521
a = new Uint8Array(4);
522522
assert(a.length, 4);
@@ -569,6 +569,17 @@ function test_typed_array()
569569
assert(a.toString(), "1,2,3,4");
570570
a.set([10, 11], 2);
571571
assert(a.toString(), "1,2,10,11");
572+
573+
a = new Uint8Array(buffer, 0, 4);
574+
a.constructor = {
575+
[Symbol.species]: function (len) {
576+
return new Uint8Array(buffer, 1, len);
577+
},
578+
};
579+
b = a.slice();
580+
assert(a.buffer, b.buffer);
581+
assert(a.toString(), "0,0,0,255");
582+
assert(b.toString(), "0,0,255,255");
572583
}
573584

574585
function test_json()

0 commit comments

Comments
 (0)