Skip to content

Commit

Permalink
fs: allow int64 offset in fs.read/readSync/fd.read
Browse files Browse the repository at this point in the history
Since v10.10.0, 'buf' can be any DataView, meaning the largest
byteLength can be Float64Array.BYTES_PER_ELEMENT * kMaxLength =
17,179,869,176.

'offset' can now be up to 2**53 - 1. This makes it possible to tile
reads into a large buffer.

Ref nodejs#26563
  • Loading branch information
zbjornson committed Mar 18, 2019
1 parent 006d530 commit de27e44
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
14 changes: 12 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,12 @@ function read(fd, buffer, offset, length, position, callback) {
validateBuffer(buffer);
callback = maybeCallback(callback);

offset |= 0;
if (offset == null) {
offset = 0;
} else {
validateSafeInteger(offset, 'offset');
}

length |= 0;

if (length === 0) {
Expand Down Expand Up @@ -491,7 +496,12 @@ function readSync(fd, buffer, offset, length, position) {
validateUint32(fd, 'fd');
validateBuffer(buffer);

offset |= 0;
if (offset == null) {
offset = 0;
} else {
validateSafeInteger(offset, 'offset');
}

length |= 0;

if (length === 0) {
Expand Down
7 changes: 6 additions & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,12 @@ async function read(handle, buffer, offset, length, position) {
validateFileHandle(handle);
validateBuffer(buffer);

offset |= 0;
if (offset == null) {
offset = 0;
} else {
validateSafeInteger(offset, 'offset');
}

length |= 0;

if (length === 0)
Expand Down
14 changes: 8 additions & 6 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "node_buffer.h"
#include "node_process.h"
#include "node_stat_watcher.h"
#include "util.h"
#include "util-inl.h"

#include "tracing/trace_event.h"

Expand Down Expand Up @@ -1831,7 +1831,7 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
*
* 0 fd int32. file descriptor
* 1 buffer instance of Buffer
* 2 offset int32. offset to start reading into inside buffer
* 2 offset int64. offset to start reading into inside buffer
* 3 length int32. length to read
* 4 position int64. file position - -1 for current position
*/
Expand All @@ -1849,15 +1849,17 @@ static void Read(const FunctionCallbackInfo<Value>& args) {
char* buffer_data = Buffer::Data(buffer_obj);
size_t buffer_length = Buffer::Length(buffer_obj);

CHECK(args[2]->IsInt32());
const size_t off = static_cast<size_t>(args[2].As<Int32>()->Value());
CHECK_LT(off, buffer_length);
CHECK(IsSafeJsInt(args[2]));
const int64_t off_64 = args[2].As<Integer>()->Value();
CHECK_GE(off_64, 0);
CHECK_LT(static_cast<uint64_t>(off_64), buffer_length);
const size_t off = static_cast<size_t>(off_64);

CHECK(args[3]->IsInt32());
const size_t len = static_cast<size_t>(args[3].As<Int32>()->Value());
CHECK(Buffer::IsWithinBounds(off, len, buffer_length));

CHECK(args[4]->IsNumber());
CHECK(IsSafeJsInt(args[4]));
const int64_t pos = args[4].As<Integer>()->Value();

char* buf = buffer_data + off;
Expand Down
12 changes: 12 additions & 0 deletions src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include <cmath>
#include <cstring>
#include "util.h"

Expand Down Expand Up @@ -491,6 +492,17 @@ void ArrayBufferViewContents<T, S>::Read(v8::Local<v8::ArrayBufferView> abv) {
}
}

// ECMA262 20.1.2.5
inline bool IsSafeJsInt(v8::Local<v8::Value> v) {
if (!v->IsNumber()) return false;
double v_d = v.As<v8::Number>()->Value();
if (std::isnan(v_d)) return false;
if (std::isinf(v_d)) return false;
if (std::trunc(v_d) != v_d) return false; // not int
if (std::abs(v_d) <= static_cast<double>(kMaxSafeJsInteger)) return true;
return false;
}

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
Expand Down
4 changes: 4 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ void DumpBacktrace(FILE* fp);

#define UNREACHABLE() ABORT()

constexpr int64_t kMaxSafeJsInteger = 9007199254740991; // 2^53-1

inline bool IsSafeJsInt(v8::Local<v8::Value> v);

// TAILQ-style intrusive list node.
template <typename T>
class ListNode;
Expand Down

0 comments on commit de27e44

Please sign in to comment.