Skip to content

fs.ftruncate silently accepts negative offsets rather than failing with ERR_INVALID #35632

Closed
@sbc100

Description

@sbc100

fs.ftruncate and fd.ftruncateSync both silently ignore negative offsets:

node/lib/fs.js

Line 840 in 2cfdf28

len = MathMax(0, len);

This didn't always do behave like this.. looks like it was introduced in 8974df1

What steps will reproduce the bug?

var fs = require("fs");
var fd = fs.openSync("f", 'w+');
console.log(fs.ftruncateSync(fd, -99));

What is the expected behavior?

The ftruncate POSIX function is described as returning EINVAL when given a negative offset:
https://linux.die.net/man/2/ftruncate

In emscripten we emulate a POSIX environment on top of the Web and on top of node and expect ftruncate to fail in the same way.

We can obviously add a check to our code as a workaround but this does seem like a bug in node.

What do you see instead?

Silently assumed 0 length is what the caller really wants.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.fsIssues and PRs related to the fs subsystem / file system.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions