Skip to content

Commit c700cc4

Browse files
committed
fs: don't limit ftruncate() length to 32 bits
The length used by ftruncate() is 64 bits in the binding layer. This commit removes the 32 bit restriction in the JS layer. PR-URL: #20851 Fixes: #20844 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 751a42a commit c700cc4

File tree

3 files changed

+4
-21
lines changed

3 files changed

+4
-21
lines changed

lib/fs.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -651,11 +651,7 @@ function ftruncate(fd, len = 0, callback) {
651651
len = 0;
652652
}
653653
validateUint32(fd, 'fd');
654-
// TODO(BridgeAR): This does not seem right.
655-
// There does not seem to be any validation before and if there is any, it
656-
// should work similar to validateUint32 or not have a upper cap at all.
657-
// This applies to all usage of `validateInt32(len, 'len')`.
658-
validateInt32(len, 'len');
654+
validateInteger(len, 'len');
659655
len = Math.max(0, len);
660656
const req = new FSReqWrap();
661657
req.oncomplete = makeCallback(callback);
@@ -664,7 +660,7 @@ function ftruncate(fd, len = 0, callback) {
664660

665661
function ftruncateSync(fd, len = 0) {
666662
validateUint32(fd, 'fd');
667-
validateInt32(len, 'len');
663+
validateInteger(len, 'len');
668664
len = Math.max(0, len);
669665
const ctx = {};
670666
binding.ftruncate(fd, len, undefined, ctx);

lib/internal/fs/promises.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const {
3333
const {
3434
isUint32,
3535
validateAndMaskMode,
36-
validateInt32,
36+
validateInteger,
3737
validateUint32
3838
} = require('internal/validators');
3939
const pathModule = require('path');
@@ -263,7 +263,7 @@ async function truncate(path, len = 0) {
263263

264264
async function ftruncate(handle, len = 0) {
265265
validateFileHandle(handle);
266-
validateInt32(len, 'len');
266+
validateInteger(len, 'len');
267267
len = Math.max(0, len);
268268
return binding.ftruncate(handle.fd, len, kUsePromises);
269269
}

test/parallel/test-fs-truncate.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -222,19 +222,6 @@ function testFtruncate(cb) {
222222
);
223223
});
224224

225-
// 2 ** 31 = 2147483648
226-
[2147483648, -2147483649].forEach((input) => {
227-
assert.throws(
228-
() => fs.ftruncate(fd, input),
229-
{
230-
code: 'ERR_OUT_OF_RANGE',
231-
name: 'RangeError [ERR_OUT_OF_RANGE]',
232-
message: 'The value of "len" is out of range. It must be ' +
233-
`>= -2147483648 && <= 2147483647. Received ${input}`
234-
}
235-
);
236-
});
237-
238225
fs.ftruncate(fd, undefined, common.mustCall(function(err) {
239226
assert.ifError(err);
240227
assert(fs.readFileSync(file5).equals(Buffer.from('')));

0 commit comments

Comments
 (0)