n-api: throw RangeError in napi_create_dataview() with invalid range · nodejs/node@f693e81 · GitHub
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 <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 a4ba791 commit f693e81

5 files changed

Lines changed: 84 additions & 7 deletions

File tree

doc/api/errors.md

Lines changed: 6 additions & 0 deletions

lib/internal/errors.js

Lines changed: 3 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 16 additions & 6 deletions
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

Lines changed: 51 additions & 1 deletion

0 commit comments

Comments
 (0)