diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 1544cc301cf..44981013498 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -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) { diff --git a/lib/internal/fs/watchers.js b/lib/internal/fs/watchers.js index c540307d79a..2dad214a729 100644 --- a/lib/internal/fs/watchers.js +++ b/lib/internal/fs/watchers.js @@ -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({ diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 29954a18c09..668e17c13da 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -80,7 +80,8 @@ function parseFileMode(value, name, def) { } validateUint32(value, name); - return value; + // Coerce -0 to +0. + return value + 0; } /** diff --git a/test/parallel/test-fs-negative-zero.js b/test/parallel/test-fs-negative-zero.js new file mode 100644 index 00000000000..538cea67faa --- /dev/null +++ b/test/parallel/test-fs-negative-zero.js @@ -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);