Repository navigation
vfs: fix negative and fractional timestamps in msToTimeSpec - #67753
lingyicute wants to merge 1 commit into
Conversation
|
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 Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
e26e65a to
dbc4b4c
Compare
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>
|
PR is rebased, ready for review. |
|
The source branch for this PR is the The pull request guidelines recommend that you create a new branch off of the upstream default 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). |
|
Thanks for your guidance! |
msToTimeSpec()inlib/internal/vfs/stats.jspreviously pairedMathFloor(ms / 1000)forsecwith(ms % 1000) * 1_000_000fornsec.%in JavaScript is a truncating remainder, not a floored modulo, so anynegative timestamp with a sub-second part (e.g.
ms = -500) producedsec = -1andnsec = -500_000_000. WhengetStatsFromBinding()rebuilt the timestamp (
sec * 1000 + nsec / 1e6forStats,sec * 1_000_000_000n + nsecforBigIntStats) the sub-second part wassubtracted twice, giving
-1500instead of-500.nsecwas also never rounded to a whole number. Values that are notexactly representable, such as
utimesSync(path, 0.1234, 0)(
0.1234 * 1000 === 123.39999999999999), madeBigInt()throw aRangeErrorwhen{ bigint: true }stats were requested.This PR:
secwithMathTrunc()so it agrees withms % 1000;nsecto whole nanoseconds, carrying intosecwhen it rounds upto
1e9;secrounded down,nsecin[0, 999_999_999]).Statsderives itsTemporal.Instantvalues with
floor(ms / 1000)and the partial nanoseconds, so a negativensecwould still be wrong there;+ 0so tiny negative values do not leak-0into the stats.Tests cover whole-second, whole-millisecond, fractional, sub-millisecond and
underflowing negative timestamps for files, directories and symlinks (both
StatsandBigIntStats), theTemporal.Instantfields (when Temporal isavailable), and an end-to-end
utimesSync()/statSync()round trip.