Skip to content

Commit bb99f31

Browse files
romandevMylesBorins
authored andcommittedApr 16, 2018
n-api: throw RangeError in napi_create_dataview() with invalid range
The API is required that `byte_length + byte_offset` is less than or equal to the size in bytes of the array passed in. If not, a RangeError exception is raised[1]. [1] https://nodejs.org/api/n-api.html#n_api_napi_create_dataview Backport-PR-URL: #19447 PR-URL: #17869 Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 3d8e1aa commit bb99f31

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed
 

‎src/node_api.cc

+8
Original file line numberDiff line numberDiff line change
@@ -3264,6 +3264,14 @@ napi_status napi_create_dataview(napi_env env,
32643264
RETURN_STATUS_IF_FALSE(env, value->IsArrayBuffer(), napi_invalid_arg);
32653265

32663266
v8::Local<v8::ArrayBuffer> buffer = value.As<v8::ArrayBuffer>();
3267+
if (byte_length + byte_offset > buffer->ByteLength()) {
3268+
napi_throw_range_error(
3269+
env,
3270+
"ERR_NAPI_INVALID_DATAVIEW_ARGS",
3271+
"byte_offset + byte_length should be less than or "
3272+
"equal to the size in bytes of the array passed in");
3273+
return napi_set_last_error(env, napi_pending_exception);
3274+
}
32673275
v8::Local<v8::DataView> DataView = v8::DataView::New(buffer, byte_offset,
32683276
byte_length);
32693277

‎test/addons-napi/test_dataview/test.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@ const assert = require('assert');
55
// Testing api calls for arrays
66
const test_dataview = require(`./build/${common.buildType}/test_dataview`);
77

8-
//create dataview
9-
const buffer = new ArrayBuffer(128);
10-
const template = Reflect.construct(DataView, [buffer]);
8+
// Test for creating dataview
9+
{
10+
const buffer = new ArrayBuffer(128);
11+
const template = Reflect.construct(DataView, [buffer]);
1112

12-
const theDataview = test_dataview.CreateDataView(template);
13-
assert.ok(theDataview instanceof DataView,
14-
`Expect ${theDataview} to be a DataView`);
13+
const theDataview = test_dataview.CreateDataViewFromJSDataView(template);
14+
assert.ok(theDataview instanceof DataView,
15+
`Expect ${theDataview} to be a DataView`);
16+
}
17+
18+
// Test for creating dataview with invalid range
19+
{
20+
const buffer = new ArrayBuffer(128);
21+
assert.throws(() => {
22+
test_dataview.CreateDataView(buffer, 10, 200);
23+
}, RangeError);
24+
}

‎test/addons-napi/test_dataview/test_dataview.c

+51-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,53 @@
33
#include "../common.h"
44

55
napi_value CreateDataView(napi_env env, napi_callback_info info) {
6+
size_t argc = 3;
7+
napi_value args [3];
8+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
9+
10+
NAPI_ASSERT(env, argc == 3, "Wrong number of arguments");
11+
12+
napi_valuetype valuetype0;
13+
napi_value arraybuffer = args[0];
14+
15+
NAPI_CALL(env, napi_typeof(env, arraybuffer, &valuetype0));
16+
NAPI_ASSERT(env, valuetype0 == napi_object,
17+
"Wrong type of arguments. Expects a ArrayBuffer as the first "
18+
"argument.");
19+
20+
bool is_arraybuffer;
21+
NAPI_CALL(env, napi_is_arraybuffer(env, arraybuffer, &is_arraybuffer));
22+
NAPI_ASSERT(env, is_arraybuffer,
23+
"Wrong type of arguments. Expects a ArrayBuffer as the first "
24+
"argument.");
25+
26+
napi_valuetype valuetype1;
27+
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
28+
29+
NAPI_ASSERT(env, valuetype1 == napi_number,
30+
"Wrong type of arguments. Expects a number as second argument.");
31+
32+
size_t byte_offset = 0;
33+
NAPI_CALL(env, napi_get_value_uint32(env, args[1], (uint32_t*)(&byte_offset)));
34+
35+
napi_valuetype valuetype2;
36+
NAPI_CALL(env, napi_typeof(env, args[2], &valuetype2));
37+
38+
NAPI_ASSERT(env, valuetype2 == napi_number,
39+
"Wrong type of arguments. Expects a number as third argument.");
40+
41+
size_t length = 0;
42+
NAPI_CALL(env, napi_get_value_uint32(env, args[2], (uint32_t*)(&length)));
43+
44+
napi_value output_dataview;
45+
NAPI_CALL(env,
46+
napi_create_dataview(env, length, arraybuffer,
47+
byte_offset, &output_dataview));
48+
49+
return output_dataview;
50+
}
51+
52+
napi_value CreateDataViewFromJSDataView(napi_env env, napi_callback_info info) {
653
size_t argc = 1;
754
napi_value args [1];
855
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
@@ -34,12 +81,15 @@ napi_value CreateDataView(napi_env env, napi_callback_info info) {
3481
napi_create_dataview(env, length, buffer,
3582
byte_offset, &output_dataview));
3683

84+
3785
return output_dataview;
3886
}
3987

4088
napi_value Init(napi_env env, napi_value exports) {
4189
napi_property_descriptor descriptors[] = {
42-
DECLARE_NAPI_PROPERTY("CreateDataView", CreateDataView)
90+
DECLARE_NAPI_PROPERTY("CreateDataView", CreateDataView),
91+
DECLARE_NAPI_PROPERTY("CreateDataViewFromJSDataView",
92+
CreateDataViewFromJSDataView)
4393
};
4494

4595
NAPI_CALL(env, napi_define_properties(

0 commit comments

Comments
 (0)
Please sign in to comment.