Skip to content

Commit

Permalink
src: migrate ERR_INDEX_OUT_OF_RANGE in C++
Browse files Browse the repository at this point in the history
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: #20121
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
  • Loading branch information
joyeecheung authored and jasnell committed Apr 19, 2018
1 parent c218854 commit 1d0ad63
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "node.h"
#include "node_buffer.h"
#include "node_errors.h"

#include "env-inl.h"
#include "string_bytes.h"
Expand All @@ -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) \
Expand Down Expand Up @@ -544,7 +545,7 @@ void Copy(const FunctionCallbackInfo<Value> &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;
Expand Down Expand Up @@ -728,9 +729,9 @@ void CompareOffset(const FunctionCallbackInfo<Value> &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);
Expand Down
4 changes: 3 additions & 1 deletion src/node_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ namespace node {
// a `Local<Value>` 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) \
Expand All @@ -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) \
Expand Down
6 changes: 5 additions & 1 deletion test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 1d0ad63

Please sign in to comment.