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
8 changes: 5 additions & 3 deletions lib/internal/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,13 @@ function validateLen(len) {
let err;

if (!isInt32(len)) {
if (typeof value !== 'number') {
if (typeof len !== 'number') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

err = new ERR_INVALID_ARG_TYPE('len', 'number', len);
} else {
// TODO(BridgeAR): Improve this error message.
} else if (!Number.isInteger(len)) {
err = new ERR_OUT_OF_RANGE('len', 'an integer', len);
} else {
// 2 ** 31 === 2147483648
err = new ERR_OUT_OF_RANGE('len', '> -2147483649 && < 2147483648', len);
}
}

Expand Down
29 changes: 27 additions & 2 deletions test/parallel/test-fs-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function testFtruncate(cb) {
process.on('exit', () => fs.closeSync(fd));

['', false, null, {}, []].forEach((input) => {
assert.throws(
common.expectsError(
() => fs.ftruncate(fd, input),
{
code: 'ERR_INVALID_ARG_TYPE',
Expand All @@ -190,6 +190,31 @@ function testFtruncate(cb) {
);
});

[-1.5, 1.5].forEach((input) => {
common.expectsError(
() => fs.ftruncate(fd, input),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "len" is out of range. It must be ' +
`an integer. Received ${input}`
}
);
});

// 2 ** 31 = 2147483648
[2147483648, -2147483649].forEach((input) => {
common.expectsError(
() => fs.ftruncate(fd, input),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "len" is out of range. It must be ' +
`> -2147483649 && < 2147483648. Received ${input}`
}
);
});

fs.ftruncate(fd, undefined, common.mustCall(function(err) {
assert.ifError(err);
assert(fs.readFileSync(file5).equals(Buffer.from('')));
Expand All @@ -209,7 +234,7 @@ function testFtruncate(cb) {

['', false, null, undefined, {}, []].forEach((input) => {
['ftruncate', 'ftruncateSync'].forEach((fnName) => {
assert.throws(
common.expectsError(
() => fs[fnName](input),
{
code: 'ERR_INVALID_ARG_TYPE',
Expand Down