Skip to content

Commit ceba08a

Browse files
panvaaduh95
authored andcommitted
fs: coerce -0 to +0 in mode flags and watch intervals
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #63556 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 0cf8342 commit ceba08a

4 files changed

Lines changed: 39 additions & 2 deletions

File tree

lib/internal/fs/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,8 @@ function getStatFsFromBinding(stats) {
724724
function stringToFlags(flags, name = 'flags') {
725725
if (typeof flags === 'number') {
726726
validateInt32(flags, name);
727-
return flags;
727+
// Coerce -0 to +0.
728+
return flags + 0;
728729
}
729730

730731
if (flags == null) {

lib/internal/fs/watchers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ StatWatcher.prototype[kFSStatWatcherStart] = function(filename,
176176

177177
filename = getValidatedPath(filename, 'filename');
178178
validateUint32(interval, 'interval');
179+
// Coerce -0 to +0.
180+
interval += 0;
179181
const err = this._handle.start(toNamespacedPath(filename), interval);
180182
if (err) {
181183
const error = new UVException({

lib/internal/validators.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ function parseFileMode(value, name, def) {
8080
}
8181

8282
validateUint32(value, name);
83-
return value;
83+
// Coerce -0 to +0.
84+
return value + 0;
8485
}
8586

8687
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const fs = require('fs');
6+
const path = require('path');
7+
const os = require('os');
8+
9+
const missing = path.join(
10+
os.tmpdir(),
11+
`node-fs-negative-zero-${process.pid}`,
12+
'entry',
13+
);
14+
15+
function ignoreExpectedError(fn) {
16+
try {
17+
fn();
18+
} catch {
19+
// Ignore expected file system errors from the missing path.
20+
}
21+
}
22+
23+
const fd = fs.openSync(process.execPath, -0);
24+
fs.closeSync(fd);
25+
26+
ignoreExpectedError(() => fs.openSync(process.execPath, 'r', -0));
27+
ignoreExpectedError(() => fs.readFileSync(process.execPath, { flag: -0 }));
28+
ignoreExpectedError(() => fs.mkdirSync(missing, { mode: -0 }));
29+
ignoreExpectedError(() => fs.chmodSync(missing, -0));
30+
ignoreExpectedError(() => fs.writeFileSync(missing, '', { mode: -0 }));
31+
32+
fs.watchFile(missing, { interval: -0 }, () => {});
33+
fs.unwatchFile(missing);

0 commit comments

Comments
 (0)