From 806c5730492a4a2f4f259e41a4b02431c88f4001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Arboleda?= Date: Fri, 29 Jan 2021 13:58:18 -0500 Subject: [PATCH] lib: avoid crash on `fs.fstatSync(-0)` call Fixes: https://github.com/nodejs/node/issues/37122 --- lib/fs.js | 3 ++- test/parallel/test-fs-stat.js | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/fs.js b/lib/fs.js index d01e99c21b0178..20c99e3224414e 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1099,7 +1099,8 @@ function hasNoEntryError(ctx) { function fstatSync(fd, options = { bigint: false, throwIfNoEntry: true }) { validateInt32(fd, 'fd', 0); const ctx = { fd }; - const stats = binding.fstat(fd, options.bigint, undefined, ctx); + // The fd should be `fd | 0` just for the edgecase when `fd = -0` + const stats = binding.fstat(fd | 0, options.bigint, undefined, ctx); handleErrorFromBinding(ctx); return getStatsFromBinding(stats); } diff --git a/test/parallel/test-fs-stat.js b/test/parallel/test-fs-stat.js index 31aec8fab00450..abd87f53e7f85c 100644 --- a/test/parallel/test-fs-stat.js +++ b/test/parallel/test-fs-stat.js @@ -69,6 +69,8 @@ fs.open('.', 'r', undefined, common.mustCall(function(err, fd) { fs.close(fd, common.mustSucceed()); })); +assert.ok(fs.fstatSync(-0)); + fs.stat(__filename, common.mustSucceed((s) => { assert.strictEqual(s.isDirectory(), false); assert.strictEqual(s.isFile(), true);