From 5821f9bd0814beb7c9543bfbabbd23797b207be0 Mon Sep 17 00:00:00 2001 From: Vladimir Kurchatkin Date: Tue, 28 Oct 2014 15:32:07 +0300 Subject: [PATCH] smalloc: fix `copyOnto` optimization `copyOnto` is broken when one argument has 1 byte size and the other >1 byte. --- src/smalloc.cc | 2 +- test/simple/test-smalloc.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/smalloc.cc b/src/smalloc.cc index 41298030ce1c..c7913d90ff84 100644 --- a/src/smalloc.cc +++ b/src/smalloc.cc @@ -207,7 +207,7 @@ void CopyOnto(const FunctionCallbackInfo& args) { size_t dest_size = ExternalArraySize(dest_type); // optimization for Uint8 arrays (i.e. Buffers) - if (source_size != 1 && dest_size != 1) { + if (source_size != 1 || dest_size != 1) { if (source_size == 0) return env->ThrowTypeError("unknown source external array type"); if (dest_size == 0) diff --git a/test/simple/test-smalloc.js b/test/simple/test-smalloc.js index be7e7ac32639..ea6f7bf4ad14 100644 --- a/test/simple/test-smalloc.js +++ b/test/simple/test-smalloc.js @@ -161,6 +161,20 @@ if (os.endianness() === 'LE') { copyOnto(c, 0, b, 0, 2); assert.equal(b[0], 0.1); +var b = alloc(1, Types.Uint16); +var c = alloc(2, Types.Uint8); +c[0] = c[1] = 0xff; +copyOnto(c, 0, b, 0, 2); +assert.equal(b[0], 0xffff); + +var b = alloc(2, Types.Uint8); +var c = alloc(1, Types.Uint16); +c[0] = 0xffff; +copyOnto(c, 0, b, 0, 1); +assert.equal(b[0], 0xff); +assert.equal(b[1], 0xff); + + // verify checking external if has external memory