Commit 2566f1b73da for nodejs
commit 2566f1b73da7f6acc82198c90798e5b920a70d7b
Author: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
Date: Sun Nov 22 19:13:12 2026 -0300
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>
diff --git a/lib/fs.js b/lib/fs.js
index c593fbfffb0..71ccb1d2cec 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -1115,6 +1115,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) {
@@ -1205,6 +1207,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) {
diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js
index 7ec15eaeab6..53eb2f4938d 100644
--- a/lib/internal/fs/promises.js
+++ b/lib/internal/fs/promises.js
@@ -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,
diff --git a/test/parallel/test-fs-negative-zero.js b/test/parallel/test-fs-negative-zero.js
index 66862762bfb..c8cda8aba6b 100644
--- a/test/parallel/test-fs-negative-zero.js
+++ b/test/parallel/test-fs-negative-zero.js
@@ -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());
+}