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: nodejs/node-addon-api#214
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
Marlyfleitas committed Jan 18, 2018
1 parent 59dde73 commit dfe47fc
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 dfe47fc

Please sign in to comment.