Skip to content

Commit

Permalink
n-api: RangeError in napi_create_dataview()
Browse files Browse the repository at this point in the history
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].

This is a partial cherry-pick from upstream[2].

[1] https://nodejs.org/api/n-api.html#n_api_napi_create_dataview
[2] nodejs/node@2e329e9

PR-URL: #214
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
romandev authored and mhdawson committed Jan 18, 2018
1 parent 4058a29 commit 17c74e5
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3255,6 +3255,14 @@ napi_status napi_create_dataview(napi_env env,
RETURN_STATUS_IF_FALSE(env, value->IsArrayBuffer(), napi_invalid_arg);

v8::Local<v8::ArrayBuffer> buffer = value.As<v8::ArrayBuffer>();
if (byte_length + byte_offset > buffer->ByteLength()) {
napi_throw_range_error(
env,
"ERR_NAPI_INVALID_DATAVIEW_ARGS",
"byte_offset + byte_length should be less than or "
"equal to the size in bytes of the array passed in");
return napi_set_last_error(env, napi_generic_failure);
}
v8::Local<v8::DataView> DataView = v8::DataView::New(buffer, byte_offset,
byte_length);

Expand Down

0 comments on commit 17c74e5

Please sign in to comment.