-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
fix: ftruncate for negative and maximum values #35645
Conversation
lib/fs.js
Outdated
len = MathMax(0, len); | ||
|
||
if (len < 0) { | ||
throw new ERR_FS_FILE_TOO_SMALL(len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use ERR_INVALID_ARG_VALUE
here – it’s not the file that has a negative length, it is an invalid argument to the function here.
Likewise, using kIoMaxLength
/ERR_FS_FILE_TOO_LARGE
seems a bit odd here. The 2 GB limit is because that is the maximum buffer size supported, but Node.js generally does support way larger files than that (just not reading or writing to them in multi-GB chunks). We should not fail just because len
exceeds this smaller limit, and only fail if it exceeds the actual limit after which the operation would fail or do the wrong thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense!
So, that should solve
if (len < 0) {
throw new ERR_INVALID_ARG_VALUE('len', len);
}
I'll update the PR
@IgorHalfeld can you drop the merge commit and rebase please? Merge commits break our tooling. |
@IgorHalfeld - this needs a rebase |
Done 🎉 |
@IgorHalfeld can you rebase rather than using merge commits please? git fetch upstream master
git rebase upstream/master -i Then you select the commits you want to keep (all of them except the merge commits), fix git conflicts, and force push to your The reason this is needed is because the CI downloads the patch from GitHub and tries to apply it on top of the |
9843a74
to
e04fd9b
Compare
any updates? |
assert.throws( | ||
() => fs.ftruncate(fd, -1), | ||
{ | ||
code: 'ERR_INVALID_ARG_VALUE', | ||
name: /(TypeError|RangeError)/, | ||
message: 'The argument \'len\' is invalid. Received -1' | ||
} | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@IgorHalfeld The fd
you have used here is already closed by the time this is run but the test passes because ERR_INVALID_ARG_VALUE
is thrown earlier:
node/test/parallel/test-fs-truncate.js
Line 71 in 7efada6
fs.closeSync(fd); |
Instead of replacing the previous test completely, consider replacing only this part with your test (you may take a look at the other tests for help):
node/test/parallel/test-fs-truncate.js
Lines 231 to 233 in 7efada6
fs.ftruncate(fd, -1, common.mustSucceed(() => { | |
assert(fs.readFileSync(file6).equals(Buffer.from(''))); | |
})); |
Also, fs.ftruncate
needs to be called with a callback: https://nodejs.org/api/fs.html#fs_fs_ftruncate_fd_len_callback
Similarly, consider adding a corresponding test for fs.ftruncateSync
too: https://nodejs.org/api/fs.html#fs_fs_ftruncatesync_fd_len
My suggestion was very architecture specific.
validateInteger(len, 'len'); | ||
len = MathMax(0, len); | ||
|
||
if (len < 0) { | ||
throw new ERR_INVALID_ARG_VALUE('len', len); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we throw a ERR_OUT_OF_RANGE
instead?
validateInteger(len, 'len'); | |
len = MathMax(0, len); | |
if (len < 0) { | |
throw new ERR_INVALID_ARG_VALUE('len', len); | |
} | |
validateInteger(len, 'len', 0); |
validateInteger(len, 'len'); | ||
len = MathMax(0, len); | ||
|
||
if (len < 0) { | ||
throw new ERR_INVALID_ARG_VALUE('len', len); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we throw a ERR_OUT_OF_RANGE
instead?
validateInteger(len, 'len'); | |
len = MathMax(0, len); | |
if (len < 0) { | |
throw new ERR_INVALID_ARG_VALUE('len', len); | |
} | |
validateInteger(len, 'len', 0); |
assert.throws( | ||
() => fs.ftruncate(fd, -1), | ||
{ | ||
code: 'ERR_INVALID_ARG_VALUE', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code: 'ERR_INVALID_ARG_VALUE', | |
code: 'ERR_OUT_OF_RANGE', |
Thank you very much for the contribution. |
fix:
fs.ftruncate
andfd.ftruncateSync
both silently ignore negative offsetsFixes #35632
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes