mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
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>
This commit is contained in:
3 files changed
+21
No files matched your search
@@ -406,6 +406,8 @@ try {
|
||||
If the file previously was shorter than `len` bytes, it is extended, and the
|
||||
extended part is filled with null bytes (`'\0'`):
|
||||
|
||||
If `len` is negative then `0` will be used.
|
||||
|
||||
#### `filehandle.utimes(atime, mtime)`
|
||||
<!-- YAML
|
||||
added: v10.0.0
|
||||
@@ -2295,6 +2297,8 @@ open('temp.txt', 'r+', (err, fd) => {
|
||||
If the file previously was shorter than `len` bytes, it is extended, and the
|
||||
extended part is filled with null bytes (`'\0'`):
|
||||
|
||||
If `len` is negative then `0` will be used.
|
||||
|
||||
### `fs.futimes(fd, atime, mtime, callback)`
|
||||
<!-- YAML
|
||||
added: v0.4.2
|
||||
|
||||
@@ -814,6 +814,7 @@ function truncate(path, len, callback) {
|
||||
}
|
||||
|
||||
validateInteger(len, 'len');
|
||||
len = MathMax(0, len);
|
||||
callback = maybeCallback(callback);
|
||||
fs.open(path, 'r+', (er, fd) => {
|
||||
if (er) return callback(er);
|
||||
|
||||
@@ -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)));
|
||||
}));
|
||||
}
|
||||
Reference in new issue
Block a user