Skip to content

Commit f693e81

Browse files
romandevevanlucas
authored andcommitted
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 PR-URL: #17869 Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent a4ba791 commit f693e81

File tree

5 files changed

+84
-7
lines changed

5 files changed

+84
-7
lines changed

doc/api/errors.md

+6
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,12 @@ While using `N-API`, a constructor passed was not a function.
12651265

12661266
While using `N-API`, `Constructor.prototype` was not an object.
12671267

1268+
<a id="ERR_NAPI_INVALID_DATAVIEW_ARGS"></a>
1269+
### ERR_NAPI_INVALID_DATAVIEW_ARGS
1270+
1271+
While calling `napi_create_dataview()`, a given `offset` was outside the bounds
1272+
of the dataview or `offset + length` was larger than a length of given `buffer`.
1273+
12681274
<a id="ERR_NO_CRYPTO"></a>
12691275
### ERR_NO_CRYPTO
12701276

lib/internal/errors.js

+3
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ E('ERR_MODULE_RESOLUTION_LEGACY', '%s not found by import in %s.' +
316316
E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times');
317317
E('ERR_NAPI_CONS_FUNCTION', 'Constructor must be a function');
318318
E('ERR_NAPI_CONS_PROTOTYPE_OBJECT', 'Constructor.prototype must be an object');
319+
E('ERR_NAPI_INVALID_DATAVIEW_ARGS',
320+
'byte_offset + byte_length should be less than or eqaul to the size in ' +
321+
'bytes of the array passed in');
319322
E('ERR_NO_CRYPTO', 'Node.js is not compiled with OpenSSL crypto support');
320323
E('ERR_NO_ICU', '%s is not supported on Node.js compiled without ICU');
321324
E('ERR_NO_LONGER_SUPPORTED', '%s is no longer supported');

src/node_api.cc

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

31723172
v8::Local<v8::ArrayBuffer> buffer = value.As<v8::ArrayBuffer>();
3173+
if (byte_length + byte_offset > buffer->ByteLength()) {
3174+
napi_throw_range_error(
3175+
env,
3176+
"ERR_NAPI_INVALID_DATAVIEW_ARGS",
3177+
"byte_offset + byte_length should be less than or "
3178+
"equal to the size in bytes of the array passed in");
3179+
return napi_set_last_error(env, napi_pending_exception);
3180+
}
31733181
v8::Local<v8::DataView> DataView = v8::DataView::New(buffer, byte_offset,
31743182
byte_length);
31753183

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)