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

Commit 849fcde

Browse files
vkurchatkintrevnorris
authored andcommitted
smalloc: fix copyOnto optimization
copyOnto is broken when one argument has 1 byte size and the other > 1 byte. PR-URL: #8637 Reviewed-by: Trevor Norris <[email protected]>
1 parent c2b4f48 commit 849fcde

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/smalloc.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ void CopyOnto(const FunctionCallbackInfo<Value>& args) {
207207
size_t dest_size = ExternalArraySize(dest_type);
208208

209209
// optimization for Uint8 arrays (i.e. Buffers)
210-
if (source_size != 1 && dest_size != 1) {
210+
if (source_size != 1 || dest_size != 1) {
211211
if (source_size == 0)
212212
return env->ThrowTypeError("unknown source external array type");
213213
if (dest_size == 0)

test/simple/test-smalloc.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,20 @@ if (os.endianness() === 'LE') {
161161
copyOnto(c, 0, b, 0, 2);
162162
assert.equal(b[0], 0.1);
163163

164+
var b = alloc(1, Types.Uint16);
165+
var c = alloc(2, Types.Uint8);
166+
c[0] = c[1] = 0xff;
167+
copyOnto(c, 0, b, 0, 2);
168+
assert.equal(b[0], 0xffff);
169+
170+
var b = alloc(2, Types.Uint8);
171+
var c = alloc(1, Types.Uint16);
172+
c[0] = 0xffff;
173+
copyOnto(c, 0, b, 0, 1);
174+
assert.equal(b[0], 0xff);
175+
assert.equal(b[1], 0xff);
176+
177+
164178

165179
// verify checking external if has external memory
166180

0 commit comments

Comments
 (0)