fs: coerce -0 to +0 in mode flags and watch intervals

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/63556
Reviewed-By: Jordan Harband <ljharb@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
This commit is contained in:
Filip Skokan authored and Antoine du Hamel committed 2026-07-30 13:51:29 +02:00
1 parent d4cfe2d8ac
commit d3d7cd05e3
4 files changed
+39 -2

No files matched your search

+2 -1
View File
@@ -592,7 +592,8 @@ function getStatFsFromBinding(stats) {
function stringToFlags(flags, name = 'flags') {
if (typeof flags === 'number') {
validateInt32(flags, name);
return flags;
// Coerce -0 to +0.
return flags + 0;
}
if (flags == null) {
+2
View File
@@ -176,6 +176,8 @@ StatWatcher.prototype[kFSStatWatcherStart] = function(filename,
filename = getValidatedPath(filename, 'filename');
validateUint32(interval, 'interval');
// Coerce -0 to +0.
interval += 0;
const err = this._handle.start(toNamespacedPath(filename), interval);
if (err) {
const error = new UVException({
+2 -1
View File
@@ -80,7 +80,8 @@ function parseFileMode(value, name, def) {
}
validateUint32(value, name);
return value;
// Coerce -0 to +0.
return value + 0;
}
/**
+33
View File
@@ -0,0 +1,33 @@
'use strict';
require('../common');
const fs = require('fs');
const path = require('path');
const os = require('os');
const missing = path.join(
os.tmpdir(),
`node-fs-negative-zero-${process.pid}`,
'entry',
);
function ignoreExpectedError(fn) {
try {
fn();
} catch {
// Ignore expected file system errors from the missing path.
}
}
const fd = fs.openSync(process.execPath, -0);
fs.closeSync(fd);
ignoreExpectedError(() => fs.openSync(process.execPath, 'r', -0));
ignoreExpectedError(() => fs.readFileSync(process.execPath, { flag: -0 }));
ignoreExpectedError(() => fs.mkdirSync(missing, { mode: -0 }));
ignoreExpectedError(() => fs.chmodSync(missing, -0));
ignoreExpectedError(() => fs.writeFileSync(missing, '', { mode: -0 }));
fs.watchFile(missing, { interval: -0 }, () => {});
fs.unwatchFile(missing);