From fc0b3610e22c0d6056efd3089b3fe710be54463a 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. Backport-PR-URL: https://github.com/nodejs/node/pull/21171 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 Backport-PR-URL: https://github.com/nodejs/node/pull/21171 Co-authored-by: Shelley Vohr --- lib/fs.js | 9 ++------- lib/internal/fs/promises.js | 4 ++-- test/parallel/test-fs-truncate.js | 5 +---- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index cc559a898dc4ea..7168b7875498c5 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -86,7 +86,6 @@ const { const { isUint32, validateInteger, - validateInt32, validateUint32 } = require('internal/validators'); @@ -788,11 +787,7 @@ fs.ftruncate = function(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); @@ -801,7 +796,7 @@ fs.ftruncate = function(fd, len = 0, callback) { fs.ftruncateSync = function(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 80b30c1a8fa21a..3646a4121c7b3a 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -33,7 +33,7 @@ const { validatePath } = require('internal/fs/utils'); const { - validateInt32, + validateInteger, validateUint32 } = require('internal/validators'); const pathModule = require('path'); @@ -264,7 +264,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 347cc0e10492d6..735385f61c5249 100644 --- a/test/parallel/test-fs-truncate.js +++ b/test/parallel/test-fs-truncate.js @@ -220,17 +220,14 @@ function testFtruncate(cb) { `an integer. Received ${input}` } ); - }); - // 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 ' + - `> -2147483649 && < 2147483648. Received ${input}` + `an integer. Received ${input}` } ); });