From 1084024e7c67845ae69bd7ecd55fcb8a0de13327 Mon Sep 17 00:00:00 2001 From: Jinho Bang Date: Sun, 14 Jan 2018 20:13:05 +0900 Subject: [PATCH] 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]. 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 --- src/node_api.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/node_api.cc b/src/node_api.cc index 119b67622..36e5dc26d 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -3253,6 +3253,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);