From 7e269f52667104c94dcf65e527b04c2632d701bb Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 16 Apr 2018 22:25:11 +0800 Subject: [PATCH] src: migrate ERR_INDEX_OUT_OF_RANGE in C++ This patch migrates the "Index out of range" errors in C++ to errors with the code `ERR_INDEX_OUT_OF_RANGE` which have equivalents in JavaScript. PR-URL: https://github.com/nodejs/node/pull/20121 Reviewed-By: Daniel Bevenius Reviewed-By: Anna Henningsen Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Jeremiah Senkpiel --- src/node_buffer.cc | 9 +++++---- src/node_errors.h | 4 +++- test/parallel/test-buffer-alloc.js | 6 +++++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 15e6c7a0c492d3..2e968697176ead 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -21,6 +21,7 @@ #include "node.h" #include "node_buffer.h" +#include "node_errors.h" #include "env-inl.h" #include "string_bytes.h" @@ -38,7 +39,7 @@ #define THROW_AND_RETURN_IF_OOB(r) \ do { \ - if (!(r)) return env->ThrowRangeError("Index out of range"); \ + if (!(r)) return node::THROW_ERR_INDEX_OUT_OF_RANGE(env); \ } while (0) #define SLICE_START_END(start_arg, end_arg, end_max) \ @@ -544,7 +545,7 @@ void Copy(const FunctionCallbackInfo &args) { return args.GetReturnValue().Set(0); if (source_start > ts_obj_length) - return env->ThrowRangeError("Index out of range"); + return node::THROW_ERR_INDEX_OUT_OF_RANGE(env); if (source_end - source_start > target_length - target_start) source_end = source_start + target_length - target_start; @@ -728,9 +729,9 @@ void CompareOffset(const FunctionCallbackInfo &args) { THROW_AND_RETURN_IF_OOB(ParseArrayIndex(args[5], ts_obj_length, &source_end)); if (source_start > ts_obj_length) - return env->ThrowRangeError("Index out of range"); + return node::THROW_ERR_INDEX_OUT_OF_RANGE(env); if (target_start > target_length) - return env->ThrowRangeError("Index out of range"); + return node::THROW_ERR_INDEX_OUT_OF_RANGE(env); CHECK_LE(source_start, source_end); CHECK_LE(target_start, target_end); diff --git a/src/node_errors.h b/src/node_errors.h index 4153553f5be0a9..584a62ece189ef 100644 --- a/src/node_errors.h +++ b/src/node_errors.h @@ -17,8 +17,9 @@ namespace node { // a `Local` containing the TypeError with proper code and message #define ERRORS_WITH_CODE(V) \ + V(ERR_INDEX_OUT_OF_RANGE, RangeError) \ V(ERR_MEMORY_ALLOCATION_FAILED, Error) \ - V(ERR_STRING_TOO_LONG, Error) \ + V(ERR_STRING_TOO_LONG, Error) \ V(ERR_BUFFER_TOO_LARGE, Error) #define V(code, type) \ @@ -42,6 +43,7 @@ namespace node { // Errors with predefined static messages #define PREDEFINED_ERROR_MESSAGES(V) \ + V(ERR_INDEX_OUT_OF_RANGE, "Index out of range") \ V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") #define V(code, message) \ diff --git a/test/parallel/test-buffer-alloc.js b/test/parallel/test-buffer-alloc.js index bce0ad83814435..b06ca2a68cffb0 100644 --- a/test/parallel/test-buffer-alloc.js +++ b/test/parallel/test-buffer-alloc.js @@ -982,7 +982,11 @@ common.expectsError(() => { const a = Buffer.alloc(1); const b = Buffer.alloc(1); a.copy(b, 0, 0x100000000, 0x100000001); -}, { code: undefined, type: RangeError, message: 'Index out of range' }); +}, { + code: 'ERR_INDEX_OUT_OF_RANGE', + type: RangeError, + message: 'Index out of range' +}); // Unpooled buffer (replaces SlowBuffer) {