fs: fix abort on negative zero write length

`validateOffsetLengthWrite()` accepts -0 as a length, but V8 does not
represent -0 as an Int32 value, so the `IsInt32()` check in the binding
aborted the process in `fs.write()`, `fs.writeSync()` and
`filehandle.write()`.

Coerce the validated length to an int32 before the call, as `fs.read()`
already does.

Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67524
Refs: https://github.com/nodejs/node/pull/65888
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Christian Aurich Zanettini Martins authored and Antoine du Hamel committed 2026-11-25 18:00:54 +01:00
1 parent f25cdbc19c
commit ee6fd3bbc8
3 files changed
+32 -1

No files matched your search

+4
View File
@@ -1120,6 +1120,8 @@ function write(fd, buffer, offsetOrOptions, length, position, callback) {
if (typeof position !== 'number')
position = null;
validateOffsetLengthWrite(offset, length, buffer.byteLength);
// V8 does not report -0 as an int32, which the binding requires.
length |= 0;
const h = vfsState.handlers;
if (h !== null) {
@@ -1210,6 +1212,8 @@ function writeSync(fd, buffer, offsetOrOptions, length, position) {
if (typeof length !== 'number')
length = buffer.byteLength - offset;
validateOffsetLengthWrite(offset, length, buffer.byteLength);
// V8 does not report -0 as an int32, which the binding requires.
length |= 0;
const h = vfsState.handlers;
if (h !== null) {
+2
View File
@@ -1576,6 +1576,8 @@ async function write(handle, buffer, offsetOrOptions, length, position) {
if (typeof position !== 'number')
position = null;
validateOffsetLengthWrite(offset, length, buffer.byteLength);
// V8 does not report -0 as an int32, which the binding requires.
length |= 0;
const bytesWritten =
(await PromisePrototypeThen(
binding.writeBuffer(handle.fd, buffer, offset,
+26 -1
View File
@@ -1,6 +1,6 @@
'use strict';
require('../common');
const common = require('../common');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const { spawnSync } = require('child_process');
@@ -45,3 +45,28 @@ assert.strictEqual(child.stdout.toString(), 'hello');
fs.watchFile(missing, { interval: -0 }, () => {});
fs.unwatchFile(missing);
// A length of -0 writes nothing, like 0.
{
const buffer = Buffer.from('abc');
const fd = fs.openSync(tmpdir.resolve('negative-zero-fd.txt'), 'w');
assert.strictEqual(fs.writeSync(fd, buffer, 0, -0), 0);
assert.strictEqual(fs.writeSync(fd, buffer, { length: -0 }), 0);
fs.write(fd, buffer, 0, -0, common.mustSucceed((written) => {
assert.strictEqual(written, 0);
fs.write(fd, buffer, { length: -0 }, common.mustSucceed((written) => {
assert.strictEqual(written, 0);
fs.closeSync(fd);
}));
}));
(async () => {
const handle =
await fs.promises.open(tmpdir.resolve('negative-zero-handle.txt'), 'w');
assert.strictEqual((await handle.write(buffer, 0, -0)).bytesWritten, 0);
assert.strictEqual(
(await handle.write(buffer, { length: -0 })).bytesWritten, 0);
await handle.close();
})().then(common.mustCall());
}