diff options
author | James M Snell <jasnell@gmail.com> | 2021-02-23 00:19:14 +0300 |
---|---|---|
committer | Michaël Zasso <targos@protonmail.com> | 2021-02-28 16:43:41 +0300 |
commit | 6b42e6598304fbb3a6af3053a2c6d226328774b0 (patch) | |
tree | 3392aa0064c9075fd2b3aa3358bb771612fdbb7b /test | |
parent | 4a40759b33355d26a54b822c8ca26cbac7c6f9bf (diff) |
fs: fixup negative length in fs.truncate
`fs.ftruncate`, `fsPromises.truncate`, and `fsPromises.ftruncate`
all adjust negative lengths to 0 before invoking the system call.
`fs.truncate()` was the one outlier.
This "fixes" https://github.com/nodejs/node/issues/35632 but in the
opposite direction than discussed in the issue -- specifically by
removing an EINVAL error from one function rather than adding it to
another.
Signed-off-by: James M Snell <jasnell@gmail.com>
Fixes: https://github.com/nodejs/node/issues/35632
PR-URL: https://github.com/nodejs/node/pull/37483
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-fs-truncate.js | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/test/parallel/test-fs-truncate.js b/test/parallel/test-fs-truncate.js index 418d56c047d..de9687ebb27 100644 --- a/test/parallel/test-fs-truncate.js +++ b/test/parallel/test-fs-truncate.js @@ -280,3 +280,19 @@ function testFtruncate(cb) { ); }); }); + +{ + const file1 = path.resolve(tmp, 'truncate-file-1.txt'); + fs.writeFileSync(file1, 'Hi'); + fs.truncateSync(file1, -1); // Negative coerced to 0, No error. + assert(fs.readFileSync(file1).equals(Buffer.alloc(0))); +} + +{ + const file1 = path.resolve(tmp, 'truncate-file-2.txt'); + fs.writeFileSync(file1, 'Hi'); + // Negative coerced to 0, No error. + fs.truncate(file1, -1, common.mustSucceed(() => { + assert(fs.readFileSync(file1).equals(Buffer.alloc(0))); + })); +} |