From e880a7192765dc63fc4645168d52e858ff708a7c Mon Sep 17 00:00:00 2001 From: lingyicute Date: Tue, 24 Nov 2026 15:50:28 +0000 Subject: [PATCH] vfs: fix negative timestamps in msToTimeSpec msToTimeSpec() computed sec with MathFloor(ms / 1000) (floor division) but computed nsec with (ms % 1000) * 1_000_000 (truncating remainder). For negative non-integer-second timestamps (ms < 0 and ms % 1000 !== 0), sec rounded down to the previous second while nsec was also negative, subtracting the sub-second part twice and shifting the resulting Stats and BigIntStats timestamps back by an extra second. Compute the sub-second remainder as ms - sec * 1000 so that sec and nsec use the same floor division. Signed-off-by: lingyicute --- lib/internal/vfs/stats.js | 2 +- test/parallel/test-vfs-stats-helpers.js | 31 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/internal/vfs/stats.js b/lib/internal/vfs/stats.js index e41e52728c5..47be5c9a466 100644 --- a/lib/internal/vfs/stats.js +++ b/lib/internal/vfs/stats.js @@ -45,7 +45,7 @@ const bigintStatsArray = new BigInt64Array(18); */ function msToTimeSpec(ms) { const sec = MathFloor(ms / 1000); - const nsec = (ms % 1000) * 1_000_000; + const nsec = (ms - sec * 1000) * 1_000_000; return { sec, nsec }; } diff --git a/test/parallel/test-vfs-stats-helpers.js b/test/parallel/test-vfs-stats-helpers.js index 252aed7d405..42f35fb2fc5 100644 --- a/test/parallel/test-vfs-stats-helpers.js +++ b/test/parallel/test-vfs-stats-helpers.js @@ -78,3 +78,34 @@ const { process.getgid = realGid; } } + +{ + const st = createFileStats(0, { + atimeMs: -500, + mtimeMs: -1500, + ctimeMs: -1000, + birthtimeMs: -1, + }); + assert.strictEqual(st.atimeMs, -500); + assert.strictEqual(st.mtimeMs, -1500); + assert.strictEqual(st.ctimeMs, -1000); + assert.strictEqual(st.birthtimeMs, -1); + assert.strictEqual(st.atime.getTime(), -500); + assert.strictEqual(st.mtime.getTime(), -1500); + + const bigSt = createFileStats(0, { + atimeMs: -500, + mtimeMs: -1500, + ctimeMs: -1000, + birthtimeMs: -1, + bigint: true, + }); + assert.strictEqual(bigSt.atimeMs, -500n); + assert.strictEqual(bigSt.mtimeMs, -1500n); + assert.strictEqual(bigSt.ctimeMs, -1000n); + assert.strictEqual(bigSt.birthtimeMs, -1n); + assert.strictEqual(bigSt.atimeNs, -500_000_000n); + assert.strictEqual(bigSt.mtimeNs, -1_500_000_000n); + assert.strictEqual(bigSt.ctimeNs, -1_000_000_000n); + assert.strictEqual(bigSt.birthtimeNs, -1_000_000n); +}