Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
e304c42
test: add unhandled rejection guard
babygoat Nov 23, 2017
887dcb9
test: remove literals that obscure assert messages
Trott Dec 13, 2017
bba1442
n-api: fix memory leak in napi_async_destroy()
Dec 17, 2017
bcbb7af
test: remove ambiguous error messages from test_error
nadrane Dec 21, 2017
6bf3769
doc: updates examples to use NULL
mhdawson Jan 5, 2018
f81330f
n-api: throw RangeError in napi_create_dataview() with invalid range
romandev Dec 26, 2017
6d23c24
n-api: expose n-api version in process.versions
mhdawson Jan 9, 2018
40ce083
doc: napi: fix unbalanced emphasis
ofrobots Jan 12, 2018
621117e
doc: napi: make header style consistent
ofrobots Jan 12, 2018
111678b
n-api: throw RangeError napi_create_typedarray()
romandev Jan 8, 2018
94618a5
test: fixed typos in napi test
furstenheim Jan 14, 2018
375d043
doc: remove uannecessary Require
mhdawson Jan 16, 2018
f7fb0c3
timers: allow Immediates to be unrefed
apapirovski Jan 13, 2018
23fd460
test: refactor addons-napi/test_exception/test.js
Trott Jan 24, 2018
021e4a4
n-api: change assert ok check to notStrictEqual.
nbdaaron Jan 27, 2018
bf96235
test: show pending exception error in napi tests
blairwilcox Jan 27, 2018
17862cd
n-api: implement wrapping using private properties
Jan 23, 2018
29bbf0c
n-api: wrap control flow macro in do/while
bnoordhuis Feb 2, 2018
6619a1b
doc: remove usage of you in n-api doc
mhdawson Feb 2, 2018
8923ad5
doc: small typo in n-api.md
iSkore Feb 4, 2018
ed8ac32
n-api: add methods to open/close callback scope
mhdawson Dec 13, 2017
01b28fa
n-api: remove extra reference from test
Feb 3, 2018
d2f0672
doc: fix typo in n-api.md
vsemozhetbyt Feb 6, 2018
9c20b87
test: improve error message output
bshankar Feb 1, 2018
ef50323
test: convert new tests to use error types
jackhorton Feb 5, 2018
c4592e6
doc: fix exporting a function example
Feb 8, 2018
5ab4d31
doc: mark NAPI_AUTO_LENGTH as code
tniessen Feb 10, 2018
a1df466
test: remove unnecessary timer
cjihrig Feb 11, 2018
eb85cd8
n-api: fix object test
Feb 27, 2018
2b3207d
doc: fix n-api asynchronous threading docs
ebickle Mar 1, 2018
7a80a8d
n-api: update reference test
Mar 2, 2018
de2d3a6
n-api: update documentation
Mar 1, 2018
a92796e
n-api: resolve promise in test
Mar 8, 2018
2cfd4fe
n-api,test: add a new.target test to addons-napi
boingoing Jan 17, 2018
4a0fc8a
n-api,test: add int64 bounds tests
kfarnung Mar 12, 2018
ff968b7
doc: fix typos on n-api
shama Mar 16, 2018
01790d4
n-api: add missing exception checking
mhdawson Mar 15, 2018
a19b829
n-api: separate out async_hooks test
Mar 16, 2018
9b1b810
n-api: add napi_fatal_exception
mafintosh Mar 14, 2018
3a50835
n-api: re-write test_make_callback
Mar 19, 2018
b25dec8
doc: Add a missing comma
leaves4j Mar 23, 2018
c33daa4
n-api: bump version of n-api supported
mhdawson Mar 20, 2018
65b06de
n-api: ensure in-module exceptions are propagated
Mar 22, 2018
e3a2737
n-api: back up env before finalize
Apr 1, 2018
d73e802
n-api: add more `int64_t` tests
kfarnung Mar 16, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
n-api: throw RangeError napi_create_typedarray()
According to the ECMA spec, we should throw a RangeError in the
following cases:
  - `(length * elementSize) + offset` > the size of the array passed in
  - `offset % elementSize` != `0`

In the current implementation, this check was omitted. So, the following
code will cause a crash.
  ```
  napi_create_typedarray(env, napi_uint16_array, 2 /* length */,
                         buffer, 1 /* byte_offset */, &output_array);
  ```

This change fixes the problem and write some related tests.

Refs:
https://tc39.github.io/ecma262/#sec-typedarray-buffer-byteoffset-length

PR-URL: #18037
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
romandev authored and Gabriel Schulhof committed Apr 12, 2018
commit 111678bd0fc3261bf43f6f2c95ffe71b5744ca5f
12 changes: 12 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,18 @@ While using `N-API`, `Constructor.prototype` was not an object.
While calling `napi_create_dataview()`, a given `offset` was outside the bounds
of the dataview or `offset + length` was larger than a length of given `buffer`.

<a id="ERR_NAPI_INVALID_TYPEDARRAY_ALIGNMENT"></a>
### ERR_NAPI_INVALID_TYPEDARRAY_ALIGNMENT

While calling `napi_create_typedarray()`, the provided `offset` was not a
multiple of the element size.

<a id="ERR_NAPI_INVALID_TYPEDARRAY_LENGTH"></a>
### ERR_NAPI_INVALID_TYPEDARRAY_LENGTH

While calling `napi_create_typedarray()`, `(length * size_of_element) +
byte_offset` was larger than the length of given `buffer`.

<a id="ERR_NO_ICU"></a>
### ERR_NO_ICU

Expand Down
3 changes: 3 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ E('ERR_NAPI_CONS_PROTOTYPE_OBJECT', 'Constructor.prototype must be an object');
E('ERR_NAPI_INVALID_DATAVIEW_ARGS',
'byte_offset + byte_length should be less than or eqaul to the size in ' +
'bytes of the array passed in');
E('ERR_NAPI_INVALID_TYPEDARRAY_ALIGNMENT', 'start offset of %s should be a ' +
'multiple of %s');
E('ERR_NAPI_INVALID_TYPEDARRAY_LENGTH', 'Invalid typed array length');
E('ERR_NO_CRYPTO', 'Node.js is not compiled with OpenSSL crypto support');
E('ERR_NO_ICU', '%s is not supported on Node.js compiled without ICU');
E('ERR_PARSE_HISTORY_DATA', 'Could not parse history data in %s');
Expand Down
51 changes: 42 additions & 9 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,30 @@ struct napi_env__ {
(!try_catch.HasCaught() ? napi_ok \
: napi_set_last_error((env), napi_pending_exception))

#define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message) \
do { \
if (!(condition)) { \
napi_throw_range_error((env), (error), (message)); \
return napi_set_last_error((env), napi_generic_failure); \
} \
} while (0)

#define CREATE_TYPED_ARRAY( \
env, type, size_of_element, buffer, byte_offset, length, out) \
do { \
if ((size_of_element) > 1) { \
THROW_RANGE_ERROR_IF_FALSE( \
(env), (byte_offset) % (size_of_element) == 0, \
"ERR_NAPI_INVALID_TYPEDARRAY_ALIGNMENT", \
"start offset of "#type" should be a multiple of "#size_of_element); \
} \
THROW_RANGE_ERROR_IF_FALSE((env), (length) * (size_of_element) + \
(byte_offset) <= buffer->ByteLength(), \
"ERR_NAPI_INVALID_TYPEDARRAY_LENGTH", \
"Invalid typed array length"); \
(out) = v8::type::New((buffer), (byte_offset), (length)); \
} while (0)

namespace {
namespace v8impl {

Expand Down Expand Up @@ -3063,31 +3087,40 @@ napi_status napi_create_typedarray(napi_env env,

switch (type) {
case napi_int8_array:
typedArray = v8::Int8Array::New(buffer, byte_offset, length);
CREATE_TYPED_ARRAY(
env, Int8Array, 1, buffer, byte_offset, length, typedArray);
break;
case napi_uint8_array:
typedArray = v8::Uint8Array::New(buffer, byte_offset, length);
CREATE_TYPED_ARRAY(
env, Uint8Array, 1, buffer, byte_offset, length, typedArray);
break;
case napi_uint8_clamped_array:
typedArray = v8::Uint8ClampedArray::New(buffer, byte_offset, length);
CREATE_TYPED_ARRAY(
env, Uint8ClampedArray, 1, buffer, byte_offset, length, typedArray);
break;
case napi_int16_array:
typedArray = v8::Int16Array::New(buffer, byte_offset, length);
CREATE_TYPED_ARRAY(
env, Int16Array, 2, buffer, byte_offset, length, typedArray);
break;
case napi_uint16_array:
typedArray = v8::Uint16Array::New(buffer, byte_offset, length);
CREATE_TYPED_ARRAY(
env, Uint16Array, 2, buffer, byte_offset, length, typedArray);
break;
case napi_int32_array:
typedArray = v8::Int32Array::New(buffer, byte_offset, length);
CREATE_TYPED_ARRAY(
env, Int32Array, 4, buffer, byte_offset, length, typedArray);
break;
case napi_uint32_array:
typedArray = v8::Uint32Array::New(buffer, byte_offset, length);
CREATE_TYPED_ARRAY(
env, Uint32Array, 4, buffer, byte_offset, length, typedArray);
break;
case napi_float32_array:
typedArray = v8::Float32Array::New(buffer, byte_offset, length);
CREATE_TYPED_ARRAY(
env, Float32Array, 4, buffer, byte_offset, length, typedArray);
break;
case napi_float64_array:
typedArray = v8::Float64Array::New(buffer, byte_offset, length);
CREATE_TYPED_ARRAY(
env, Float64Array, 8, buffer, byte_offset, length, typedArray);
break;
default:
return napi_set_last_error(env, napi_invalid_arg);
Expand Down
18 changes: 18 additions & 0 deletions test/addons-napi/test_typedarray/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,21 @@ arrayTypes.forEach((currentType) => {
assert.notStrictEqual(theArray, template);
assert.strictEqual(theArray.buffer, buffer);
});

arrayTypes.forEach((currentType) => {
const template = Reflect.construct(currentType, buffer);
assert.throws(() => {
test_typedarray.CreateTypedArray(template, buffer, 0, 136);
}, /Invalid typed array length/);
});

const nonByteArrayTypes = [ Int16Array, Uint16Array, Int32Array, Uint32Array,
Float32Array, Float64Array ];
nonByteArrayTypes.forEach((currentType) => {
const template = Reflect.construct(currentType, buffer);
assert.throws(() => {
test_typedarray.CreateTypedArray(template, buffer,
currentType.BYTES_PER_ELEMENT + 1, 1);
console.log(`start of offset ${currentType}`);
}, /start offset of/);
});
28 changes: 25 additions & 3 deletions test/addons-napi/test_typedarray/test_typedarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ napi_value External(napi_env env, napi_callback_info info) {
}

napi_value CreateTypedArray(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
size_t argc = 4;
napi_value args[4];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc == 2, "Wrong number of arguments");
NAPI_ASSERT(env, argc == 2 || argc == 4, "Wrong number of arguments");

napi_value input_array = args[0];
napi_valuetype valuetype0;
Expand Down Expand Up @@ -136,6 +136,28 @@ napi_value CreateTypedArray(napi_env env, napi_callback_info info) {
NAPI_CALL(env, napi_get_typedarray_info(
env, input_array, &type, &length, NULL, &in_array_buffer, &byte_offset));

if (argc == 4) {
napi_valuetype valuetype2;
NAPI_CALL(env, napi_typeof(env, args[2], &valuetype2));

NAPI_ASSERT(env, valuetype2 == napi_number,
"Wrong type of arguments. Expects a number as third argument.");

uint32_t uint32_length;
NAPI_CALL(env, napi_get_value_uint32(env, args[2], &uint32_length));
length = uint32_length;

napi_valuetype valuetype3;
NAPI_CALL(env, napi_typeof(env, args[3], &valuetype3));

NAPI_ASSERT(env, valuetype3 == napi_number,
"Wrong type of arguments. Expects a number as third argument.");

uint32_t uint32_byte_offset;
NAPI_CALL(env, napi_get_value_uint32(env, args[3], &uint32_byte_offset));
byte_offset = uint32_byte_offset;
}

napi_value output_array;
NAPI_CALL(env, napi_create_typedarray(
env, type, length, input_buffer, byte_offset, &output_array));
Expand Down