Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@
return node::THROW_ERR_OUT_OF_RANGE(env, "Index out of range"); \
} while (0) \

#define SLICE_START_END(env, start_arg, end_arg, end_max) \
size_t start = 0; \
size_t end = 0; \
THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, start_arg, 0, &start)); \
THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, end_arg, end_max, &end)); \
if (end < start) end = start; \
THROW_AND_RETURN_IF_OOB(Just(end <= end_max)); \
size_t length = end - start;

namespace node {
namespace Buffer {

Expand Down Expand Up @@ -468,7 +459,13 @@ void StringSlice(const FunctionCallbackInfo<Value>& args) {
if (buffer.length() == 0)
return args.GetReturnValue().SetEmptyString();

SLICE_START_END(env, args[0], args[1], buffer.length())
size_t start = 0;
size_t end = 0;
THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[0], 0, &start));
THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[1], buffer.length(), &end));
if (end < start) end = start;
THROW_AND_RETURN_IF_OOB(Just(end <= buffer.length()));
size_t length = end - start;

Local<Value> error;
MaybeLocal<Value> ret =
Expand Down
11 changes: 7 additions & 4 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ constexpr char kPathSeparator = '/';
const char* const kPathSeparator = "\\/";
#endif

#define GET_OFFSET(a) (IsSafeJsInt(a) ? (a).As<Integer>()->Value() : -1)
inline int64_t GetOffset(Local<Value> value) {
return IsSafeJsInt(value) ? value.As<Integer>()->Value() : -1;
}

#define TRACE_NAME(name) "fs.sync." #name
#define GET_TRACE_ENABLED \
(*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
Expand Down Expand Up @@ -1681,7 +1684,7 @@ static void WriteBuffer(const FunctionCallbackInfo<Value>& args) {
CHECK_LE(len, buffer_length);
CHECK_GE(off + len, off);

const int64_t pos = GET_OFFSET(args[4]);
const int64_t pos = GetOffset(args[4]);

char* buf = buffer_data + off;
uv_buf_t uvbuf = uv_buf_init(buf, len);
Expand Down Expand Up @@ -1721,7 +1724,7 @@ static void WriteBuffers(const FunctionCallbackInfo<Value>& args) {
CHECK(args[1]->IsArray());
Local<Array> chunks = args[1].As<Array>();

int64_t pos = GET_OFFSET(args[2]);
int64_t pos = GetOffset(args[2]);

MaybeStackBuffer<uv_buf_t> iovs(chunks->Length());

Expand Down Expand Up @@ -1765,7 +1768,7 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsInt32());
const int fd = args[0].As<Int32>()->Value();

const int64_t pos = GET_OFFSET(args[2]);
const int64_t pos = GetOffset(args[2]);

const auto enc = ParseEncoding(isolate, args[3], UTF8);

Expand Down