Skip to content

Commit

Permalink
fs: fixup negative length in fs.truncate
Browse files Browse the repository at this point in the history
`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" #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 <[email protected]>
Fixes: #35632
  • Loading branch information
jasnell committed Feb 22, 2021
1 parent f3eb224 commit 56b265a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 56b265a

Please sign in to comment.