From 17c74e5a5e12752ee05a55e2f3c3724b56705088 Mon Sep 17 00:00:00 2001 From: Jinho Bang Date: Sun, 14 Jan 2018 20:13:05 +0900 Subject: [PATCH] n-api: RangeError in napi_create_dataview() 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] https://github.com/nodejs/node/pull/17869/commits/2e329e9b776e0bea5e045a370858fb908dbdea5d PR-URL: https://github.com/nodejs/node-addon-api/pull/214 Reviewed-By: Michael Dawson --- src/node_api.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/node_api.cc b/src/node_api.cc index 5a2dbb06b..235f3dbac 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -3255,6 +3255,14 @@ napi_status napi_create_dataview(napi_env env, RETURN_STATUS_IF_FALSE(env, value->IsArrayBuffer(), napi_invalid_arg); v8::Local buffer = value.As(); + 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 DataView = v8::DataView::New(buffer, byte_offset, byte_length);