Skip to content

vfs: fix negative timestamps in msToTimeSpec - #67753

Open
lingyicute wants to merge 1 commit into
nodejs:mainfrom
lingyicute:main
Open

lingyicute wants to merge 1 commit into
nodejs:mainfrom
lingyicute:main

Conversation

@lingyicute

Copy link
Copy Markdown

msToTimeSpec() in lib/internal/vfs/stats.js previously paired MathFloor(ms / 1000) for sec with (ms % 1000) * 1_000_000 for nsec.

Because % in JavaScript is a truncating remainder rather than floor modulo, any negative timestamp with a sub-second component (e.g. ms = -500) produced sec = -1 and nsec = -500_000_000. When getStatsFromBinding() reconstructed the timestamp via sec * 1000 + nsec / 1e6 (or sec * 1000000000n + nsec for BigIntStats), the sub-second part was subtracted twice, yielding -1500 instead of -500.

Compute nsec as (ms - sec * 1000) * 1_000_000 so that sec and nsec are consistent for pre-epoch timestamps.

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 <xlyxinglinyi@gmail.com>
@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. vfs Issues and PRs related to the virtual filesystem subsystem. labels Nov 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. vfs Issues and PRs related to the virtual filesystem subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants