Skip to content

vfs: fix negative and fractional 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

@lingyicute lingyicute commented Nov 24, 2026 •

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.

% in JavaScript is a truncating remainder, not a floored modulo, so any
negative timestamp with a sub-second part (e.g. ms = -500) produced
sec = -1 and nsec = -500_000_000. When getStatsFromBinding()
rebuilt the timestamp (sec * 1000 + nsec / 1e6 for Stats,
sec * 1_000_000_000n + nsec for BigIntStats) the sub-second part was
subtracted twice, giving -1500 instead of -500.

nsec was also never rounded to a whole number. Values that are not
exactly representable, such as utimesSync(path, 0.1234, 0)
(0.1234 * 1000 === 123.39999999999999), made BigInt() throw a
RangeError when { bigint: true } stats were requested.

This PR:

  • computes sec with MathTrunc() so it agrees with ms % 1000;
  • rounds nsec to whole nanoseconds, carrying into sec when it rounds up
    to 1e9;
  • normalizes the result to the file system convention (sec rounded down,
    nsec in [0, 999_999_999]). Stats derives its Temporal.Instant
    values with floor(ms / 1000) and the partial nanoseconds, so a negative
    nsec would still be wrong there;
  • adds + 0 so tiny negative values do not leak -0 into the stats.

Tests cover whole-second, whole-millisecond, fractional, sub-millisecond and
underflowing negative timestamps for files, directories and symlinks (both
Stats and BigIntStats), the Temporal.Instant fields (when Temporal is
available), and an end-to-end utimesSync() / statSync() round trip.

@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.

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.51%. Comparing base (7ed1ca0) to head (abaef53).
⚠️ Report is 24 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #67753   +/-   ##
=======================================
  Coverage   90.51%   90.51%           
=======================================
  Files         805      805           
  Lines      285878   285897   +19     
  Branches    55200    55196    -4     
=======================================
+ Hits       258756   258779   +23     
- Misses      17214    17215    +1     
+ Partials     9908     9903    -5     
Files with missing lines Coverage Δ
lib/internal/vfs/stats.js 100.00% <100.00%> (ø)

... and 27 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@lingyicute
lingyicute force-pushed the main branch 4 times, most recently from e26e65a to dbc4b4c Compare November 27, 2026 15:56
msToTimeSpec() paired MathFloor(ms / 1000) for `sec` with the truncating
remainder (ms % 1000) * 1_000_000 for `nsec`. For negative timestamps
with a sub-second part (for example -500) this produced sec = -1 and
nsec = -500_000_000, so the sub-second part was subtracted twice and the
resulting Stats and BigIntStats timestamps were shifted back by an extra
second.

In addition, nsec was never rounded to a whole number. Millisecond
values that are not exactly representable, such as 123.4 (which is
123.39999999999999 as a double, e.g. from utimesSync(path, 0.1234, 0)),
made BigInt() throw a RangeError when bigint stats were requested.

Compute sec with MathTrunc() so that it agrees with the truncating
remainder, round nsec to whole nanoseconds and normalize the result to
the convention used by the file system: sec rounded down and nsec in
[0, 999_999_999]. Keeping nsec non-negative also keeps the
Temporal.Instant values derived from Stats correct. Adding 0 prevents
-0 from leaking into the stats for tiny negative values.

Signed-off-by: lingyicute <xlyxinglinyi@gmail.com>
@lingyicute lingyicute changed the title vfs: fix negative timestamps in msToTimeSpec vfs: fix negative and fractional timestamps in msToTimeSpec Nov 27, 2026
@lingyicute

Copy link
Copy Markdown
Author

PR is rebased, ready for review.

@jasnell
jasnell requested a review from mcollina November 27, 2026 17:41
@MikeMcC399

Copy link
Copy Markdown
Member

The source branch for this PR is the main branch in your fork of the repo.

The pull request guidelines recommend that you create a new branch off of the upstream default branch (main), so that it has a different name. See Step 2: Branch.

GitHub does not allow changing the source (head) branch of an open PR. This is guidance you can follow if you open any new pull request in the future (but note that first-time contributors are limited to one open PR at a time).

@lingyicute

Copy link
Copy Markdown
Author

Thanks for your guidance!

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.

3 participants