From c700cc42da9cf73af9fec2098520a6c0a631d901 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sun, 20 May 2018 11:41:39 -0400 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/20851 Fixes: https://github.com/nodejs/node/issues/20844 Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Benjamin Gruenbaum --- lib/fs.js | 8 ++------ lib/internal/fs/promises.js | 4 ++-- test/parallel/test-fs-truncate.js | 13 ------------- 3 files changed, 4 insertions(+), 21 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index cfb4f2777b8388..b804336bcf6193 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -651,11 +651,7 @@ function ftruncate(fd, len = 0, callback) { len = 0; } validateUint32(fd, 'fd'); - // TODO(BridgeAR): This does not seem right. - // There does not seem to be any validation before and if there is any, it - // should work similar to validateUint32 or not have a upper cap at all. - // This applies to all usage of `validateInt32(len, 'len')`. - validateInt32(len, 'len'); + validateInteger(len, 'len'); len = Math.max(0, len); const req = new FSReqWrap(); req.oncomplete = makeCallback(callback); @@ -664,7 +660,7 @@ function ftruncate(fd, len = 0, callback) { function ftruncateSync(fd, len = 0) { validateUint32(fd, 'fd'); - validateInt32(len, 'len'); + validateInteger(len, 'len'); len = Math.max(0, len); const ctx = {}; binding.ftruncate(fd, len, undefined, ctx); diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 9700765f085732..687dfb5aead2a8 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -33,7 +33,7 @@ const { const { isUint32, validateAndMaskMode, - validateInt32, + validateInteger, validateUint32 } = require('internal/validators'); const pathModule = require('path'); @@ -263,7 +263,7 @@ async function truncate(path, len = 0) { async function ftruncate(handle, len = 0) { validateFileHandle(handle); - validateInt32(len, 'len'); + validateInteger(len, 'len'); len = Math.max(0, len); return binding.ftruncate(handle.fd, len, kUsePromises); } diff --git a/test/parallel/test-fs-truncate.js b/test/parallel/test-fs-truncate.js index 9bb601b7c9953c..8193e05bb7d4b0 100644 --- a/test/parallel/test-fs-truncate.js +++ b/test/parallel/test-fs-truncate.js @@ -222,19 +222,6 @@ function testFtruncate(cb) { ); }); - // 2 ** 31 = 2147483648 - [2147483648, -2147483649].forEach((input) => { - assert.throws( - () => 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 ' + - `>= -2147483648 && <= 2147483647. Received ${input}` - } - ); - }); - fs.ftruncate(fd, undefined, common.mustCall(function(err) { assert.ifError(err); assert(fs.readFileSync(file5).equals(Buffer.from('')));