-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
fs: allow int64 offset in fs.read/readSync/write/writeSync #26572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
75b6ec9
a888eca
6789e96
8f545bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,7 +85,7 @@ const { | |
isUint32, | ||
parseMode, | ||
validateBuffer, | ||
validateInteger, | ||
validateSafeInteger, | ||
validateInt32, | ||
validateUint32 | ||
} = require('internal/validators'); | ||
|
@@ -453,7 +453,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) { | ||
|
@@ -490,7 +495,12 @@ function readSync(fd, buffer, offset, length, position) { | |
validateInt32(fd, 'fd', 0); | ||
validateBuffer(buffer); | ||
|
||
offset |= 0; | ||
if (offset == null) { | ||
offset = 0; | ||
} else { | ||
validateSafeInteger(offset, 'offset'); | ||
} | ||
|
||
length |= 0; | ||
|
||
if (length === 0) { | ||
|
@@ -531,8 +541,11 @@ function write(fd, buffer, offset, length, position, callback) { | |
|
||
if (isArrayBufferView(buffer)) { | ||
callback = maybeCallback(callback || position || length || offset); | ||
if (typeof offset !== 'number') | ||
if (offset == null || typeof offset === 'function') { | ||
offset = 0; | ||
} else { | ||
validateSafeInteger(offset, 'offset'); | ||
} | ||
if (typeof length !== 'number') | ||
length = buffer.length - offset; | ||
if (typeof position !== 'number') | ||
|
@@ -570,8 +583,11 @@ function writeSync(fd, buffer, offset, length, position) { | |
if (isArrayBufferView(buffer)) { | ||
if (position === undefined) | ||
position = null; | ||
if (typeof offset !== 'number') | ||
if (offset == null) { | ||
offset = 0; | ||
} else { | ||
validateSafeInteger(offset, 'offset'); | ||
} | ||
if (typeof length !== 'number') | ||
length = buffer.byteLength - offset; | ||
validateOffsetLengthWrite(offset, length, buffer.byteLength); | ||
|
@@ -621,7 +637,7 @@ function truncate(path, len, callback) { | |
len = 0; | ||
} | ||
|
||
validateInteger(len, 'len'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these changes be a part of this PR, as we are dealing only with offsets here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bnoordhuis suggested the change (#26572 (comment)), and I isolated them in a separate commit (intending all commits in this PR to be merged, not squashed). |
||
validateSafeInteger(len, 'len'); | ||
callback = maybeCallback(callback); | ||
fs.open(path, 'r+', (er, fd) => { | ||
if (er) return callback(er); | ||
|
@@ -662,7 +678,7 @@ function ftruncate(fd, len = 0, callback) { | |
len = 0; | ||
} | ||
validateInt32(fd, 'fd', 0); | ||
validateInteger(len, 'len'); | ||
validateSafeInteger(len, 'len'); | ||
len = Math.max(0, len); | ||
const req = new FSReqCallback(); | ||
req.oncomplete = makeCallback(callback); | ||
|
@@ -671,7 +687,7 @@ function ftruncate(fd, len = 0, callback) { | |
|
||
function ftruncateSync(fd, len = 0) { | ||
validateInt32(fd, 'fd', 0); | ||
validateInteger(len, 'len'); | ||
validateSafeInteger(len, 'len'); | ||
len = Math.max(0, len); | ||
const ctx = {}; | ||
binding.ftruncate(fd, len, undefined, ctx); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
|
||
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#include <cmath> | ||
#include <cstring> | ||
#include "util.h" | ||
|
||
|
@@ -521,6 +522,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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Simply returning the result of this condition also should work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very true. This method is a verbatim port of the referenced part of ECMA-262 (https://www.ecma-international.org/ecma-262/9.0/index.html#sec-number.issafeinteger) though; I thought that would be clearer to follow exactly. |
||
return false; | ||
} | ||
|
||
} // namespace node | ||
|
||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
Uh oh!
There was an error while loading. Please reload this page.