Skip to content

Commit 806c573

Browse files
committed
lib: avoid crash on fs.fstatSync(-0) call
Fixes: nodejs#37122
1 parent 17467d1 commit 806c573

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

lib/fs.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,8 @@ function hasNoEntryError(ctx) {
10991099
function fstatSync(fd, options = { bigint: false, throwIfNoEntry: true }) {
11001100
validateInt32(fd, 'fd', 0);
11011101
const ctx = { fd };
1102-
const stats = binding.fstat(fd, options.bigint, undefined, ctx);
1102+
// The fd should be `fd | 0` just for the edgecase when `fd = -0`
1103+
const stats = binding.fstat(fd | 0, options.bigint, undefined, ctx);
11031104
handleErrorFromBinding(ctx);
11041105
return getStatsFromBinding(stats);
11051106
}

test/parallel/test-fs-stat.js

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ fs.open('.', 'r', undefined, common.mustCall(function(err, fd) {
6969
fs.close(fd, common.mustSucceed());
7070
}));
7171

72+
assert.ok(fs.fstatSync(-0));
73+
7274
fs.stat(__filename, common.mustSucceed((s) => {
7375
assert.strictEqual(s.isDirectory(), false);
7476
assert.strictEqual(s.isFile(), true);

0 commit comments

Comments
 (0)